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

How to stop looping in Resizing event of Winform?

我正在使用 Winform。我有调整大小事件。当我调用该事件时,它会一次又一次地调用自身。以下是我的代码。

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; 一次又一次地调用自身 我怎样才能停止通话循环?

这里有一个可能的解决方案:

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

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