468,248 Members | 1,513 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,248 developers. It's quick & easy.

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

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
Nov 21 '05 #1
5 25494
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/de...boxcontrol.asp

"James P." <ha*********@yahoo.com> wrote in message
news:f4**************************@posting.google.c om...
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

Nov 21 '05 #2
"Russell Jones" <ar**@nospam.northstate.net> schrieb:
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).

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #3
"Russell Jones" <ar**@nospam.northstate.net> wrote in message news:<eT**************@TK2MSFTNGP14.phx.gbl>...
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/de...boxcontrol.asp

"James P." <ha*********@yahoo.com> wrote in message
news:f4**************************@posting.google.c om...
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


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
Nov 21 '05 #4
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

"James P." <ha*********@yahoo.com> wrote in message
news:f4**************************@posting.google.c om...
"Russell Jones" <ar**@nospam.northstate.net> wrote in message
news:<eT**************@TK2MSFTNGP14.phx.gbl>...
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/de...boxcontrol.asp

"James P." <ha*********@yahoo.com> wrote in message
news:f4**************************@posting.google.c om...
> 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


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

Nov 21 '05 #5
"Chris" <chris@No_Spam_Please.com> wrote in message news:<ef**************@TK2MSFTNGP10.phx.gbl>...
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

"James P." <ha*********@yahoo.com> wrote in message
news:f4**************************@posting.google.c om...
"Russell Jones" <ar**@nospam.northstate.net> wrote in message
news:<eT**************@TK2MSFTNGP14.phx.gbl>...
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/de...boxcontrol.asp

"James P." <ha*********@yahoo.com> wrote in message
news:f4**************************@posting.google.c om...
> 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


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


Thank you so much, guys. It worked now.

James
Nov 21 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Gamze | last post: by
reply views Thread by Doug | last post: by
3 posts views Thread by Simon Tamman | last post: by
8 posts views Thread by =?Utf-8?B?RyBIdXN0aXM=?= | last post: by
3 posts views Thread by Gerrit | last post: by
1 post views Thread by Andrus | last post: by
2 posts views Thread by Andrus | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by kermitthefrogpy | last post: by
reply views Thread by zattat | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.