What is wrong with the ComboBox !!

Hi all,

I am writting very simple event for the ComboBox... but my msgbox displays nothing. kindly have alook at my code.

Public Class Form1

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

If Me.ComboBox1.SelectedIndex <> -1 Then

'MsgBox(Me.ComboBox1.Text.ToString)

MsgBox(Me.ComboBox1.SelectedText.ToString)

End If

End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Dim conn As New Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Documents and Settings\MaHMouD\Desktop\db1.mdb;Persist Security Info=False")

Dim cmd As New OleDb.OleDbCommand("Select * from t1", conn)

Dim da As New OleDb.OleDbDataAdapter(cmd)

Dim dt As New DataTable

da.Fill(dt)

Me.ComboBox1.DataSource = dt

Me.ComboBox1.ValueMember = "id"

Me.ComboBox1.DisplayMember = "sname"

AddHandler ComboBox1.SelectedIndexChanged, AddressOf ComboBox1_SelectedIndexChanged

End Sub

End Class




Answer this question

What is wrong with the ComboBox !!

  • Kees_de_Waard

    thanks alot, it took alot of time and workarounds until we solved such a small problem

    thanks anyway my dear.

    Yours,

    have a look at the code finally.

    Public Class Form1

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

    If Me.ComboBox1.SelectedIndex <> -1 Then

    MessageBox.Show("you have chosen item " & Me.ComboBox1.Text & " and its value is " & Me.ComboBox1.SelectedValue.ToString())

    End If

    End Sub

    Private Sub LoadCBitems()

    'Please change the path of the Db in the next line.

    Dim conn As New Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Documents and Settings\MaHMouD\Desktop\db1.mdb;Persist Security Info=False")

    Dim cmd As New OleDb.OleDbCommand("Select * from t1", conn)

    Dim da As New OleDb.OleDbDataAdapter(cmd)

    Dim dt As New DataTable

    da.Fill(dt)

    Me.ComboBox1.DataSource = Nothing

    Me.ComboBox1.Items.Clear()

    Me.ComboBox1.DataSource = dt.DefaultView

    Me.ComboBox1.ValueMember = "id"

    Me.ComboBox1.DisplayMember = "sname"

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    RemoveHandler ComboBox1.SelectedIndexChanged, AddressOf ComboBox1_SelectedIndexChanged

    LoadCBitems()

    AddHandler ComboBox1.SelectedIndexChanged, AddressOf ComboBox1_SelectedIndexChanged

    End Sub

    End Class



  • Alex_b

    well, you can check to see if an item has been selected checking to see if there is text in the textbox. As I suggested earlier, the reason for the message boxes appearing in the selectedIndexChanged event for the combobox

    One way of doing this is to remove the eventhandler, then readd it - but this is not entirely recommended. It works however:

    RemoveHandler ComboBox1.SelectedIndexChanged, AddressOf ComboBox1_SelectedIndexChanged

    Me.ComboBox1.DataSource = dt.DefaultView

    Me.ComboBox1.DisplayMember = "sname"

    Me.ComboBox1.ValueMember = "id"

    AddHandler ComboBox1.SelectedIndexChanged, AddressOf ComboBox1_SelectedIndexChanged



  • DavidThi808

    ok thats much better with explanation. This is easily done and I believe to have made it work.

    in order to get its value, you need to use the SelectedValue property in the combobox. Example:

    MessageBox.Show("you have chosen item " & Me.comboBox1.Text & " and its value is " & Me.comboBox1.SelectedValue.ToString())

    The selectedValue property automatically gets the appropriate value, when you set the valuemember property of the control

     

    The reason for the multiple Message Boxes is because:

  • you set a datasource to the combobox

  • you set the valuemember property

  • you then set the displaymember property, which sets the first item chosen in the combobox

     

    does this work for you



  • willajo

    Private Sub LoadCBitems()
    Dim dtTable As New DataTable("TEST")
    Dim dtCol1 As New DataColumn("Col1")
    dtTable.Columns.Add(dtCol1)
    Dim dtCol2 As New DataColumn("Col2")
    dtTable.Columns.Add(dtCol2)

    For index As Integer = 0 To 10
    Dim dtRow As DataRow
    dtRow = dtTable.NewRow
    dtRow.Item("Col1") = index.ToString
    dtRow.Item("Col2") = index.ToString
    dtTable.Rows.Add(dtRow)
    Next

    Me.ComboBox1.DataSource = dtTable
    Me.ComboBox1.ValueMember = "Col1"
    Me.ComboBox1.DisplayMember = "Col2"
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    LoadCBitems()
    End Sub

    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
    'If ComboBox1.SelectedIndex = 0 Then Exit Sub

    If Not ComboBox1.SelectedValue.GetType Is GetType(DataRowView) Then
    MessageBox.Show("you have chosen item " & Me.ComboBox1.Text & " and its value is " & Me.ComboBox1.SelectedValue.ToString())
    End If
    End Sub



  • dnweb

    but i am still getting 3 msgboxes. this is not good. i only want to get one msgbox. is there any idea were i can get rid of the first 2 useless msgboxes thanks again.

  • Alvedon

    please send me by email (email in profile, click my name) the entire project including the database and tell me (and everyone here) what exactly you are having a problem with.



  • Omega_C&amp;#35;

    thanks alot for ur fast reply. :)

    :( , unfortunatly it doesnt work!!!

    I have added a button to my form and moved all the code that databindes the DB to my ComboBox to a separate sub and this still displays 3 msgboxes.

    have alook again at my code, and i dont know if i have chosen the right event to write the code of combobox inside of it.

    Public Class Form1

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

    If Me.ComboBox1.SelectedIndex <> -1 Then

    MsgBox(Me.ComboBox1.Text)

    End If

    End Sub

    Private Sub LoadCBitems()

    Dim conn As New Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Documents and Settings\MaHMouD\Desktop\db1.mdb;Persist Security Info=False")

    Dim cmd As New OleDb.OleDbCommand("Select * from t1", conn)

    Dim da As New OleDb.OleDbDataAdapter(cmd)

    Dim dt As New DataTable

    da.Fill(dt)

    Me.ComboBox1.Items.Clear()

    Me.ComboBox1.DataSource = dt.DefaultView

    Me.ComboBox1.ValueMember = "id"

    Me.ComboBox1.DisplayMember = "sname"

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    LoadCBitems()

    End Sub

    End Class



  • 4B7

    Hi ahmed,

    Thanks alot for ur fast reply.

    I tired to do what u've suggested. but when i run the code first it displays a msgbox with "System.Data.DataRowView" then another msgbox with "1" inside of it then the first item in the combo box, then it loads the form. i just want an explanation for the 2 msgboxes i get before the first item in the combobox is displayed in the msgbox, also what if I want to get the Value of the selected item. here is my code after the modifications u have suggested.

    Public Class Form1

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

    If Me.ComboBox1.SelectedIndex <> -1 Then

    MsgBox(Me.ComboBox1.Text.ToString)

    End If

    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    Dim conn As New Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Documents and Settings\MaHMouD\Desktop\db1.mdb;Persist Security Info=False")

    Dim cmd As New OleDb.OleDbCommand("Select * from t1", conn)

    Dim da As New OleDb.OleDbDataAdapter(cmd)

    Dim dt As New DataTable

    da.Fill(dt)

    Me.ComboBox1.DataSource = dt

    Me.ComboBox1.ValueMember = "id"

    Me.ComboBox1.DisplayMember = "sname"

    End Sub

    End Class



  • champa

    I believe the reason for the msgbox's to show is because when you databind the datatable to the combobox, the selectedindex gets changed. I'm not sure why it would show it twice however but can understand showing it once. It could be the fact that it's binding it twice, meaning, the first time you are setting the datasource and the second time the displaymember, but I could be wrong.

    On the form load you are connecting to the database and then databinding the combobox to the dataset, this is not great as it may take some time (it can happen especially on remote connections for example) to get the data and the user would be wondering what's going on. However, let's not get into this and stick to the original problem.

     

    you should also try doing:

    Me.ComboBox.DataSource = dt.DefaultView

    and see what happens - it will show the defaultview of the items in the combobox.

    To get the value of the selected item, it's just as I had suggested and what's in your code right now. This will get the item chosen from the combobox.

     



  • Chris Pyman

    try doing this:

    MessageBox.Show(Me.theComboBox.Text)

    you also shouldnt need to addHandler of the combobox yourself, just double click it to make the event in the designer view and remove the addHandler portion after this in the Form_Load event



  • Cyber_Wolf

    thanks alot Andre Aung

    that works fine with me



  • SaucerBoy

    thanks ahmed, I appreciate ur patience.

    here is the project and the database (http://www.wegraphics.com/dotNet/WindowsApplication4.rar)

    i have uploaded it to my website so all the viewers can help. :)

    my problem is that i want to fill the combo box with a group of items from my database, and when the user selects an item from the combobox, I want to display a msgbox that says " hi, you have chosen item <blab la blab> and its values is <123> "

    thats all.

    Thanks alot.



  • What is wrong with the ComboBox !!