[英]How can I prevent a Form_activated event from looping?
我希望激活的事件只運行一次。 我嘗試使用If條件,但Reload變量未設置為false,因此不斷循環。 有沒有解決的辦法?
Form1.cs代碼:
private void Form1_Activated(object sender, EventArgs e)
{
if (Class1.Reload == true) {
Class1.Reload = false;
}
}
Class1.cs代碼:
public class Class1 {
public static void Refresh() { Reload = true; }
public static bool Reload { get; set; }
只需在第一次觸發事件時取消訂閱該事件即可。
private void Form1_Activated(object sender, EventArgs e)
{
this.Activated -= Form1_Activated;
// Do other stuff here.
}
當CathalMF的解決方案有效時,我將發布實現的解決方案,該解決方案的目的是在回到主窗體時刷新DatagridView 。
private void Form1_Activated(object sender, EventArgs e) {
if (Class1.Reload == true) {
Activated -= Form1_Activated;
Class1.Reload = false;
//Here I implement the code to refresh a DatagridView
Activated += Form1_Activated;
}
}
Class1.cs保持不變。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.