Change the ComboBox's text field when an item is selected from the drop down list?

前端 未结 2 1164
终归单人心
终归单人心 2021-01-20 14:23

I have a ComboBox on a form. The DropDownStyle property of the ComboBox is set to DropDown, so that the user can select an item from the drop down

相关标签:
2条回答
  • 2021-01-20 14:46

    You can try this:

    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
      if (comboBox1.SelectedIndex > -1)
      {
        string value = comboBox1.Items[comboBox1.SelectedIndex].ToString().Substring(4);
        this.BeginInvoke((MethodInvoker)delegate { this.comboBox1.Text = value; });
      }
    }
    
    0 讨论(0)
  • 2021-01-20 14:53

    Just to clarify, are you displaing "101 Cat", "102 Dog", etc. when the user dropdown the combo and displaying them when selected? Is 101 the key for the "Cat", 102 the key for the "Dog", and so on? If so, why are you displaying them and not only displaying the text of each item (if this is your requirement, apologize my answer). For what I understand, I'll configure the combobox valuemember to the property that returns you the 101, 102, etc. and the displaymember to the property that returns you the text of each items. This way, you'll already get what you want, i.e. displaying the "Cat", "Dog" and "Bird" text. Also, you can attach an event to the TextChanged event of the combo if you want furter processing.

    0 讨论(0)
提交回复
热议问题