2014-01-08 65 views
0

我是新來的C#和我創造了這個線程類似於人的用戶控件:如何訪問下拉選定的索引更改?

add user control to a form

而已,我用4個下拉列表。我創建了一個名爲CustomBaseUserControl.cs的自定義用戶控件。它具有爲每個下拉菜單選擇的所有索引更改事件。從表單中,將其稱爲TheFormControl,將CustomBaseUserControl放入,如何訪問這些事件更改值?

在此先感謝!

回答

1

如果需要檢索TheFormControl選定的索引,你既可以

使用變量來存儲上CustomBaseUserControl的價值,在這種情況下,你要聽的的SelectedIndexChanged事件並更新你的價值觀。

觸發自定義選擇的指數從CustomBaseUserControl改爲TheFormControl

-

class CustomBaseUserControl: UserControl{ 
int idx1=-1; 
public CustomBaseUserControl() 
{ 
    Initialize(); 
    //Fill ComboBox 

    //Suscribe Event 
    combobox1.SelectedIndexChanged += combobox1_SelectedIndexChanged; 
} 
    void combobox1_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     int index = combobox1.SelectedIndex; 
     if (index != idx1) 
     { 
     idx1=index; 
     RaiseIndexChanged(e); 
     } 

    } 

     public virtual void RaiseIndexChanged(EventArgs ea) 
    { 
     var handler = OnIndexChanged; 
     if (OnIndexChanged != null) 
      OnIndexChanged(this, ea); 
    } 
    public event EventHandler OnIndexChanged; 
} 

主叫類將是

class TheFormControl: Form 
{ 
    CustomBaseUserControl cb; 
    public TheFormControl() 
    { 
     Initialize(); 
     cb = new CustomBaseUserControl(); 
     cb.OnIndexChanged +=cb_OnIndexChanged; 
    } 
    void cb_OnIndexChanged(object sender, EventArgs e) 
    { 
    // Here you know index has changed on CustomBaseUserControl 
    } 
} 
+0

感謝您的建議R.基哈諾但是當我把一個破發指向combobox1_SelectedIndexChanged,當用戶更改下拉列表的值時,它不會被觸發(點擊中斷點)。你有想法在我腦中搖動大理石嗎?我現在一直在撓頭。謝謝! – Allen

+0

感謝RQuijano!這正是我需要的!我已經寫了一些邏輯,但它沒有工作,但你給了我正確的方向!謝謝! – Allen

相關問題