Results 1 to 5 of 5

Thread: [RESOLVED] [2008] CheckBox Loop for Uncheck then Check

  1. #1

    Thread Starter
    New Member VBNetUser84's Avatar
    Join Date
    Feb 2007
    Posts
    10

    Resolved [RESOLVED] [2008] CheckBox Loop for Uncheck then Check

    I have a little problem that nearly drives me xx! I've five Checkboxes and when I check the fifth I want the others to be unchecked and number five stay checked.
    In runtime I got stuck with owerflow when it lies in "CheckBox5_CheckedChanged" Method and in other it wont just work guess it pretty simple right?

    Code:
        Private Sub CheckBox5_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles CheckBox5.Click
            Dim Check As Control
    
            If CheckBox5.Checked = True Then 
                CheckBox5.Checked = False
                Exit Sub
            Else
                For Each Check In Me.GroupBox1.Controls
                    If TypeOf Check Is CheckBox Then
                        CType(Check, CheckBox).Checked = False
                    End If
                Next
    
                CheckBox5.Checked = True
            End If
    
        End Sub

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: [2008] CheckBox Loop for Uncheck then Check

    vb.net Code:
    1. If not CheckBox5.Checked Then
    2.    For Each Check In Me.GroupBox1.Controls
    3.         If TypeOf Check Is CheckBox  and check.name <> "CheckBox5" Then
    4.            CType(Check, CheckBox).Checked = False
    5.         End If
    6.    Next
    7. End If
    Last edited by .paul.; Oct 8th, 2008 at 02:41 PM.

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2008] CheckBox Loop for Uncheck then Check

    Why do you need a loop? You know exactly what you want to do:
    vb.net Code:
    1. If Me.CheckBox5.Checked Then
    2.     Me.CheckBox1.Checked = False
    3.     Me.CheckBox2.Checked = False
    4.     Me.CheckBox3.Checked = False
    5.     Me.CheckBox4.Checked = False
    6. End If
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    New Member VBNetUser84's Avatar
    Join Date
    Feb 2007
    Posts
    10

    Re: [2008] CheckBox Loop for Uncheck then Check

    Nice Paul that worked great thanks - much better syntax never would figuered that one out, I was stucked with my pretty If-code =)



    To answear jmcilhinney I say that's not programming, and about my program there will be more checkboxes and maybe I do a Sub out of it.

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2008] CheckBox Loop for Uncheck then Check

    Quote Originally Posted by VBNetUser84
    To answear jmcilhinney I say that's not programming
    Hmmm... I could have sworn it was. Oh well. I guess I'll keep that in mind for the future.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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