lundi 5 février 2018

Disable textboxes with the corresponding checkboxes in VB.net

Public Class Form1

Dim controlNames() As String = {"Tea", "Cola", "Coffee", "Orange", "Water", "VanillaCone", "VanillaShake", "StrawberryShake", "ChocolateMilkshake", "Fries", "Salad", "Hamburger", "OnionRings", "ChickenSalad", "FishSandwich", "CheeseSandwich", "ChickenSandwich"}


Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load

    For Each ctrl As Control In Me.Panel2.Controls

        If TypeOf ctrl Is TextBox Then
                ctrl.Enabled = False
            End If

    Next

End Sub

Private Sub Timer1_Tick(sender As Object, e As EventArgs)
    For i = 0 To 16
        For Each ctrl As Control In Me.Panel2.Controls
            If TypeOf ctrl Is CheckBox Then
                If ctrl.Name = "chk" & controlNames(i) Then
                    If DirectCast(ctrl, CheckBox).CheckState = CheckState.Checked Then
                        If TypeOf ctrl Is TextBox Then
                            If ctrl.Name = "txt" & controlNames(i) Then
                                ctrl.Enabled = True
                            End If
                        End If
                    End If
                End If
            End If
        Next
    Next
End Sub

I have a VB.NET assignment and I am attempting to enable textboxes based on whether or not the checkbox with the same name was checked. This is my code so far and it obviously doesn't work. What I want to do essentially:

All textboxes start as disabled. Then, textboxes only get enabled if the corresponding checkboxes are checked. For example, if chkTea is checked, then it enables txtTea.

I am aware I can copy paste something like "if chkTea = checked then txt.tea = enabled". I do not want to do this as this seems like a poor way to go about this. I want to know if I can do something like my barely readable code shows.




Aucun commentaire:

Enregistrer un commentaire