2012-04-13 59 views
1

在我的主要形式,我運行此:方法不工作

this.disableForm(); 
btnAbort.Enabled = true; 

disableForm是在我的節目形態定義爲以下的擴展方法:

public static void disableForm(this Form f) 
{ 
    foreach (Control c in f.Controls) 
    { 
     f.Enabled = false; 
    } 

    f.Cursor = Cursors.WaitCursor; 
} 

的問題是,下一個命令btnAbort.Enabled = true;不會做任何事情。

它的工作原理是如果我把代碼直接放在方法中而不是調用disableForm()。這是爲什麼發生?它與線程有關嗎?

+0

你不能從一個非UI線程修改UI元素,如果這就是你在問什麼 – 2012-04-13 08:56:11

+0

凡在我的問題我提到這不是一個UI線程? – TheGateKeeper 2012-04-13 08:58:30

+0

然後,您應該更徹底地瞭解「這與線程有關」。 – 2012-04-13 08:59:07

回答

7

這條線:

f.Enabled = false; 

應該

c.Enabled = false; 

的問題是因爲你不小心禁用你的整個形式。

+0

哦,夥計,我怎麼錯過了! – TheGateKeeper 2012-04-13 09:02:28

+0

S l v n a l t c c。 – 2012-04-13 09:02:57

+0

@MartinJames ??? – 2012-04-13 09:03:51

2
public static void disableForm(this Form f) 
    { 
     foreach (Control c in f.Controls) 
     { 
      //f.Enabled = false; 
       c.Enabled = false; 
     } 

     f.Cursor = Cursors.WaitCursor; 
    } 
+0

爲什麼這個答案被拒絕投票... – Sadaf 2012-04-13 09:22:54