How do I clear a combobox?

前端 未结 16 1749
醉梦人生
醉梦人生 2020-12-04 18:55

I have some combo-boxes that are set up as drop down lists, and the user can pick a number in them. I also have a Clear button that should clear the text from the combo boxe

相关标签:
16条回答
  • 2020-12-04 19:32

    Use:

    comboBox1.ResetText();
    

    and it's done.

    Docs: ComboBox.ResetText Method (Namespace: System.Windows.Forms) https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.combobox.resettext?view=netframework-4.8

    0 讨论(0)
  • 2020-12-04 19:33
    private void Resetbtn_Click(object sender, EventArgs e)
    {    
        comboBox1.Items.Clear(); // it will clear a combobox
    
        comboBox1.Items.Add("Student"); //then add combobox elements again. 
        comboBox1.Items.Add("Staff");
    }
    
    0 讨论(0)
  • 2020-12-04 19:34

    Combo Box, DropDown all are having the same logic to clear/remove all items from them and it is like below.

    //For checkbox list
    cblTest.Items.Clear();
    
    //For drop down list
    ddlTest.Items.Clear();
    
    0 讨论(0)
  • When ComboBox is not data-bound, I've found I need both: Clear() removes the items but still leaves the SelectedItem's text, while ResetText() removes that text. VS2008.

    ComboBox.Items.Clear();
    ComboBox.ResetText();
    
    0 讨论(0)
  • 2020-12-04 19:38

    If you just want to clear the current selection, but leave all of the items in the list, you can use:

    cboHour.SelectedIndex = -1
    
    0 讨论(0)
  • 2020-12-04 19:39

    This worked for me when I added ComboBox.Focus()

    ComboBox.Items.Clear();
    ComboBox.ResetText();
    ComboBox.Focus();
    
    0 讨论(0)
提交回复
热议问题