Setting Combo box text property

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I would like the text property of combo box to read the first item of the
list on form load. I have a set of four items in the collection.
How do I do it?
Thanks
 
Is there some other property I should set. Setting Selected Index does not
help. It simply reads the text property I set during design time.
 
Nope.

The code below selects the first item on the combobox's Items collection:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
ComboBox1.SelectedIndex = 0
End Sub
 
Can you show the code you are using to "read" the selected item?

you need to use combobox1.selecteditem and then read your string from there.

chris
 
ComboBox1.Text = ComboBox1.Items(0).ToString
cboDPI.SelectedIndex = 0

Both these statements don't work. Any help?
 
Um, This is what I think you are trying to do. You have a combobox called
cboDPI. You want the text in the box to display the first item in the list.
You are populating the list with strings. If this is what you are trying to
do, then this is the code you need in the form load event.

cboDPI.Items.Add("audi")
cboDPI.Items.Add("buick")
cboDPI.Items.Add("chevy")
cboDPI.Items.Add("ford")
cboDPI.SelectedIndex = 0

now audi will show up as the selecteditem. If you want to read that item
later in code do:

MessageBox.Show(CStr(cboDPI.SelectedItem))

Good Luck
Chris
 
VB User said:
I would like the text property of combo box to read the first item of the
list on form load. I have a set of four items in the collection.

Set the control's 'SelectedIndex' property to 0.

If you don't want the user to be able to type text into the combobox (which
is true for most cases), set the combobox's 'DropDownStyle' property to
'DropDownList'.
 
Thanks everyone for the info, it works.

Herfried K. Wagner said:
Set the control's 'SelectedIndex' property to 0.

If you don't want the user to be able to type text into the combobox (which
is true for most cases), set the combobox's 'DropDownStyle' property to
'DropDownList'.
 

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

Back
Top