So I have a WinForms project, which contains a Form, which then contains a ComboBox-element.
That ComboBox-element is set to "Style: DropDownList", meaning that the displayed text inside the ComboBox can only be an item from its list OR null
.
So if I want to change the currently selected item, I realized I can use both
ComboBox.SelectedItem = "example"
but also
ComboBox.Text = "example"
Is there any difference between those two? (Keep in mind that the ComboBox is a DropDownList!)
Is there any difference between those two? (Keep in mind that the ComboBox is a DropDownList!)
For this particular ComboBox.DropDownStyle
, the anser depends on the type of the objects used to populate the combo box Items
collection. If the combo box is populated with string
s, there is no difference, if other type of object is used, they will always differ. For instance, if you populate it with some class objects, then SelectedItem
will be an instance of that class, while Text
will be either obtained from ToString()
method of that class, or some property of the class if specified by DisplayMember property.
In general, these properties have a different meaning, so use the one which is appropriate for what you are trying to achieve.
The Text
property is used when the combo allows text to be entered. SelectedItem
refers to an item in the ComboBox list of Items. If you allow text entry then it is possible SelectedItem
does not refer to the current Text
value, the inverse is true also, the Text
value doesn't mean there is an item in the source items collection.
If your ComboBox doesn't allow free form text to be entered then you only need to use the SelectedItem
property.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.