Windows Develop Bookmark and Share   
 index > Windows Forms General > Setting Combobox default item at Design time
 

Setting Combobox default item at Design time

Hello,

I added a combobox to my forms app, and set the Dropdown style as "DropDownList", so the user can't do anything but select a different item.

I know I can set a default item within the form constructor with the SelectedIndex, SelectedItem, etc properties. But I rather set the default to an item with "Select One" at design time, then I don't need to add a line of code every time I insert a ComboBox. So, any sugestion?

I tried using the Text property but it gets erased when I press enter after entering my "default text".

Thanks,
John

PS: My comboboxes aren't bound to anything their own collections.
milasch  Tuesday, February 03, 2009 8:08 PM
Set the Text property to the text of the item you want to pre-select.
Hans Passant.
nobugz  Wednesday, February 04, 2009 2:35 AM
Use this property. (Searced withthe Object Browser Window....)

publicoverrideintSelectedIndex{set;get;}
MemberofSystem.Windows.Forms.ComboBox
Summary:
Getsorsetstheindexspecifyingthecurrentlyselecteditem.
Returns:
Azero-basedindexofthecurrentlyselecteditem.
Avalueofnegativeone(-1)isreturnedifnoitemisselected.

Add that to a control that inherits from ComboBox, then use that custom control instead.


Mark the best replies as answers. "Fooling computers since 1971."
  • Edited byRudedog2 Tuesday, February 03, 2009 8:30 PMdfadf
  •  
Rudedog2  Tuesday, February 03, 2009 8:29 PM
Set the Text property to the text of the item you want to pre-select.
Hans Passant.
nobugz  Wednesday, February 04, 2009 2:35 AM

Hi milasch,

Yes, you are right, the Text property cannot be set after your set the DropDownStyle to DropDownList. Also there is no property you can set in property window. If you really want to do so, you need to create a custom ComboBox inherited from the system one and make a property called "DefaultIndex". That won't as easy as set its SelectedIndex in the code.

Hint: ComboBox always select the first item when it is bound to a list datasource.

Sincerely,
Kira Qian


Please mark the replies as answers if they help and unmark if they don't.
Kira Qian  Thursday, February 05, 2009 3:48 AM
Nah, Text works fine. Try it.
Hans Passant.
nobugz  Thursday, February 05, 2009 11:06 AM
There is a slight issue though, in the VS 2008 that we have.
If we try to set the Text property then it is not taken.

In this case, we will have to set SelectedIndex in the InitializeComponent to whichever index we want. Then, the value is correctly reflected in the Text property, as also in the Form design.
vsha  Thursday, September 17, 2009 3:57 PM

You can use google to search for other answers

Custom Search

More Threads

• Change Textbox size dynamically
• Shared Assemblies - applications running across network
• Checking language at runtime
• Switching display code
• DataGridViewCell Properties Related problems
• DataGrid.AllowSort=false doesn't work
• How to Get ListView in VirtualMode to Autoscroll?
• Unable to set the listview size properly
• Need to pull mobile device hardware data
• C# (2005) Disable Horizontal Scrollbar In ListView?