Home c# How to choose a value in ComboBox to get it text?

How to choose a value in ComboBox to get it text?

Author

Date

Category

How to choose a value in ComboBox to get its text? SelectedValue, selectedItem does not help. And how to add multiple values ​​in ComboBox without adding the same?


Answer 1, Authority 100%

// *** Getting the value when choosing in ComboBox
  // Connect an event handler - if the value is needed at the time of choice
  // Event handler when selecting a value from the list
  Private Void ComboBoxInfo_SelectedIndExchanged (Object Sender, Eventargs E)
  {
    // First method
    MessageBoxInfo.SelectedItem.Tostring ());
    // Second Method
    MessageBox.Show (ComboBoxInfo.selectedIndex] .tostring ());
  }
    // *** Adding a group of values ​​without repetition
    // Array of new values
    String [] NewValue = {
                "New value 1",
                "New value 2",
                "New value 3",
                "New value 4"
              };
    // Watch the array in the cycle
    Foreach (String Val in Newvalue)
    {
      // Check the availability in the list and if not - add
      if (comboboxinfo.findstring (VAL) == -1) ComboBoxInfo.Items.add (VAL);
    }

In WPF – almost also, only an event handler will be called a little differently and checking the element in the list will not be findstring, but Items.Indexof (VAL). For WPF – it will be like this:

private void comboboxinfo_selectionchanged (Object Sender, SelectionChangeDEVENTARGS E)
  {
    // First method
    MessageBoxInfo.SelectedItem.Tostring ());
    // Second Method
    MessageBox.Show (ComboBoxInfo.selectedIndex] .tostring ());
    // Array of new values
    String [] NewValue = {
              "New value 1",
              "New value 2",
              "New value 3",
              "New value 4"
            };
    // Watch the array in the cycle
    Foreach (String Val in Newvalue)
    {
      // Check the availability in the list and if not - add
      if (ComboBoxInfo.Items.Indexof (VAL) == -1) ComboBoxInfo.Items.add (VAL);
    }
  }

Answer 2

combobox1.selectitem.content

Answer 3

You can use combobox4.displaymember.tostring () Then you can get the element displayed in the box.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions