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...
Asked
Active
Viewed 2.3k times
13
-
There are better ways to do control layout. – leppie Jun 21 '10 at 08:44
-
I know and I wouldn't do this that way, but it's a little special case ;) – brovar Jun 21 '10 at 08:47
-
Anything wrong with the `Control.Resize` event? – leppie Jun 21 '10 at 08:50
-
3@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. – djdd87 Jun 21 '10 at 08:54
4 Answers
15
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.
djdd87
- 65,256
- 25
- 153
- 194
-
Very 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. – brovar Jun 21 '10 at 09:08
-
@brovar: That's baloney! Every control has a `ParentForm` property. So in fact you have all that accessible! – leppie Jun 21 '10 at 09:09
-
@Why can you not just add a `ResizeMeNow()` method to the user control and call it on the Form's `ResizeEnd` event? – djdd87 Jun 21 '10 at 09:10
-
@brovar - or like leppie has said, you can just hook into UserControl.ParentForm.ResizeEnd+= ... etc. – djdd87 Jun 21 '10 at 09:11
-
Yes, 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 Jun 21 '10 at 09:19
-
Hmm... but it seems that ResizeEnd event is not firing in case when form is maximized. (Layout event is fired, but it fires before contained controls are resized) – Prokurors May 14 '14 at 16:59
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.
Wolf5
- 15,580
- 11
- 58
- 58
-
I'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. – Jazimov Jan 19 '20 at 17:52
1
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
}
}
Darrel K.
- 1,417
- 16
- 26
-
1I don't even remember what the question was actually about, but wow! Thanks for answering! – brovar Oct 01 '21 at 14:12
0
Maybe you can use the SizeChanged Event. But i don´t know how often or when it´s called during resizing.
Jehof
- 33,666
- 10
- 120
- 151