|
-
Mar 31st, 2006, 01:17 PM
#1
Thread Starter
New Member
[2005] Newbie question
I am looking for a way to check the state of several different checkboxes without having to write the same code over and over with differen name.checked = true
IE: I want to reduce the code
Private Sub chk_Click(ByVal sender As System.Object, ByVal e_
As System.EventArgs) Handles chk.Click
if name1.checked = True then msgbox("1 is checked")
if name2.checked = True then msgbox("2 is checked")
if name3.checked = True then msgbox("3 is checked")
ect.. up to 6
End Sub
Any help would be great.
Thanks,
Steve
-
Mar 31st, 2006, 01:35 PM
#2
Re: [2005] Newbie question
you can use:
VB Code:
For Each ctrl As Control In Me.Controls
If TypeOf (ctrl) Is CheckBox And ctrl.Name.Substring(0, 4) = "name" Then
If DirectCast(ctrl, CheckBox).Checked = True Then MsgBox(ctrl.Name.Substring(4, 1) & " is checked")
End If
Next
Last edited by Atheist; Mar 31st, 2006 at 01:41 PM.
-
Mar 31st, 2006, 02:34 PM
#3
Thread Starter
New Member
Re: [2005] Newbie question
I tried to inster your text but got the following error
System.ArgumentOutOfRangeException was unhandled
Message="Index and length must refer to a location within the string.
Parameter name: length"
ParamName="length"
Source="mscorlib"
StackTrace:
it looks like it has to do with the (ctrl)
thanks,
-
Mar 31st, 2006, 02:55 PM
#4
Re: [2005] Newbie question
oh yeah sorry.
Made some modifications and it works now 
VB Code:
For Each ctrl As Control In Me.Controls
If TypeOf (ctrl) Is CheckBox And ctrl.Name.Length >= 4 Then
If DirectCast(ctrl, CheckBox).Checked = True And LCase(ctrl.Name.Substring(0, 4)) = "name" Then MsgBox(ctrl.Name.Substring(4, 1) & " is checked")
End If
Next
-
Mar 31st, 2006, 03:57 PM
#5
Re: [2005] Newbie question
You can simply add the checkboxes onto the Handles clause, to allow them to call the same sub...
VB Code:
Private Sub chk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles chk.Click, chk2.Click, chk3.Click, chk4.Click
'your check code here
End Sub
The above code would be called anytime one of the checkboxes listed in the handles clause are clicked
-
Mar 31st, 2006, 04:26 PM
#6
Thread Starter
New Member
Re: [2005] Newbie question
Thanks that is working.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|