2015-09-18 34 views
0

我正在使用Winform。我有Resize事件。當我打電話給事件時,它會一次又一次地調用自己。以下是我的代碼。如何在Winform的Resizing事件中停止循環?

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; 如何停止通話的循環一次次給自己?

回答

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#