|
-
Aug 25th, 2009, 09:28 AM
#1
Thread Starter
Junior Member
[RESOLVED] Checkbox Listbox......Confused
Afternoon all, got a little query about these checkbox listboxes I've just discovered (a listbox with style option 1 selected). I have one with a big list of checkboxes and I want to be able to write to a file which ones are selected so they can be instantly recalled later, but I'm having trouble referring to each chkbox individually. With my other controls I've just been writing the values to a CSV file:
Code:
Print #1, chk2001.Value & "," & chk2002.Value & "," & chk2003.Value & "," & chk2004.Value _
& "," & chk2005.Value & "," & chk2006.Value & "," & chk2007.Value & "," & chk2008.Value
And reading them back like this:
Code:
Private Sub lstSystems_Click()
sName = lstSystems.Text
Open syDir & "\" & sName & ".txt" For Input As #1
Line Input #1, mLine
Close #1
mVals = Split(mLine, ",")
chk2001.Value = mVals(0)
chk2002.Value = mVals(1)
chk2003.Value = mVals(2)
chk2004.Value = mVals(3)
chk2005.Value = mVals(4)
chk2006.Value = mVals(5)
chk2007.Value = mVals(6)
Anyone have any idea's how I can do this with a chkbox lstbox ?
-
Aug 25th, 2009, 09:35 AM
#2
Re: Checkbox Listbox......Confused
You are showing us checkboxes vs listbox, if you want to use a listbox instead, you can do that a few ways
Code:
' Loop thru your checkbox and write the value
Dim Index As Long
For Index = 0 To List1.ListCount -1
' print to file: List1.Selected(Index) ' which is either true or false
Next
' When reading...
For Index = 0 To UBound(mVals)
List1.Selected(Index) = CBool(mVals(Index))
Next
The above will error if you have more values in the file than items in your listbox
Try this idea if you are still wanting to use checkboxes
Code:
Private Sub lstSystems_Click()
Dim Index As Long
sName = lstSystems.Text
Open syDir & "\" & sName & ".txt" For Input As #1
Line Input #1, mLine
Close #1
mVals = Split(mLine, ",")
For Index = 1 To UBound(mVals)
Me.Controls("chk" & CStr(2000+Index)).Value = CBool(mVals(Index-1))
Next
The above code will error if you have more values written to the file than the number of checkboxes. Also if your checkboxes are not sequentially numbered, error too. If these are real issues, reconsider how you are writing the file so it can be used easier by your importing code.
Last edited by LaVolpe; Aug 25th, 2009 at 09:52 AM.
-
Aug 25th, 2009, 10:20 AM
#3
Thread Starter
Junior Member
Re: Checkbox Listbox......Confused
 Originally Posted by LaVolpe
You are showing us checkboxes vs listbox, if you want to use a listbox instead, you can do that a few ways
Cheers for the reply but actually, I'm using a listbox with checkboxes inside it. If I do:
Code:
MsgBox lstCourse.Selected(x)
It returns 'True', so the value of lstCourse.Selected(x) is what I'll be writing to my file, the problem is when trying to assign the 'True' value from the file I need to do something like:
Code:
lstCourse.ListIndex(x).Selected = True
This obviously doesn't work but It must be something along those lines ?
-
Aug 25th, 2009, 10:31 AM
#4
Re: Checkbox Listbox......Confused
 Originally Posted by NickyG
Code:
lstCourse.ListIndex(x).Selected = True
This obviously doesn't work but It must be something along those lines ?
Look at my previous reply closer. You don't use the ListIndex, you the Selected property.
-
Aug 25th, 2009, 11:36 AM
#5
Thread Starter
Junior Member
Re: Checkbox Listbox......Confused
Solved it, I was testing lstCourse.Selected(3) = True in the form load section and it wasn't working.........because I had it before the code that populates the listbox ! DOH !
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|