• 0

how to use combobox in vb.net 2010 ?


Question

Hi to all

I have something that doesn't seem to work in vb 2010.

I have set up a combobox on a form. The user can select an item from the drop-down list. That part works.

I want to get the selection text into a variable so that it can be referenced globally. That does not seem to work. Maybe I am using the wrong variable type in the variable definition ( I thought String would cover it. ) .

The correct item can be seen and referred to as ComboBox.SelectedItem but this will not go into a variable. Why not ? Does anyone know ?

I have tried;

Dim this As String

this = Combobox1.SelectedItem

MsgBox(this)

but it comes up with nothing for 'this' although MsgBox(Combobox1.SelectedItem) will correctly display what it should be.

Does anyone know what I am doing wrong ?

Thanx

Link to comment
Share on other sites

6 answers to this question

Recommended Posts

  • 0

The SelectedItem property is actually the object (for binding). You may want to try SelectedItem.ToString() or cast it to the appropriate type and then get the property/value out of the object. Also the reason why it isn't giving you an error is because option strict is off so the compiler is trying to make the change for you, but failing.

Link to comment
Share on other sites

  • 0

The SelectedItem property is actually the object (for binding). You may want to try SelectedItem.ToString() or cast it to the appropriate type and then get the property/value out of the object. Also the reason why it isn't giving you an error is because option strict is off so the compiler is trying to make the change for you, but failing.

Thak you SpartyJohnson

We have already tried the ToString method and it did not work. With regards to casting, what is the appropriate type ? We have already tried Dim this As Combobox.SelectedItem and Dim this As Combobox

We have also tried all sorts of variable types. I know it is an object but I don't know what type to call it.

And yes you were right, the compiler does not spot the error even though it can usually spot type mismatches.

thanx

Link to comment
Share on other sites

  • 0

You want the SelectedText property.

Thanx Omnicoder but we already tried that. It comes up with nothing. We have seen that the SelectedItem property holds what we want, but we cannot push that into a variable. All I can think of is that the variable definition is of the wrong type to hold that data, although the data should be a name ie a string.

Link to comment
Share on other sites

  • 0

Here is some code that I used to test combo boxes

Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged

Dim TheItem As String = ""

TheItem = ComboBox1.SelectedItem.ToString

Me.Text = TheItem

End Sub

Yes I could have made a label but I just used the form caption

Hope this helps and good luck

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.