Hi, I was wondering if there is anyway to turn a huge amount of code into a simple function. Right now I have a bunch of check boxes and they are all named C1-C7 and W1-W7, instead of putting in code for each one when clicked, which results in a large amount of code, is there anyway I could make one function to handle all of these.
I tried something like this.
If C(num).checked=true then
W(num).checked=false
endif
its just a basic example of what i want and i could really use this later on.
Thanks

How to make a simple function
Jeanne P
AlucardHellSing
----------------------------------------------------------------
I could be wrong but this could be written thusly:
Private Sub c1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles c1.CheckedChanged, c2.CheckedChanged, c3.CheckedChanged, c4.CheckedChanged, c5.CheckedChanged, c6.CheckedChanged, c7.CheckedChanged, w1.CheckedChanged, w2.CheckedChanged, w3.CheckedChanged, w4.CheckedChanged, w5.CheckedChanged, w6.CheckedChanged, w7.CheckedChanged
Dim ckBox As CheckBox = sender
ckBox.checked = Not ckBox.Checked
end sub
Unless C7's name is not "w7"
That might be a bad assumption on my part. The function of the code doesn't make sense
If a user checks a box.... uncheck it and vice-versa
Btw this is a subroutine or method it is not a function.
Tryin2Bgood
I don't know if this is any shorter but it is more compact. I assume that you are toggleing between the two sets of check boxes.
ramesh_1031
Well, it gets out of the infinite loop but it doesnt allow me to check anything. I can probably use it, still want it to uncheck rW1 if rC1 is checked.
Oh, whats the difference between
ckBox.checked = Not ckBox.Checked
and
ckBox.checked = false
Aniruddha Surange
hsm13
The following is just for three buttons per set. Just add more handles clauses as required.
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.LoadC1.Checked =
TrueC2.Checked =
TrueC3.Checked =
True End Sub Private Sub CheckChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles C1.CheckedChanged, C2.CheckedChanged, C3.CheckedChanged, W1.CheckedChanged, W2.CheckedChanged, W3.CheckedChanged Dim Box As CheckBox = CType(sender, CheckBox) If Box.Name.StartsWith("C") Then CType(Me.Controls("W" & Box.Name.Substring(1)), CheckBox).Checked = Not Box.Checked ElseIf Box.Name.StartsWith("W") Then CType(Me.Controls("C" & Box.Name.Substring(1)), CheckBox).Checked = Not Box.Checked End If End Subdragoncells
The code
Dim ckBox As CheckBox = sender
ckBox.checked = Not ckBox.Checked
makes an infinite loop, but now i know how to use sender which should be very useful, thanks!
JavaBoy
Dang, thought i had dave299s code working
McGeeky
ckBox.checked = Not ckBox.Checked ' is the opposite of checboxes current setting
and
ckBox.checked = false ' Always clears the setting........
GinaK
Heinz Krug
Ort7667
BE SURE to see what I left in the thread. If you don't, you'll hate yourself in the morning.
Reza Bemanian
Well WayneSpanglers example does work but the other one might be nice for future use, ad thanx for showing me how to use cases.
Just note that all the cases had to be uppercase to match mine, thats all.
EX. c4 must be C4
WXS123
Private Sub c1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles c1.CheckedChanged, c2.CheckedChanged, c3.CheckedChanged, c4.CheckedChanged, c5.CheckedChanged, c6.CheckedChanged, c7.CheckedChanged, w1.CheckedChanged, w2.CheckedChanged, w3.CheckedChanged, w4.CheckedChanged, w5.CheckedChanged, w6.CheckedChanged, w7.CheckedChanged
Static DismissEvent as Boolean
If DismissEvent then Exit Sub
DismissEvent = true
Dim ckBox As CheckBox = sender
ckBox.checked = Not ckBox.Checked
Application.DoEvents
DismissEvents = False
end sub
Cheap Synchronization