如何在Winform的Resizing事件中停止循环?

时间:2015-09-18 12:51:02

标签: c# winforms devexpress-windows-ui

我正在使用Winform。我有Resize事件。当我打电话给这个事件时,它会一次又一次地召唤自己。以下是我的代码。

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);
}

事件从this.Height = tabHeight + 20;一次又一次地召唤自己 如何停止呼叫循环?

1 个答案:

答案 0 :(得分:0)

这是一个可能的解决方案:

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;
}

但是有一些更好的方法可以只在一个方向上调整窗口大小: Vertically (only) resizable windows form in C#