Link to home
Start Free TrialLog in
Avatar of TCPIP2600
TCPIP2600

asked on

check boxes in list control

ok this must be easy.... but for some reason im doing something wrong. I have some checkboxes in a list control (one of the properties), but i dont know how to automatically turn them on and off.
ASKER CERTIFIED SOLUTION
Avatar of PatrickVD
PatrickVD

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You cannot set the Check from code, and the only way to get the current checked status of a list entry is to monitor all the ItemCheck events.

The Selected property only tells you if the item is selected, not whether its checked or not.

If you want to programatically set the state of the Check box you should use the listview control instead. Slightly more complicated to use, but not too heavy on the old brain cells.

Gordon.
Avatar of PatrickVD
PatrickVD

Sorry Gordon... but I think you're mistaking....

If you use a standard listbox and set its Style property to 'Check Box (Value = 1) the Selected property of the listbox will automatically reflect the state of the checkmark. If you set it to True it will be checled, and the checkmark will be unchecked if you set its value to False....
This means that you can set the state of the checkbox programmatically !

To proove you just try the following :

Create a standard exe in VB6, add a listbox List1 to the form and a Command button Command1.
No copy and paste this code in your form's code window and run it....
When its running in debug mode, try to click around on the listbox items by changing the state of the checkboxes... and press the Command1 on each change...


---CODE TO COPY

Private Sub Command1_Click()

    Dim strSelected As String
    Dim i As Integer
   
    For i = 0 To (List1.ListCount - 1)

        If List1.Selected(i) Then
            strSelected = strSelected & "Item Selected = " & CStr(i) & vbCrLf
        End If

    Next i

    Debug.Print strSelected

End Sub

Private Sub Form_Load()
    'Just create some entries in the listbox to have it filled...
    List1.AddItem "Checked"
    List1.Selected(List1.NewIndex) = True
    List1.AddItem "Not Checked"
    List1.AddItem "Checked2"
    List1.Selected(List1.NewIndex) = True
    List1.AddItem "Not Checked"
    List1.AddItem "Checked"
    List1.Selected(List1.NewIndex) = True
    List1.AddItem "Not Checked"
    List1.AddItem "Checked2"
    List1.Selected(List1.NewIndex) = True
    List1.AddItem "Not Checked"
    List1.AddItem "Checked"
    List1.Selected(List1.NewIndex) = True
    List1.AddItem "Not Checked"
    List1.AddItem "Checked2"
    List1.Selected(List1.NewIndex) = True
    List1.AddItem "Not Checked"
   

End Sub

---- END OF CODE

Try this and you'll see that it works...

Cheers,

Patrick.


Well, my mistake, and boy am I pissed off. So many things could have been so much simpler.

Gordon
Well, my mistake, and boy am I pissed off. So many things could have been so much simpler.

Gordon
Well, my mistake, and boy am I pissed off. So many things could have been so much simpler.

Gordon
Well, my mistake, and boy am I pissed off. So many things could have been so much simpler.

Gordon