Results 1 to 6 of 6

Thread: [2005] Newbie question

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2006
    Location
    Washington
    Posts
    3

    [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

  2. #2
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: [2005] Newbie question

    you can use:

    VB Code:
    1. For Each ctrl As Control In Me.Controls
    2.             If TypeOf (ctrl) Is CheckBox And ctrl.Name.Substring(0, 4) = "name" Then
    3.                 If DirectCast(ctrl, CheckBox).Checked = True Then MsgBox(ctrl.Name.Substring(4, 1) & " is checked")
    4.             End If
    5.         Next
    Last edited by Atheist; Mar 31st, 2006 at 01:41 PM.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  3. #3

    Thread Starter
    New Member
    Join Date
    Mar 2006
    Location
    Washington
    Posts
    3

    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,

  4. #4
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: [2005] Newbie question

    oh yeah sorry.

    Made some modifications and it works now
    VB Code:
    1. For Each ctrl As Control In Me.Controls
    2.             If TypeOf (ctrl) Is CheckBox And ctrl.Name.Length >= 4 Then
    3.                 If DirectCast(ctrl, CheckBox).Checked = True And LCase(ctrl.Name.Substring(0, 4)) = "name" Then MsgBox(ctrl.Name.Substring(4, 1) & " is checked")
    4.             End If
    5.         Next
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  5. #5
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re: [2005] Newbie question

    You can simply add the checkboxes onto the Handles clause, to allow them to call the same sub...
    VB Code:
    1. Private Sub chk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
    2.                               Handles chk.Click, chk2.Click, chk3.Click, chk4.Click
    3.        'your check code here
    4. End Sub
    The above code would be called anytime one of the checkboxes listed in the handles clause are clicked

  6. #6

    Thread Starter
    New Member
    Join Date
    Mar 2006
    Location
    Washington
    Posts
    3

    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
  •  



Click Here to Expand Forum to Full Width