62952

How to call OnPaint event from TextBox TextChanged event?

Question:

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.

Answer1:

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

Answer2:

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.

Recommend

  • Way to display image scale in picturebox [closed]
  • What is window.onpaint?
  • Light-weight winform graphic element substitute of Control
  • How to add informative text to menu strip separator in vb.net or C#
  • c# Resize parent control when a child pictureBox changes the Image
  • Responsive SVG line graph with Raphaël
  • Find polygon perimeter of points quickly in Javascript
  • How to override UserControl class to draw a custom border?
  • How to Get TCanvas DC in Firemonkey?
  • How to store the last counter value in css and display as content in html?
  • css font-size and line-height not matching the baseline
  • Winforms: Add a close “x” button in a UserControl
  • In matplotlib, how do you change the fontsize of a single figure?
  • Spring: No transaction manager has been configured
  • accepts_nested_attributes_for practical form use for in Rails 3
  • Object and struct member access and address offset calculation
  • Highlight and Bold text in JTextPane
  • Algorithm for a smudge tool?
  • Xamarin Forms - UWP Fonts
  • Update CALayer sublayers immediately
  • JFileChooser in front of fullscreen Swing application
  • How reduce the height of an mschart by breaking up the y-axis
  • How to convert from System.Drawing.Color to Excel.ColorFormat in C#? Change comment color
  • Why doesn't :active or :focus work on text links in webkit? (safari & chrome)
  • How to apply VCL Styles to DLL-based forms in Inno Setup?
  • QuartzCore.framework for Mono Develop
  • Warning: Can't call setState (or forceUpdate) on an unmounted component
  • Arrays break string types in Julia
  • How to format a variable of double type
  • VB.net deserialize, JSON Conversion from type 'Dictionary(Of String,Object)' to type '
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • How do I configure my settings file to work with unit tests?
  • Android Google Maps API OnLocationChanged only called once
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • Bitwise OR returns boolean when one of operands is nil
  • unknown Exception android
  • sending mail using smtp is too slow
  • costura.fody for a dll that references another dll
  • Binding checkboxes to object values in AngularJs
  • failed to connect to specific WiFi in android programmatically