How to only check that checkbox which is entered in textbox? [closed]
How to only check that checkbox which is entered in textbox ?
If i have 4 checkboxes eith text checkbox1, checkbox2, checkbox3 and checkbox4 and 1 textbox
if i type in textbox 1,2 then only checkbox1 and checkbox2 would be checked again directly after doing that i开发者_如何学Python type 3,4 in textbox then only 3,4 will be checked and 1,2 will be uncheked..
remember i wanna do this coding for unlimited no. of checkboxes .. so dnt provide coding for 4 checkboxes only ..... provide me the coding for that for unlimited checkboxes...
Just loop through all your checkboxes and uncheck them before you check the new ones.
try using a naming convention for TextBoxes and CheckBoxes. so if you use one TextChanged event handler for all of them, you can use Sender
parameter to identify which text box is changing. then uncheck previous check box and check new one:
Public Class Form1
Public oldCheckBox As CheckBox
Private Sub TextBox4_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox4.TextChanged, TextBox3.TextChanged, TextBox2.TextChanged, TextBox1.TextChanged
Dim x As TextBox = sender
Dim i As Integer = x.Text.Substring(7) // 7 = length of "TextBox"
Dim relatedCheckBox As CheckBox = Me.Controls("CheckBox" + i.ToString())
relatedCheckBox.Checked = True
If (oldCheckBox IsNot Nothing) And (oldCheckBox IsNot relatedCheckBox) Then
oldCheckBox.Checked = False
End If
oldCheckBox = relatedCheckBox
End Sub
End Class
精彩评论