0

I would like to ask if it is possible for VB.net applications to detect if a combobox has been changed without using SelectedIndexChange.

For Example my application has 4 comboboxes and this comboboxes was dynamically created from the user configuration. (the tool autocreates the comboboxes when the application was launched). Then once the application was launched, I want to run a SUB everytime those comboboxes was changed. Badly needed help. TIA

Ňɏssa Pøngjǣrdenlarp
  • 38,411
  • 12
  • 59
  • 178
user2819919
  • 35
  • 2
  • 4
  • Dynamically created controls can respond to events, so whats the issue with `SelectedIndexChanged`? – Ňɏssa Pøngjǣrdenlarp Jul 11 '16 at 18:13
  • 1
    You may be asking the [wrong question](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). – sstan Jul 11 '16 at 18:15
  • Why don't you look into add handlers? This is what you would need since you are creating the controls at runtime... Without showing any of your current code it makes this kind of hard to help you. Please update your question with what you have tried for us to further help you. – Trevor Jul 11 '16 at 18:23

1 Answers1

1

If you are creating controls dynamically, you should add the event handlers dynamically as well. There is nothing wrong with using the SelectedIndexChanged event.

You can test this by making a new project and pasting this code inside Public Class Form1.

Private myComboBox1 As ComboBox
Private myComboBox2 As ComboBox

Private Shared selectedIndexChanged As EventHandler =
    Sub(sender As Object, e As EventArgs)
        Dim myComboBox = DirectCast(sender, ComboBox)
        ' alert the user as to what was selected
        MessageBox.Show(String.Format("{0} value: {1}, index: {2}",
                        myComboBox.Name, myComboBox.Text, myComboBox.SelectedIndex))
        ' you can do something different on each one by name in a case statement
        Select Case myComboBox.Name
            Case "myComboBox1"
                ' do something for 1
            Case "myComboBox2"
                ' do something for 2
        End Select
    End Sub

Private Sub addHandlers()
    AddHandler myComboBox1.SelectedIndexChanged, selectedIndexChanged
    AddHandler myComboBox2.SelectedIndexChanged, selectedIndexChanged
End Sub

Private Sub removeHandlers()
    RemoveHandler myComboBox1.SelectedIndexChanged, selectedIndexChanged
    RemoveHandler myComboBox2.SelectedIndexChanged, selectedIndexChanged
End Sub

Form eventhandlers

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    ' dynamically generate combo boxes
    myComboBox1 = New ComboBox() With {.Name = "myComboBox1",
                                       .Left = 30,
                                       .Top = 30}
    myComboBox2 = New ComboBox() With {.Name = "myComboBox2",
                                       .Left = 30,
                                       .Top = 60}
    ' add some items
    myComboBox1.Items.AddRange({1, 2, 3})
    myComboBox2.Items.AddRange({"four", "five", "six"})
    ' add the combo boxes to the form
    Me.Controls.Add(myComboBox1)
    Me.Controls.Add(myComboBox2)
    ' add event handlers
    addHandlers()
End Sub

Private Sub Form1_FormClosed(sender As Object, e As FormClosedEventArgs) Handles Me.FormClosed
    removeHandlers()
End Sub
djv
  • 15,168
  • 7
  • 48
  • 72
  • Not only did you post an answer that clearly the OP hasn't attempted or tried, it's important to remove the handlers as well; bad things happen when handlers are ***not*** removed. Please try and refrain from posting an answer when the OP doesn't show an attempt. If things like this manifest, people may get the idea ***we will code it for them*** which is not the case here. Hence the reason I didn't post an answer, but rather a comment. – Trevor Jul 11 '16 at 20:19
  • **You can test this by making a new project and pasting this code...** means that this is a demo and not intended to be a complete solution for OP. OP can take what he wants from it, and ignore what he doesn't. You said it's hard to help him. For you, maybe? I enjoyed writing it and I hope this demo has enough of the elements he needs to get his code working. By the way, SO suffers when newbies are turned away by not getting help when not knowing exactly how to ask a proper question. I saw three elitist comments and not much help up there. – djv Jul 11 '16 at 21:00
  • I have updated the answer to store the event handler in a variable so it can be removed later. It should be done when the controls are removed from your form. It could also be done when the form is disposed http://stackoverflow.com/questions/17399991/should-i-always-disconnect-event-handlers-in-the-dispose-method but that code doesn't fit in the example very well. – djv Jul 11 '16 at 21:14
  • 1
    Thank you so much for your response Sir Verdolino it it very much appreciated. This piece of code would be very much helpful to create my own code. I'm a newbie and I badly needed an idea or sample to finish my work. I tried searching for answers but the thing is I don't know exactly what to search. I wouldn't be posting this question if I haven't tried to solve it by myself. – user2819919 Jul 12 '16 at 10:27
  • I only explained my situation because I'm not expecting a coded answer for this. I just want an idea on how it is to be coded or ways on how it could be done. But still some of you think that we're not even trying. Some of us newbie also wants to become a good professional like you do. – user2819919 Jul 12 '16 at 10:43