How to raise a Control.Resize event without actually resizing?

I do not subclass the control. An attempt to trigger an event through Control.Size = Control.Size fails, because it does not fire even if the new size is the same.

+4
source share
4 answers

If you subclass Control , you can directly call OnResize or set it in the API:

  public void OnResize() { this.OnResize(EventArgs.Empty); } 

However, you cannot do this for arbitrary controls. Can you change the Size on-and-fro? Alternatively, you can use reflection, but this is hacked:

  typeof (Control).GetMethod("OnResize", BindingFlags.Instance | BindingFlags.NonPublic) .Invoke(myControl, new object[] {EventArgs.Empty}); 
+8
source

I always do this by calling the Control Resize event handler:

 control_Resize(null, null); 
+1
source

Just resize the control using: Control.Size = new Size (x, y);

Resizing the control will throw a resize event for that control, and the control should resize.

Alternatively, if you just want to redraw the control, follow these steps: Control.Invalidate ();

0
source

Why do you want to do this and in which scenario? You can call OnResize, for example, when you are in the control itself (i.e. in your derived control class). (Or through Reflection when you are outside.)

Also, you probably have to resize the control, as this is a Resize event for :)

0
source

All Articles