combobox C#, Selectedtext vs Selecteditem.Tostring()

Asked

Viewed 42 times

0

What’s the difference between the two? Can values be different if an error occurs because of this? Which takes up more memory?

Apparently the two return the same value, but I would like to remove this doubt to avoid future "surprises"

2 answers

0


The first quoted, ComboBox.SelectedItem returns a Object text if you have a selected item there, or null if there is no item selected in the control.

Already the ComboBox.SelectedText will return a String with the selected item text. If null, it will be empty (""), and if the property DropDownStyle for DropDownList, it will always return as empty.

In this case, the SelectedText is useful for when the control is in written text mode, and when it is list, it is convenient to use the SelectedItem.

0

The Selectedtext property returns or changes the selected text in the combobox. Its Return type is a string. The Selecteditem property returns or changes the Combobox Item. Its return type is an Object. That’s why you need to use the toString() method to display the selected text in Selecteditem. The purpose of Selecteditem is to allow you to store complex objects in the combobox and to override the toString method of this object to display any other type of value representative of the item selected in the combobox.

https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.combobox?view=net-5.0

Browser other questions tagged

You are not signed in. Login or sign up in order to post.