2011-06-07 100 views
0

我有一個帶有組合框的Windows窗體應用程序。我將項目添加到組合框(例如,1,2,3,4)。如果我在組合框中選擇一個項目,SelectedIndex應該返回到另一個類中的變量。將值設置爲來自另一個類的變量

class Form1 
{ 
    private void Combobox1_SelecetedIndexChanged(object sender,eventArgs e) 
    { 
     combobox1.selecetdeIndex ; //I will get the selected index. 
     //If the selected index is changed then the currentValue in the AnotherClass should be 
     //changed. 
    } 
} 

Class AnotherClass 
{ 
    private int currentValue; 
    //There are some methods which depends on currentValue. 
} 
+0

某些代碼請[從另一種形式的C#呼叫可變]的 – 2011-06-07 09:23:02

+0

可能重複(http://stackoverflow.com/questions/5928071/call-variable-from-another-form-c) – 2011-06-07 09:35:47

+0

Form1類 { 私人無效Combobox1_SelecetedIndexChanged(對象發件人,EventArgs的) { //我將得到所選索引 } } – Karthik 2011-06-07 10:01:03

回答

0

沿着這些東西線:(僞碼)

class SomeClass 
{ 

    public int comboindex 
    { 
    get; set; 
    } 

} 

SomeClass c = new SomeClass(); 

c.comboindex = mycombo.selectedindex; 
2

Windows Forms,我想最簡單的方法是處理SelectedIndexChanged事件,和事件處理程序中,設置要在那裏設置變量。

因此,事件處理程序是這樣的:得到這個在Windows窗體自動完成

private void MyComboBox_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    ComboBox comboBox = (ComboBox) sender; 
    instanceOfMyOtherClass.VariableInOtherClass = ComboBox1.SelectedItem; 
} 

沒有大的魔力。

0

不同於其它類所預訂的形式引發一個事件?

相關問題