Posted by: Xiaonan Ji | April 16, 2010

The difference between SelectedText and SelectedItem in ComboBox control

In ComboBox there are two properties, SelectedText and SelectedItem. SelectedItem property returns the currently selected object of the ComboBox. It can be any object. Use the ToString() method to get the string of the selected item if necessary. The SelectedText returns the editable text from the ComboBox. This property is only relavent when you set the ComboBox’s DropDownStyle to either Simple or DropDown. In these two cases, you will find that you can enter arbitrary text to the ComboBox. Then you can use SelectedText to return at any time the text in the ComboBox. If you set the ComboBox’s DropDownStyle to DropDownList, you will notice that you cannot enter text of your own. Instead, you can only select existing items from the list. In this case, at any time, SelectedText returns an empty string.

SelectedText will be useful when you want to implement your own style of suggestion list of the ComboBox. You will need to combine the usage of this property with the KeyPress event of the ComboBox.

Until next time, happy coding!

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Categories

Follow

Get every new post delivered to your Inbox.