Results 1 to 7 of 7

Thread: changing a property of all checkboxes on a form

  1. #1

    Thread Starter
    Frenzied Member wengang's Avatar
    Join Date
    Mar 2000
    Location
    Beijing, China
    Posts
    1,604
    I know this is an easy one.
    Please tell me what the right code is here.

    For each (checkbox) in (form)
    (checkbox).value = 0

    Just to "turn off" all the checkboxes at once.

    I know it's either this or a with statement but I don't remember it.

    If you do, please post.
    Thanks.
    wengang
    Wen Gang, Programmer
    VB6, QB, HTML, ASP, VBScript, Visual C++, Java

  2. #2
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527
    you could put all the check boxes into a control array and make it very easy:
    Code:
    For i = 0 To Check1.Count - 1
    If Check1(i).Value = 1 Then
    Check1(i).Value = 0
    Else
    Check1(i).Value = 1
    End If
    Next i
    this alternates the status of each checkbox.

  3. #3

    Thread Starter
    Frenzied Member wengang's Avatar
    Join Date
    Mar 2000
    Location
    Beijing, China
    Posts
    1,604
    Thanks

    but the code i'm looking for is along these lines

    Dim fCheckbox As Checkbox
    For Each fCheckbox In frmMain
    fCheckbox.Value= 0
    Next

    This code doesn't work obviously, so does anybody know how to repair it?

    Wen Gang, Programmer
    VB6, QB, HTML, ASP, VBScript, Visual C++, Java

  4. #4
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527
    this does it without having an array
    Code:
    For i = 0 To Form1.Controls.Count - 1
        If TypeOf Form1.Controls(i) Is CheckBox Then
            If Form1.Controls(i).Value = 0 Then
            Form1.Controls(i).Value = 1
            Else
            Form1.Controls(i).Value = 0
            End If
        End If
    Next i
    it alternates the value of every checkbox on the form

  5. #5
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527
    yet another way:
    Code:
    On Error GoTo ErrHandler:
    For Each CheckBox In Me.Controls
            If CheckBox.Value = 0 Then
            CheckBox.Value = 1
            Else
            CheckBox.Value = 0
            End If
    Next CheckBox
    ErrHandler:
    i think this was along the lines you were thinking, but all of my examples work

    so, how's this one?

  6. #6

    Thread Starter
    Frenzied Member wengang's Avatar
    Join Date
    Mar 2000
    Location
    Beijing, China
    Posts
    1,604
    Thanks a lot David. I never lost confidence in you.

    But, it was the second example that worked for me.

    The third one (for whatever reason) sends out the error 'Object doesn't support this property or method'

    Anyway, i appreciate it. That will make my "Reset" key a lot faster to code in the future.

    Wen Gang, Programmer
    VB6, QB, HTML, ASP, VBScript, Visual C++, Java

  7. #7
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527
    well, i got there in the end,

    i don't know why the third one didn't work, it worked for me

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