2015-10-19 97 views
0
更改按鈕屬性

我想問如何從類中更改主窗體中的按鈕屬性。 我與button called Call形式。此按鈕是已禁用。 我有正在檢查網絡連接的課。我的課程何時連接我希望啓用按鈕從課程網絡呼叫,但我無法更改此課程的屬性。在主窗體中從類

代碼

public class network 
{ 
... 
MainForm frm = new MainForm(); 
.. 
. 
. 
    if(isNetwork==true) 
    { 
    frm.Call.Enabled = true; //nothing happends, button is still disabled 
    methodUpdate(); // second solution with calling method, but it 
         doesn't work too ... 
} 
    } 

的MainForm

...

public void methodUpdate() 
{ 
Call.Enabled = true; 
} 

,但是當我在主要形式和方法,單擊我有methodUpdate另一個按鈕()所有運作良好;

button click method in MainForm 
private void button1_Click(object sender, EventArgs e) 
     { 
      methodUpdate(); //this is working well 

     } 
+0

你調試你的代碼?這聽起來好像它沒有進入你的if block ......你能證實它是什麼嗎? –

+0

是的,正如Roryap所質疑的,你是否已經逐步完成了每行代碼?我也懷疑這不會觸及你的'if'陳述。 – Sean

+0

您正在創建一個新表單。那是實際顯示的那個嗎?你是否從UI線程進行修改?如果是這樣,你可能會阻止UI線程,從而阻止它改變按鈕狀態。如果不是,當您嘗試從其他線程修改UI控件時,您會遇到異常。 – Luaan

回答

0

您是否試圖通過構造函數將MainForm放入Network類?

在您的網絡類:

public class Network 
{ 
    private MainForm _frm; 
    public Network(MainForm frm) 
    { 
    _frm = frm; 
    } 
    . 
    . 
    . 
    if(isNetwork == true) 
    { 
     _frm.Call.Enabled = true; 
    } 
} 

當實例在MainForm中的網絡對象調用類似的東西:

public Network myNetwork = new Network(this);