2009-10-18 34 views
0

我需要爲可以啓用/禁用各種控件的多線程程序編寫委託。對所有控件使用一個處理程序將是最佳選擇似乎是合乎邏輯的,但我甚至不確定在.net中這是可能的,如果是的話,如何實現。我可以使用.net控件父類來啓用/禁用它嗎?

+0

你可以解釋一下,爲什麼代表?這與處理程序是一樣的嗎? – 2009-10-18 17:06:21

+0

使用工作者線程時,必須調用委託來更新在另一個線程中創建的UI控件。我想創建一個委託,可以處理所有類型的UI控件,只要啓用禁用即可。 – Gio 2009-10-18 17:13:39

+0

在這種情況下,刺客93的答案似乎是現貨。 – 2009-10-18 20:34:26

回答

1
public void SetControlsEnabled(bool enabled) 
{ 
    // Make sure we're in the correct thread 
    if (InvokeRequired) 
    { 
     // If not, run the method on the UI thread 
     Invoke(new MethodInvoker(() => SetControlsEnabled(enabled))); 
     return; 
    } 

    // Put all control code here, e.g: 
    // control1.Enabled = enabled; 
    // control2.Enabled = enabled; 

    // Alternatively, do a foreach(Control c in Controls) { ... } 

} 
+0

不錯。我真正想要的是基礎類「控制」。謝謝。 – Gio 2009-10-22 05:15:22

相關問題