0

I am using Winform. I have Resize event. When I call the event, it call itself again and again. Following is my code.

private void Form1_Resize(object sender, EventArgs e)
{
     int tabHeight = 100;
     this.Height = tabHeight + 20;
     this.Top = (Screen.PrimaryScreen.Bounds.Height / 2) - (this.Height / 2);
}

Event call itself again and again from this.Height = tabHeight + 20; How can I stop looping of call?

NJ Bhanushali
  • 901
  • 1
  • 12
  • 21

1 Answers1

0

Here a possible solution:

bool _resizing;

private void Form_Resize(object sender, EventArgs e)
{
    if (_resizing) return;
    _resizing = true;

    int tabHeight = 100;
    Height = tabHeight + 20;
    Top = (Screen.PrimaryScreen.Bounds.Height / 2) - (Height / 2);

    _resizing = false;
}

But there are some better ways to resize the window in only one direction: Vertically (only) resizable windows form in C#

Community
  • 1
  • 1
Jotrius
  • 647
  • 4
  • 19