0

I have a textBox textchanged event:

private void anyTextBox_TextChanged(object sender, EventArgs e)
{
  btnUpload.Enabled = txtHost.TextLength > 0 && txtUploadFile.TextLength > 0;
}

And I have a onPaint event:

protected override void OnPaint(PaintEventArgs e)
{
  base.OnPaint(e);
  Pen penBorder = new Pen(Color.Red, 3);
  if (btnUpload.Enabled == false)
  {
    e.Graphics.DrawRectangle(penBorder, txtHost.Location.X, txtHost.Location.Y,
                                        txtHost.Width - 1, txtHost.Height - 1);
    e.Graphics.DrawRectangle(penBorder, txtUploadFile.Location.X, txtUploadFile.Location.Y,
                                        txtUploadFile.Width - 1, txtUploadFile.Height - 1);
  }
}

But now when I type text in one of the textboxes depending which one to remove the red rectangle around it and if both textboxes with text remove the red rectangle around both of them.

The problem is that the OnPaint event is being called only once when I'm running the program.

LarsTech
  • 80,625
  • 14
  • 153
  • 225
Daniel Lip
  • 3,867
  • 7
  • 58
  • 120
  • 1
    Duplicate of [Drawing with Winforms](http://stackoverflow.com/questions/8996409/drawing-with-winforms), or possibly [How to have a control redraw the Windows form?](http://stackoverflow.com/questions/23910733/how-to-have-a-control-redraw-the-windows-form) – Peter Duniho Nov 11 '14 at 07:11
  • Yes the problem i didn't explain good is how to remove/delete the rectangles in the OnPaint event once i called it again and now the btnUpload enabled is true. When it's true i need somehow it to remove/delete the rectangle so the textBoxes will return to how they are in original. Just doing txtHost.Invalidate(); in the Textchanged event is not good. – Daniel Lip Nov 11 '14 at 07:20
  • If you call `Invalidate()` without any arguments, it will force the entire control to redraw. But you do have to call the method on the right `Control` instance, i.e. the one you want redrawn, or an ancestor of that `Control` (e.g. its immediate parent, the whole `Form`, etc.) If you redraw the entire `Control` and the button's `Enabled` property is `true`, then the rectangles simply won't be drawn and that will have the effect of "removing" them. If that's not happening, you're doing something wrong, but there's not enough code in your example to know what, exactly. – Peter Duniho Nov 11 '14 at 07:40
  • Control invalidate didn't work but this.Invalidate(); is working. – Daniel Lip Nov 11 '14 at 18:07
  • I don't know what you mean. `this` is a `Control`, so you _are_ calling the `Control.Invalidate()` method. – Peter Duniho Nov 11 '14 at 18:08

2 Answers2

1

Use the Control.Invalidate() method to force a control to be redrawn.

Peter Duniho
  • 68,759
  • 7
  • 102
  • 136
0

Invalidate() marks the control to the current update region of the window which will be repainted when the next WM_PAINT message is received. This can be earlier or later so it may not always have the effect you wish.

Update() sends this WM_PAINT message and also bypasses the message-queque, so you might want to use a combination of Invalidate() and Update() to always get the result you are hoping for.

To make it easier just call Refresh() because this will do Invalidate() and Update() for you synchronously.

Paul Weiland
  • 727
  • 10
  • 24