如何在Winform的调整大小事件中停止循环

本文关键字:小事件 循环 大小事 调整 Winform | 更新日期: 2023-09-27 18:13:34

我使用的是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;多次调用自身我怎样才能停止循环调用?

如何在Winform的调整大小事件中停止循环

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

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

但是有一些更好的方法可以在一个方向上调整窗口的大小:c#中垂直(仅)可调整大小的窗体