Results 1 to 5 of 5

Thread: [RESOLVED] Checkbox Listbox......Confused

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jul 2009
    Posts
    24

    Resolved [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 ?

  2. #2
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    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.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jul 2009
    Posts
    24

    Re: Checkbox Listbox......Confused

    Quote Originally Posted by LaVolpe View Post
    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 ?

  4. #4
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Checkbox Listbox......Confused

    Quote Originally Posted by NickyG View Post
    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.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Jul 2009
    Posts
    24

    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
  •  



Click Here to Expand Forum to Full Width