Windows Develop Bookmark and Share   
 index > Windows Forms General > How to set a default value for a Combo Box?
 

How to set a default value for a Combo Box?

I’m creating a Combobox dynamicly at run time, however, I don’tâ€?nbsp;know how to set the default selected value for it!!? Thanks.



Dim MyComboBox As New ComboBox
MyComboBox.Name = â€œMyComboBoxâ€?br/>MyComboBox.DataSource = MyDefaultView
MyComboBox.DisplayMember = "ItemList"
MyComboBox.ValueMember = "ValueList"

----This line has NO effect on MyComboBox!!! ----
MyComboBox.SelectedValue = 6

MyComboBox.Location = New Point(80, 60)
Me.Controls.Add(MyComboBox)


MigrationUser 1  Thursday, June 16, 2005 5:25 PM
Hi.
It looks to me like you want to say MyCombox.SelectedIndex, not SelectedValue?
MigrationUser 1  Thursday, June 16, 2005 11:48 PM
Just use 

MyComboBox.SelectedIndex=XXXX
MigrationUser 1  Friday, June 17, 2005 1:06 AM
Use:

MyComboBox.SelectedIndex = 6;
MigrationUser 1  Friday, June 17, 2005 1:29 AM
When setting SelectedValue, the value you assign must be in the ValueMember list.  If you want to select an item by index, use SelectedIndex.  I'm not sure from your code which you are trying to do but, if the ValueList column of MyDefaultView does not contain a 6 then your code will have no effect.
MigrationUser 1  Friday, June 17, 2005 5:36 AM
is the actual value of the item you are trying to set 6? or is that the index of it? If you know the order that the items are going into the comboBox, you can set MyComboBox.SelectedIndex = 6

(Note: Remember to start counting from 0, not( at least in c# )) 
MigrationUser 1  Friday, June 17, 2005 5:47 AM
is the actual value of the item you are trying to set 6? or is that the index of it? If you know the order that the items are going into the comboBox, you can set MyComboBox.SelectedIndex = 6

(Note: Remember to start counting from 0, not 1( at least in c# )) 
MigrationUser 1  Friday, June 17, 2005 5:48 AM
I don't know the number of items or index for that matter, however, I know the actual value which in this case is "6". When I set the index to a number other than -1 I get an error! Any idea? Thanks.
MigrationUser 1  Friday, June 17, 2005 12:02 PM
Ok, I got it. The trick is to add the selectedValue after adding the control to the Form!!!. Here is the code:


Dim MyComboBox As New ComboBox

MyComboBox.Name = â€œMyComboBoxâ€?br/>MyComboBox.DataSource = MyDefaultView
MyComboBox.DisplayMember = "ItemList"
MyComboBox.ValueMember = "ValueList"
MyComboBox.Location = New Point(80, 60)

Me.Controls.Add(MyComboBox)
MyComboBox.SelectedValue = 6

MigrationUser 1  Friday, June 17, 2005 12:13 PM

Here is work also (if you have 6 items, the last one is number 5)

MyComboBox.SelectedIndex= 5

Karokpa  Sunday, October 14, 2007 12:59 PM

You can use google to search for other answers

Custom Search

More Threads

• C# Forms: Different text size of label and DrawString
• ListView Not accepting scroll messages
• Simple Toobar question ? perhaps not....
• Multiple Forms each with its own ToolStrip
• forcing an event
• how to print my form
• An unhandled exception of type 'System.NullReferenceException'
• Testing scalar query for NULL
• backcolor=transparent
• ¿DataGrid Column Resize? (solved, check it out for sourceCode)