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 = checked,那么txt.tea = enabled"。我不想这样做,因为这似乎是一个糟糕的方式来解决这个问题。我想知道我是否可以做一些像我几乎无法阅读的代码演示。
答案 0 :(得分:2)
如果您要将所有控件重命名为与默认名称不同的控件,则此代码应该可以正常工作。
你不需要一个计时器,当任何一个CheckBox的状态发生变化时它就会触发。
在示例代码中,我创建了一个CheckedChanged处理程序,并将其设置为处理一些CheckBox(您希望添加所有要处理的内容)。如果单击任何这些CheckBox,则处理程序将触发。然后处理程序将哪个复选框更改为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
答案 1 :(得分:0)
在您浏览完每个复选框之后,您只关注该复选框,因此当您找到正确的复选框时,您需要通过表单上的所有控件RELOOP重新检查它们是否是相应的文本框
像下面这样的东西应该有用,或者至少让你开始沿着正确的道路前进:
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 Sub