0

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
McMillan Cheng
  • 382
  • 1
  • 6
  • 20
A.Sidhu
  • 11
  • 1
  • 2
  • 1
    Probably an `If` statement would be the appropriate way to achieve your goal. – YowE3K Nov 05 '17 at 06:27
  • 1
    I think I should've added this to the question. I tried the If statement approach, but it didn't show the form I wanted. In fact it showed nothing. – A.Sidhu Nov 05 '17 at 06:28
  • 1
    Definitely update the question to include the code you tried. Doing so will help prevent the question from being closed as "too broad" (or similar), and will help prevent downvotes for "does not show any research effort". – YowE3K Nov 05 '17 at 06:45
  • 1
    Replace `SelectedText` with `SelectedItem`. Also use [**`AndAlso` instead of `And`**](https://stackoverflow.com/questions/8409467/orelse-and-or-and-andalso-and-and-when-to-use). – Visual Vincent Nov 05 '17 at 08:13

1 Answers1

1

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
McMillan Cheng
  • 382
  • 1
  • 6
  • 20