add values to a comboBox in VB.NET in Windows application.

J

James P.

Hello,

For my new Windows application, all I want is to create an initial
form to demo to the user to show them how it looks like with some data
on it. So I figure the fastest way is to create some comboBox-es to
show some data on them manually entered in the code without connecting
to the database yet. It should be simple but somehow I can't make it
work.

First, I drag a comboBox from the ToolBox to the form. Then,
double-click the form to go to the code behind page with the Load
event, I tried either:

ComboBox1.Items.AddRange(New Object() {"red", "blue"})
OR
ComboBox1.Items.Add("red")
ComboBox1.Items.Add("blue")

Yet when I ran, it showed nothing on the comboBox, except the word
"comboBox1" (default text property). Any ideas what I am missing,
anybody?

Or if you can tell me how create a comboBox display values from a
database the quickest way.

Thanks,
James
 
R

Russell Jones

Your code loads the combo box just fine. To eliminate the "ComboBox1" text,
either set the SelectedIndex of your combo box to a valid index (e.g. 0 or 1
with your example), which will select one of the values by default, or set
the Text property to an empty string, e.g.

Me.ComboBox1.Items.AddRange(New Object() {"red", "blue"})
Me.ComboBox1.SelectedIndex = 0

or
Me.ComboBox1.Items.AddRange(New Object() {"red", "blue"})
Me.ComboBox1.Text = ""

You can find an example of binding a combobox to a database here:
http://msdn.microsoft.com/library/d...ingcomboboxcheckedlistboxorlistboxcontrol.asp
 
H

Herfried K. Wagner [MVP]

Russell Jones said:
Me.ComboBox1.Items.AddRange(New Object() {"red", "blue"})
Me.ComboBox1.SelectedIndex = 0

.... and if the user should not be able to enter text, set 'DropDownStyle' to
'DropDownList' (this will give a blank entry if 'SelectedIndex' is not set
to 0).
 
J

James P.

Russell Jones said:
Your code loads the combo box just fine. To eliminate the "ComboBox1" text,
either set the SelectedIndex of your combo box to a valid index (e.g. 0 or 1
with your example), which will select one of the values by default, or set
the Text property to an empty string, e.g.

Me.ComboBox1.Items.AddRange(New Object() {"red", "blue"})
Me.ComboBox1.SelectedIndex = 0

or
Me.ComboBox1.Items.AddRange(New Object() {"red", "blue"})
Me.ComboBox1.Text = ""

You can find an example of binding a combobox to a database here:
http://msdn.microsoft.com/library/d...ingcomboboxcheckedlistboxorlistboxcontrol.asp

Thank you both of you for responding and the link. When I tried:

Me.ComboBox1.Items.AddRange(New Object() {"red", "blue"})
Me.ComboBox1.SelectedIndex = 0

It showed only "blue" on the comboBox. If I changed SelectedIndex to
1, it only showed "red".

I know I did not post my question clear earlier. Sorry about that.
This is what I need: when the comboxBox first shown, I'd like it to
show as "Select the color". However, when the user clicks the
comboBox, it should show the choice of "blue" and "red". How do I do
that?

James
 
C

Chris

Like dis....
ComboBox1.Items.Add("audi")
ComboBox1.Items.Add("buick")

ComboBox1.Items.Add("chevy")

ComboBox1.Items.Add("ford")

ComboBox1.SelectedIndex = -1

ComboBox1.Text = "Pick a car"

Good Luck

Chris
 
J

James P.

Chris said:
Like dis....
ComboBox1.Items.Add("audi")
ComboBox1.Items.Add("buick")

ComboBox1.Items.Add("chevy")

ComboBox1.Items.Add("ford")

ComboBox1.SelectedIndex = -1

ComboBox1.Text = "Pick a car"

Good Luck

Chris

Thank you so much, guys. It worked now.

James
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top