Consulting

Results 1 to 9 of 9

Thread: Select - deselect chkboxes

  1. #1

    Arrow Select - deselect chkboxes

    I hv 5 chkboxes on my form.

    Apart from 4, 1 is for selecting or deselecting all the rest 4 chkboxes.

    If the value of this is true, select all else select none.

    Also if i select any of the other 4 chkbox then the select-all chkbox should be reflected.

    Means, if the value of any of the 4 chkbox is false then set select-all chkbox as false.

    but keep the values of the other 3 same.

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Option Explicit 
    
    Private mEnableEvents As Boolean
    
    Private Sub CheckBox1_Click()
        With Me
            If mEnableEvents Then
                mEnableEvents = False
                If .CheckBox1.Value Then
                    .CheckBox2.Value = True
                    .CheckBox3.Value = True
                    .CheckBox4.Value = True
                    .CheckBox5.Value = True
                Else
                    .CheckBox2.Value = False
                    .CheckBox3.Value = False
                    .CheckBox4.Value = False
                    .CheckBox5.Value = False
                End If
                mEnableEvents = True
                Me.Repaint
            End If
        End With
    End Sub
    
    Private Sub CheckBox2_Click()
        With Me
            If mEnableEvents Then
                mEnableEvents = False
                If Not .CheckBox2.Value Then
                    .CheckBox1.Value = False
                ElseIf .CheckBox3.Value And .CheckBox4.Value And .CheckBox5.Value Then
                    .CheckBox1.Value = True
                End If
                mEnableEvents = True
            End If
        End With
    End Sub
    
    Private Sub CheckBox3_Click()
        With Me
            If mEnableEvents Then
                mEnableEvents = False
                If Not .CheckBox3.Value Then
                    .CheckBox1.Value = False
                ElseIf .CheckBox2.Value And .CheckBox4.Value And .CheckBox5.Value Then
                    .CheckBox1.Value = True
                End If
                mEnableEvents = True
            End If
        End With
    End Sub
    
    Private Sub CheckBox4_Click()
        With Me
            If mEnableEvents Then
                mEnableEvents = False
                If Not .CheckBox4.Value Then
                    .CheckBox1.Value = False
                ElseIf .CheckBox2.Value And .CheckBox3.Value And .CheckBox5.Value Then
                    .CheckBox1.Value = True
                End If
                mEnableEvents = True
            End If
        End With
    End Sub
    
    Private Sub CheckBox5_Click()
        With Me
            If mEnableEvents Then
                mEnableEvents = False
                If Not .CheckBox5.Value Then
                    .CheckBox1.Value = False
                ElseIf .CheckBox2.Value And .CheckBox3.Value And .CheckBox4.Value Then
                    .CheckBox1.Value = True
                End If
                mEnableEvents = True
            End If
        End With
    End Sub
    
    Private Sub UserForm_Activate()
        mEnableEvents = True
    End Sub

  3. #3
    VBAX Contributor Richie(UK)'s Avatar
    Joined
    May 2004
    Location
    UK
    Posts
    188
    Location
    Hi xld,

    Do we need the second If statement for Checkbox1? Could we not use:

    Private Sub CheckBox1_Click()
        With Me
            If mEnableEvents Then
                mEnableEvents = False
                .CheckBox2.Value = .CheckBox1.Value
                .CheckBox3.Value = .CheckBox1.Value
                .CheckBox4.Value = .CheckBox1.Value
                .CheckBox5.Value = .CheckBox1.Value
                mEnableEvents = True
                Me.Repaint
            End If
        End With
    End Sub

    Nothing wrong with the functionality of the original, this just seems a little more intuitive. What do you think?

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Hi Richie,

    Quote Originally Posted by Richie(UK)
    Nothing wrong with the functionality of the original, this just seems a little more intuitive. What do you think?
    My view, for what it is worth, is that whilst I don't think it is more intuitive, I do feel it is tighter, which makes it better.

    BTW the repaint isn't necessary. I put it in when testing and one part didn't work, but it wasn't the cause or the solution. Thus it should be removed because it is a performance drag.

    It c an be simplified from a maintenance point as well by

    Option Explicit 
     
    Private mEnableEvents As Boolean
     
    Private Sub CheckBox1_Click()
    With Me
    If mEnableEvents Then
    mEnableEvents = False
    mEnableEvents = False
    .CheckBox2.Value = .CheckBox1.Value
    .CheckBox3.Value = .CheckBox1.Value
    .CheckBox4.Value = .CheckBox1.Value
    .CheckBox5.Value = .CheckBox1.Value
    mEnableEvents = True
    mEnableEvents = True
    End If
    End With
    End Sub
     
    Private Sub CheckBox2_Click()
    With Me
    If mEnableEvents Then
    SetCheckboxes .CheckBox2
    End If
    End With
    End Sub
     
    Private Sub CheckBox3_Click()
    With Me
    If mEnableEvents Then
    SetCheckboxes .CheckBox3
    End If
    End With
    End Sub
     
    Private Sub CheckBox4_Click()
    With Me
    If mEnableEvents Then
    SetCheckboxes .CheckBox4
    End If
    End With
    End Sub
     
    Private Sub CheckBox5_Click()
    With Me
    If mEnableEvents Then
    SetCheckboxes .CheckBox5
    End If
    End With
    End Sub
     
    Private Sub UserForm_Activate()
    mEnableEvents = True
    End Sub
     
    Private Sub SetCheckboxes(thisCb As MSForms.CheckBox)
        With Me
            mEnableEvents = False
            If Not thisCb.Value Then
                .CheckBox1.Value = False
            ElseIf .CheckBox2.Value And .CheckBox3.Value And _
                    .CheckBox4.Value And .CheckBox5.Value Then
                .CheckBox1.Value = True
            End If
            mEnableEvents = True
        End With
    End Sub

  5. #5
    xld,

    what was the different between your first posted code and this new one??

    both seems to be working fine.

  6. #6
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by ilyaskazi
    what was the different between your first posted code and this new one??
    Two things.

    I incorporated Richie's suggestions.

    I used a separate procedure to do the setting/unsetting, rather than do it in each checkbox event. In the checkbox event code I pass the checkbox object to that shared procedure.

    That is from


    Private Sub CheckBox2_Click()
    With Me
    If mEnableEvents Then
    mEnableEvents = False
    If Not .CheckBox2.Value Then
    .CheckBox1.Value = False
    ElseIf .CheckBox3.Value And .CheckBox4.Value And .CheckBox5.Value Then
    .CheckBox1.Value = True
    End If
    mEnableEvents = True
    End If
    End With
    End Sub
    to

    Private Sub CheckBox2_Click()
    With Me
    If mEnableEvents Then
    SetCheckboxes .CheckBox2
    End If
    End With
    End Sub
    and the extra procedure.

    The advantage to this is that if you want to change what happens, you only have to do it in one place, not every checkbox event.

    Also, see the post with stapuff on usefrom pseudo control arrays http://www.vbaexpress.com/forum/show...?t=3335&page=2

  7. #7
    alright, thanku.

    I hv used the next code and working perfect.

  8. #8
    xld,

    is it possible if any of this chkbox value is true then enable the "START" button else disable.

  9. #9
    ilyaskazi vbmenu_register("postmenu_28695", true);

    A suggestion for you (that I use). Do not show the "Start" button until a checkbox is selected.

    On userform initialize -
    set the buttons visible to false then add a 1 liner to xld's code that shows the button.

    Me.CommandButton1.Visible = False
     
     
    If mEnableEvents Then 
            SetCheckboxes .CheckBox2 
            Me.CommandButton1.Visible = True
        End If 


    Kurt



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •