Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WinForms - action after resize event

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...

like image 685
brovar Avatar asked Jun 21 '10 08:06

brovar


1 Answers

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
  }
}
like image 186
Darrel K. Avatar answered Sep 21 '22 13:09

Darrel K.