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我有一个VB.NET赋值,并且我试图根据是否选中同名复选框来启用文本框。到目前为止,这是我的代码,显然不起作用。我想要做的是:
所有文本框都以禁用的形式开始。然后,只有在选中相应的复选框时才启用文本框。例如,如果选中chkTea,则启用txtTea。
我知道我可以复制类似于“如果chkTea =选中,那么txt.tea =已启用”之类的粘贴。我不想这样做,因为这似乎是一种糟糕的做法。我想知道我是否能做一些类似于我难以读懂的代码显示的事情。
发布于 2018-02-05 15:13:59
如果要用数字将所有控件重命名为与默认名称不同的名称,则此代码应该工作得很好。
您不需要计时器,它只是在任何CheckBoxes状态发生更改时触发。
在示例代码中,我创建了一个CheckedChanged处理程序,并将其设置为处理某些CheckBoxes (您需要添加所有想要处理的CheckBoxes)。如果单击这些CheckBoxes中的任何一个,处理程序将触发。然后,处理程序将哪个复选框更改为SyncTextBoxWithCheckBoxState方法。然后使用FindMatchingCheckBox方法查找匹配的文本框,并将文本框的.Enabled状态设置为与CheckBox的.Checked状态相同。
Private Sub chkTea_CheckedChanged(sender As Object, e As EventArgs) Handles chkTea.CheckedChanged, chkCoffee.CheckedChanged, chkCola.CheckedChanged, chkOrange.CheckedChanged, chkTea.CheckedChanged, chkVanillaCone.CheckedChanged, chkVanillaCone.CheckedChanged, chkWater.CheckedChanged
SyncTextBoxWithCheckBoxState(CType(sender, CheckBox))
End Sub
Private Sub SyncTextBoxWithCheckBoxState(chkBox As CheckBox)
Dim txtBox As TextBox = FindMatchingTextBox(chkBox)
txtBox.Enabled = chkBox.Checked
End Sub
Private Function FindMatchingTextBox(chkbox As CheckBox) As TextBox
For Each ctrl As Control In Panel2.Controls
If ctrl.GetType Is GetType(TextBox) And ctrl.Name.Contains(chkbox.Name.Substring(3)) Then
Return CType(ctrl, TextBox)
End If
Next
Return Nothing
End Function编辑
要使代码目标有多个面板,请像以前一样将要检测到的所有复选框添加到事件处理程序中,并且在FindMatchingTextBox方法中,只需在现有面板周围添加另一个循环,以遍历每个面板。像这样..。
Private Function FindMatchingTextBox(chkbox As CheckBox) As TextBox
'This is the new loop which loops through the two panels. It's
'a bit quick and dirty, but it works
For Each pnl As Panel In New Panel() {Panel2, Panel3}
'In this line note that the reference to Panel2 now refers to pnl
For Each ctrl As Control In pnl.Controls
If ctrl.GetType Is GetType(TextBox) And ctrl.Name.Contains(chkbox.Name.Substring(3)) Then
Return CType(ctrl, TextBox)
End If
Next
'End point of the new loop
Next
Return Nothing
End Function发布于 2018-02-05 12:06:34
在遍历了每个复选框之后,您只关心该复选框,因此当您找到正确的复选框时,需要重新循环窗体上的所有控件,如果它们是相应的textbox。
像下面这样的东西应该能起作用,或者至少让你走上正确的道路:
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
For Each ctrl As Control In Me.Panel2.Controls
If TypeOf ctrl Is TextBox Then
If ctrl.Name = "txt" & controlNames(i) Then
ctrl.Enabled = True
End If
End If
Next
End If
End If
End If
Next
Next
End Subhttps://stackoverflow.com/questions/48621580
复制相似问题