WinForms - action after resize event
Solution 1
Just use the ResizeEnd
event:
private void Form1_ResizeEnd(object sender, EventArgs e)
{
// Your code here
}
From MSDN:
The ResizeEnd event is raised when the user finishes resizing a form, typically by dragging one of the borders or the sizing grip located on the lower-right corner of the form, and then releasing it. For more information about the resizing operation.
Solution 2
You can fake a local ResizeEnd like this:
public class Dummy:UserControl
{
private readonly Timer _tDelayedResize;
public Dummy()
{
this.Resize += this_Resize;
_tDelayedResize = new Timer();
_tDelayedResize.Interval = 5;
_tDelayedResize.Tick += this_ResizeEnd;
}
void this_Resize(object sender, EventArgs e)
{
_tDelayedResize.Stop();
_tDelayedResize.Start();
}
void this_ResizeEnd(object sender, EventArgs e)
{
_tDelayedResize.Stop();
//Do your ResizeEnd logic here
//...
}
}
The interval can be modified. The higher it is the more delay after the last resize event it will be.
Solution 3
Another option if you are using a control and not a form and would like to perform actions after the resize has been completed (user stopped resizing control):
private int resizeTimeout = 0;
private Task resizeTask;
public void OnResize()
{
resizeTimeout = 300; //Reset timeout
//Only resize on completion. This after resizeTimeout if no interaction.
if (resizeTask == null || resizeTask.IsCompleted)
{
resizeTask = Task.Run(async () =>
{
//Sleep until timeout has been reached
while (resizeTimeout > 0)
{
await Task.Delay(100);
resizeTimeout -= 100;
}
ResizeControl();
});
}
}
private void ResizeControl()
{
if (this.InvokeRequired)
{
//Call the same method in the context of the main UI thread.
this.Invoke((MethodInvoker)delegate { ResizeControl(); });
}
else
{
// Do resize actions here
}
}
Related videos on Youtube
Comments
-
brovar almost 2 years
Is it possible to perform a specific action after the resize event (of the user control), for example when mouse button is released? I need to manually resize an inner control and doing it on every single firing of the event would be quite, hmm, inefficient...
-
djdd87 about 14 years@leppie - the Resize happens a number of times during a user resizing a window. He wants an event that occurs when the user has finished resizing.
-
-
brovar about 14 yearsVery tempting and I'd probably have already used it, but it's happening in the user control (I forgot to specify that, sorry) and I don't have access to the form's events.
-
leppie about 14 years@brovar: That's baloney! Every control has a
ParentForm
property. So in fact you have all that accessible! -
djdd87 about 14 years@Why can you not just add a
ResizeMeNow()
method to the user control and call it on the Form'sResizeEnd
event? -
djdd87 about 14 years@brovar - or like leppie has said, you can just hook into UserControl.ParentForm.ResizeEnd+= ... etc.
-
brovar about 14 yearsYes, I do have ParentForm, but it's not accessible after Handles and AddHandler is not something very welcomed in this application's code. (yep, VB.NET, not c#)
-
brovar about 14 yearsJust like Resize, time after time.
-
Jazimov over 4 yearsI'm just looking at this code (I didn't run it)--but it seems to me that this will equate to a Resizing event and not a ResizeEnd event.
-
brovar over 2 yearsI don't even remember what the question was actually about, but wow! Thanks for answering!