How to make a simple function

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



Answer this question

How to make a simple function

  • Jeanne P

    Cool, thanx for the replies and I will try them out.
  • AlucardHellSing

    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 = CType(sender, CheckBox)
            Select Case ckBox.Name
                Case "c1"
                    w1.Checked = Not ckBox.Checked
                Case "c2"
                    w2.Checked = Not ckBox.Checked
                Case "c3"
                    w3.Checked = Not ckBox.Checked
                Case "c4"
                    w4.Checked = Not ckBox.Checked
                Case "c5"
                    w5.Checked = Not ckBox.Checked
                Case "c6"
                    w6.Checked = Not ckBox.Checked
                Case "c7"
                    w7.Checked = Not ckBox.Checked
                Case "w1"
                    c1.Checked = Not ckBox.Checked
                Case "w2"
                    c2.Checked = Not ckBox.Checked
                Case "c3"
                    c3.Checked = Not ckBox.Checked
                Case "w4"
                    c4.Checked = Not ckBox.Checked
                Case "w5"
                    c5.Checked = Not ckBox.Checked
                Case "w6"
                    c6.Checked = Not ckBox.Checked
                Case "w7"
                    c7.Checked = Not ckBox.Checked
            End Select
        End S


    ----------------------------------------------------------------

    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.

    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 = CType(sender, CheckBox)
    Select Case ckBox.Name
    Case "c1"
    w1.Checked = Not ckBox.Checked
    Case "c2"
    w2.Checked = Not ckBox.Checked
    Case "c3"
    w3.Checked = Not ckBox.Checked
    Case "c4"
    w4.Checked = Not ckBox.Checked
    Case "c5"
    w5.Checked = Not ckBox.Checked
    Case "c6"
    w6.Checked = Not ckBox.Checked
    Case "c7"
    w7.Checked = Not ckBox.Checked
    Case "w1"
    c1.Checked = Not ckBox.Checked
    Case "w2"
    c2.Checked = Not ckBox.Checked
    Case "c3"
    c3.Checked = Not ckBox.Checked
    Case "w4"
    c4.Checked = Not ckBox.Checked
    Case "w5"
    c5.Checked = Not ckBox.Checked
    Case "w6"
    c6.Checked = Not ckBox.Checked
    Case "w7"
    c7.Checked = Not ckBox.Checked
    End Select
    End S



  • 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

    LOL, dont worry, i saw it and thanks again.
  • 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.Load

    C1.Checked = True

    C2.Checked = True

    C3.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 Sub


  • dragoncells

    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

    I am getting a nullreferenceexception was unhandled error for the code posted last. It looks like what I want, just needs to work.
  • Heinz Krug

    Cool, I have learned a lot today, thank you for all the help.
  • 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



  • How to make a simple function