A.Sidhu
A.Sidhu

Reputation: 11

If ComboBox equals an item on the list then do this?

I'm doing a program on Visual Basic Community 2017, that has a serious of ComboBoxes with various dropdown items. If there is a certain combination of the Comboboxes, then open this form. How would I implement this?

E.g

ComboBox1 items (string) = 1, 2, 3 ,4 ,5

ComboBox2 items (string) = a, b, c, d, e

ComboBox 3 items (string)= A, B, C, D, E

The user picks 1,a,A

Clicks a button

Then Show form 1

Thanks, I hope it makes enough sense.

The code I tried was

Public Class Form2
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        If ComboBox1.SelectedText.ToString() = "1" And ComboBox2.SelectedText.ToString() = "a" And ComboBox3.SelectedText.ToString() = "A" Then
            Then 
            Form1.Show()
        Else
            MsgBox("Doesn't Work")

        End If
    End Sub
End Class

Upvotes: 0

Views: 6994

Answers (1)

McMillan Cheng
McMillan Cheng

Reputation: 380

Following is example code based on your case. Hope this can help you.

Public Class Form2
    Dim selectedItem1, selectedItem2, selectedItem3 As Object

    Private Sub ComboBox2_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox2.SelectedIndexChanged
        showForm1()
    End Sub

    Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
        showForm1()
    End Sub

    Private Sub ComboBox3_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox3.SelectedIndexChanged
        showForm1()
    End Sub

    Private Sub showForm1()
        selectedItem1 = ComboBox1.SelectedItem
        selectedItem2 = ComboBox2.SelectedItem
        selectedItem3 = ComboBox3.SelectedItem
        If selectedItem1 Is Nothing OrElse selectedItem2 Is Nothing OrElse selectedItem3 Is Nothing Then
            Exit Sub
        End If

        If ((selectedItem1.ToString() = "1") AndAlso (selectedItem2.ToString() = "a") AndAlso (selectedItem3.ToString() = "A")) Then
            Form1.Show()
        End If
    End Sub

    Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    ' your code to initialize items of ComboBox1, ComboBox2, ComboBox3
    End Sub
End Class

Upvotes: 1

Related Questions