Initialize value in comboBox in C #

I understand that my comboBox is always empty when the program starts. I have to click on the arrow next to select a value. How to do this so that comboBox shows the value when the program starts?

+4
source share
4 answers

There are 4 properties that you can set:

// Gets or sets the index specifying the currently selected item. comboBox1.SelectedIndex = someIndex; //int // Gets or sets currently selected item in the ComboBox. comboBox1.SelectedItem = someItem; // object // Gets or sets the text that is selected in the editable portion of a ComboBox. comboBox1.SelectedText = someItemText; // string // Gets or sets the value of the member property specified by the ValueMember property. comboBox1.SelectedValue = someValue; // object 

Comment lines directly from MSDN: http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.aspx

+6
source

If you have a combo box and want to set its data source, you can do it as follows:

  string[] items = new string[]{"Ram","Shyam"}; comboBox1.DataSource = items; comboBox1.SelectedIndex = 0; 

So try setting SelectedIndex to the first index.

+2
source

Try this code:

 comboBox1.Items.Add("Test"); 
0
source

Davenewza is responsible for the programmatic approach (which I highly recommend). Another way that is less elegant but uses the property toolbar is as follows:

  • In the project view, click the appropriate comboBox.
  • Go to Appearance-> Text and enter any desired line.

To be safe, I would enter a value that matches the one that will be selected in the field to prevent unwanted lines from spreading to other functions / variables. This reason can cause a lot of headaches if they are not handled carefully, so a programmatic approach is preferred.

0
source

All Articles