Results 1 to 4 of 4

Thread: Combo Box Events

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2011
    Posts
    71

    Combo Box Events

    I have combobox1 and combobox2, both databound.

    Box2 will be a selected value that changes based off box1 selection.

    Code:
    Private Sub ComboBox2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox2.SelectedIndexChanged
            If ComboBox2.Text = "1" Then
                Form1.show()
            ElseIf ComboBox2.Text = "2" Then
                Form2.show()
            End If
        End Sub
    Problem is, when you select items from Box1 and no changes are made to Box2 like say it stays at "2" then it never fires the event. Also, if i do it to the Box1 event, it goes off what is in Box2 before it changed, not what it changed to.

    How can I get it to trigger the event (or what event should I use) if Box2 value doesn't change???

  2. #2
    Addicted Member
    Join Date
    Aug 2010
    Posts
    164

    Re: Combo Box Events

    I tried out a workaround - hope it helps

    Code:
    Private Sub ComboBox1_SelectedValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedValueChanged
            ComboBox2.Text = ComboBox1.Text
            If ComboBox2.Text = "1" Then
                Form1.Show
            ElseIf ComboBox2.Text = "2" Then
                Form2.Show
            End If
        End Sub

  3. #3
    New Member
    Join Date
    Nov 2010
    Location
    U.S.
    Posts
    15

    Re: Combo Box Events

    I came up with this, just in case you do need both CBs (ComboBoxes);

    vb Code:
    1. Public Class Form1
    2.  
    3.     Private Sub Form1_Load(ByVal sender As System.Object, _
    4.     ByVal e As System.EventArgs) Handles MyBase.Load
    5.         Me.BackColor = Color.Red
    6.         Form2.BackColor = Color.Green
    7.         CB1.Items.Add("1")
    8.         CB1.Items.Add("2")
    9.         CB2.Items.Add("1")
    10.         CB2.Items.Add("2")
    11.     End Sub
    12.  
    13.     Private Sub CB1_SelectedIndexChanged(ByVal sender As System.Object, _
    14.     ByVal e As System.EventArgs) Handles CB1.SelectedIndexChanged
    15.         CB2.ResetText()
    16.         If CB1.SelectedIndex = 0 Then
    17.             CB2.SelectedIndex = 0
    18.         Else
    19.             CB2.SelectedIndex = 1
    20.         End If
    21.     End Sub
    22.     Private Sub ComboBox2_SelectedIndexChanged(ByVal sender As System.Object, _
    23.     ByVal e As System.EventArgs) Handles CB2.SelectedIndexChanged
    24.         If CB2.Text = "1" Then
    25.             Me.Show()
    26.         ElseIf CB2.Text = "2" Then
    27.             Me.Hide()
    28.             Form2.Show()
    29.         End If
    30.     End Sub
    31. End Class

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Feb 2011
    Posts
    71

    Re: Combo Box Events

    Thanks for the help so far, it's not quite working for me, I don't think I explained it very well, I was trying to keep it basic. I'm using MDI forms, they load within a parent based on CB2's text... whatever item is displayed in CB2, that's what MDIChild data entry form shows up. Right now I have the datatable.fill in the FormActivated event and the datatable.update in the FormDeactivate event. I just use bringtofront based off CB2's text. Problem is, if you select an item from CB1 and CB2 doesn't change, it doesn't fire the event to activate the page so therefore the data never gets update/filled. The form just stays there with the old data in it.

    To keep it simple, lets say CB1 is names... John, Jim, Jake... (could be a hundred names)
    CB2 is fruit... Apple, Orange, Banana, etc... (could be like 20 fruits)

    So you select John and CB2 switches to Banana, that's what is selected and saved in the database... everytime you select John the CB2 will change to Banana until you choose a different one, and the banana form would be shown, and John's data would be loaded.

    BUT, if you select Jake and it's also Banana, the event won't fire and Jakes data won't load because CB2 never changed, and John's data will still be displayed. If you had selected Jim and CB2 switches to Apple, the event fires and loads Jim's data...

Posting Permissions

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



Click Here to Expand Forum to Full Width