I have a form with A GroupBox on it. Within the GroupBox is a TextBox and a ComboBox. I am trying to disable one or the other when either text is entered into the textbox OR when an item has been selected in the combobox.

I can get the combobox to become disabled by using the TextChanged event in the textbox control. If I select an item in the combobox control it will disable the textbox using the SelectedIndex event, however if I delete the selection manually, I cannot get the textbox to become enabled and the combobox to become disabled.

This is the code I have so far:

    Private Sub TextBox5_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox5.TextChanged
        If TextBox5.Text <> "" Then
            ComboBox1.Enabled = False
        Else
            ComboBox1.Enabled = True
        End If
    End Sub

    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
        If ComboBox1.SelectedIndex = -1 Then
            TextBox5.Enabled = True
        ElseIf ComboBox1.SelectedIndex <> -1 Then
            TextBox5.Enabled = False
        End If
    End Sub

Hi,

Try this:

 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ComboBox1.Items.Add("")
        ComboBox1.Items.Add("1")
    End Sub

    Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
        If Not TextBox1.Text <> "" Then
            ComboBox1.Enabled = True
        Else
            ComboBox1.Enabled = False
        End If
    End Sub

    Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
        If Not ComboBox1.SelectedIndex <> 1 Then
            TextBox1.Enabled = False
        Else
            TextBox1.Enabled = True
        End If
    End Sub

Here's a way to disable the other control when text is entered and switch back if the text is deleted:

Private Sub TextBox2_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBox2.TextChanged
    If TextBox2.Text = "" Then
        TextBox2.Enabled = False
    Else
        TextBox2.Enabled = True
    End If
    ComboBox1.Enabled = Not TextBox2.Enabled
End Sub

Private Sub ComboBox1_TextChanged(sender As System.Object, e As System.EventArgs) Handles ComboBox1.TextChanged
    If ComboBox1.Text = "" Then
        ComboBox1.Enabled = False
    Else
        ComboBox1.Enabled = True
    End If
    TextBox2.Enabled = Not ComboBox1.Enabled
End Sub

On a side note, one thing you might be forgetting. The combobox is not just a dropdown box it's also a textbox. Therefore with just a combobox the user can type their input or choose one from the list or edit an item in the list. This would eliminate the textbox and enabling and disabling the controls.

tinstaafl:
You are a genius and totally correct! Thank you for your help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.