|
-
Aug 20th, 2012, 10:23 AM
#1
Thread Starter
New Member
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
#########
-
Aug 20th, 2012, 11:05 AM
#2
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.
-
Aug 20th, 2012, 11:14 AM
#3
Re: Handling multiple comboboxs and selected index changed events...
As Jmc says use the sender parameter in the event handler like:-
vbnet Code:
' Private Sub cmbA_DropDown(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmbA.DropDown,cmbB.DropDown, cmbC.DropDown Dim cmb As ComboBox = DirectCast(sender, ComboBox) cmb.SelectedIndex = -1 cmb.Items.Clear()
-
Aug 23rd, 2012, 04:11 AM
#4
Thread Starter
New Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|