2017-04-25 167 views
0

我希望我的組合框,當我按下文本框,並且下拉符號C#組合框的下拉

我已經這樣做了下拉:

private void comboBoxOpretKomponentLevel_Enter(object sender, EventArgs e) 
{   
    if (comboBoxOpretKomponentLevel.SelectedIndex <= 0) 
    { 
     comboBoxOpretKomponentLevel.Text = null; 
    } 
    comboBoxOpretKomponentLevel.Focus();  
    comboBoxOpretKomponentLevel.DroppedDown = true;   
} 

的.Droppeddown =真正使得它的工作,如果被選中的文本(「選擇產品」) 但是,當按下dropbox的下拉符號時 - 下拉菜單會再次變爲false。 我該如何做這項工作? 據我知道我不能使用DropDownList,因爲我不能有我的(「選擇產品」)文本。

+0

當我再次按下它會出錯?你的意思是關閉......?因爲不是那個預期的行爲 – EpicKip

+0

好吧,也許我沒有解釋它足夠好! 當我按下下拉符號 - 下拉菜單打開 - 但是當它進入_Enter事件時它會在執行.Dropdown = true命令時再次關閉下拉菜單。 所以結果是,它很快打開下拉列表,然後再次關閉它。 –

回答

0

我只是簡單地使用MouseClick事件。由於您的問題太廣泛,我只有這段代碼可以幫助您。

它所做的是,只有當你clickComboBox或任何控制器有關該ComboBox它會打開dropdownlist。要在事件簡單地關閉它click,它會閒置dropdownlist

private void comboBoxOpretKomponentLevel_MouseClick(object sender, MouseEventArgs e) 
    { 
     //This piece will dropdown the combobox once you click it. 
     comboBoxOpretKomponentLevel.DroppedDown = true; 
     comboBoxOpretKomponentLevel.Focus(); 
    } 
private void YourForm_Click (object sender, EventArgs e) 
    { 
     //This piece will simply close the dropdown from your combobox and use the selected value. 
     comboBoxOpretKomponentLevel.DroppedDown = false; 

    } 

希望它能幫助,否則簡單的重新制定你的問題,所以我們可以幫助你。