combobox event sequance question

Aquitus 146 Reputation points
2022-03-02T17:33:02.853+00:00

i have a combobox where when the user selects one of its options the option is removed from the second combobox
my code goes somthing like this:

    Private Sub Combo_DropDownClosed(sender As ComboBox, e As EventArgs) Handles dropdown1.???
                If dropdown1.Text = "" Then
                             dropdown2.Items.Remove(dropdown1.SelectedItem)
                ElseIf dropdown1.Text <> dropdown1.SelectedItem Then
                             dropdown2.Items.Add(dropdown1.Text)
                             dropdown2.Items.Remove(dropdown1.SelectedItem)
                End If
    end sub

what can i replace '???' with that happens before the text is written over(dropdown1.Text is still equal to "") and after a item is selected? (dropdown1.SelectedItem is equal to 'example 1' or somthing)

to be clear when the above code completes dropdown2 should not contain what the user selected

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,606 questions
0 comments No comments
{count} votes

Accepted answer
  1. Dewayne Basnett 1,361 Reputation points
    2022-03-02T18:52:03.073+00:00

    I created two combo boxes with string items in them. Then I tested this code and it worked.

        Private Sub ComboBox1_SelectedIndexChanged(sender As Object,
                                                   e As EventArgs) Handles ComboBox1.SelectedIndexChanged
    
            For idx As Integer = 0 To ComboBox2.Items.Count - 1
                Dim itm As String = DirectCast(ComboBox2.Items(idx), String)
                If ComboBox1.SelectedItem Is itm Then
                    ComboBox2.Items.RemoveAt(idx)
                    Exit For
                End If
            Next
        End Sub
    
    1 person found this answer helpful.
    0 comments No comments

3 additional answers

Sort by: Most helpful
  1. LesHay 7,126 Reputation points
    2022-03-02T19:16:05.667+00:00

    Hi
    Dewayne beat me to it. However, here is my offering.

    ' Form1 with ComboBox1
    ' and ComboBox2
    Option Strict On
    Option Explicit On
    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            ' sample data for ComboBoxes
            ComboBox1.Items.AddRange({"One", "Two", "Three", "Four", "Five"})
    
            ' same data just mixed a little
            ComboBox2.Items.AddRange({"Two", "Three", "Five", "One", "Four"})
    
        End Sub
        Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
            Dim s As String = ComboBox1.SelectedItem.ToString
            Dim cb2ind As Integer = ComboBox2.FindString(s)
            If cb2ind > -1 Then
                If MessageBox.Show("Found " & s & " at index " & cb2ind.ToString & " in Combobox2" & vbCrLf & "Click YES to delete it.", "Delete item from CB2", MessageBoxButtons.YesNo) = DialogResult.Yes Then
                    ComboBox2.Items.Remove(s)
                End If
            Else
                MessageBox.Show("'" & s & "' was not found in Combobox2")
            End If
        End Sub
    End Class
    
    1 person found this answer helpful.
    0 comments No comments

  2. LesHay 7,126 Reputation points
    2022-03-02T18:15:04.703+00:00

    Hi
    Here is some info on finding and using auto handler insertion.

    From the Designer, select a Control (ComboBox in your case), and then choose the lightning symbol (see image) from the Property pane - this will open all the events for the control. Double click on the event you wantto use and the base framework for the event will be inserted into your code.

    179360-111.png

    Alternatively, when in the code editor, and say you have one Handler Sub there and you want to insert another for a different event, then put the cursor in the existing Handler code and choose the drop down above (see image) and select the desired event you want and the base code will be inserted into your code.

    179364-444.png

    Doing thing that way will deal with the (signatures) needed to handle the event.

    The 'signature' you want above is:

    Private Sub ComboBox1_DropDownClosed(sender As Object, e As EventArgs) Handles ComboBox1.DropDownClosed  
    
    0 comments No comments

  3. Aquitus 146 Reputation points
    2022-03-02T20:03:57.893+00:00

    after thoroughly inserting my code into every possibly event ive come to the conclusion that for some reason none of them are able to do what my code used to be able to do
    there isnt an event that occurs before the text is updated, ill have to write my code around that fact now

    0 comments No comments