Results 1 to 4 of 4

Thread: Handling multiple comboboxs and selected index changed events...

  1. #1
    New Member
    Join Date
    Aug 12
    Posts
    2

    Handling multiple comboboxs and selected index changed events...

    Hi, I have put together (in a laborious fashion) code that controls multiple comboboxes and their selectedIndexChanged events.

    I am using the selectedIndexChanged event to catch when the .text of the comboBox changes, and also to remember the .selectedIndex, so that when the box drops down again, it returns selected at the same position.

    I have multiple iterations of the 2 subs at the bottom of the post for cmbA, cmbB, cmbC etc, and (in the spirit of "if you type it more than once, it can be coded better") I would like to redesign my code so that I can handle all the comboboxes in one sub, ie :


    ########
    private sub comboBox_DropDown( blah ) handles cmbA.Dropdown
    private sub comboBox_DropDown( blah ) handles cmbB.Dropdown
    private sub comboBox_DropDown( blah ) handles cmbC.Dropdown

    and

    private sub comboBox_SelectedIndexChanged ( blah ) handles cmbA.SelectedIndexChanged, cmbB.SelectedIndexChanged, cmbC.SelectedIndexChanged
    #########

    BUT..... I'm stumped as to how I can get the control names and unique variable names that describe the selectedIndex & text values to be passed to the dropdown event and be handles.

    My code that I'm starting from is below, I (think) I need to keep the multiple dropdown events as each one has a unique SQL statement, but if I can at least get rid of the repeated selectedIndexChanged code it would be a start.


    #########
    Private Sub cmbA_DropDown(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmbA.DropDown

    cmbA.SelectedIndex = -1
    cmbA.Items.Clear()

    Try
    Dim query As String = "select something from a dbase"
    Dim connection As New MySqlConnection(connStr)
    Dim cmd As New MySqlCommand(query, connection)
    connection.Open()

    Dim reader As MySqlDataReader
    reader = cmd.ExecuteReader()

    While reader.Read()
    cmbA.Items.Add(reader.GetValue(0))

    End While

    reader.Close()
    connection.Close()

    Catch ex As MySqlException
    Console.WriteLine(ex.Message)

    MessageBox.Show("Database error # " & ex.Number & ": " & ex.Message, ex.GetType.ToString)

    End Try

    If cmbA_SelectedIndex <> -1 Then
    Try
    cmbA.SelectedIndex = cmbA_SelectedIndex
    Catch ex As Exception
    cmbA.SelectedIndex = -1
    End Try
    End If
    End Sub

    Private Sub cmbA_selectedindexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmbA.SelectedIndexChanged
    cmbA_Value = cmbA.Text
    cmbA_SelectedIndex = cmbA.SelectedIndex

    end sub
    #########

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 05
    Location
    Sydney, Australia
    Posts
    80,747

    Re: Handling multiple comboboxs and selected index changed events...

    In every event handler, the 'sender' parameter is a reference to the object that raised the event, i.e. the ComboBox that you want. If you need any other information then you just have to create the appropriate relationship in the first place to allow you to use the ComboBox to access it, e.g. a Dictionary, concurrent arrays or setting the Tag property of the controls.

  3. #3
    Burning Member Niya's Avatar
    Join Date
    Nov 11
    Posts
    3,094

    Re: Handling multiple comboboxs and selected index changed events...

    As Jmc says use the sender parameter in the event handler like:-
    vbnet Code:
    1. '
    2.     Private Sub cmbA_DropDown(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmbA.DropDown,cmbB.DropDown, cmbC.DropDown
    3.         Dim cmb As ComboBox = DirectCast(sender, ComboBox)
    4.  
    5.         cmb.SelectedIndex = -1
    6.         cmb.Items.Clear()
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | Create Sortable BindingList(not mine) | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading


    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. -jmcilhinney

  4. #4
    New Member
    Join Date
    Aug 12
    Posts
    2

    Re: Handling multiple comboboxs and selected index changed events...

    Thank you to JMc and Niya, for explanation and example respectively. This has allowed me to cut great swathes of repeated code.

    Thanks

    Alan

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •