Results 1 to 6 of 6

Thread: [RESOLVED] Save checked selections from a listbox?

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    May 2006
    Posts
    2,295

    Resolved [RESOLVED] Save checked selections from a listbox?

    Hi there folks! I am working on a program that uses a listbox with check boxes. It is a program that teaches students how to spell word family words ("at, ate, ot, op, etc".) Anyway, teachers can go in and select from a listbox which word families they want to use. What I am stuck with, is how do I save their selections to a textfile for future reference.

    For example, if a teacher selects 5 of the 38 word families, I would like to save them in a textfile called SAVEDList. Then the next time the teacher runs the program, it would check the textfile, see which word families are listed, and check them automatically, so they don't have to repeat the process.

    Does anyone have any experience working with checkbox listfiles, mine is called UsingList.

    Thanks!

  2. #2
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    Re: Save checked selections from a listbox?

    You can save/read check state in INI file
    vb Code:
    1. Option Explicit
    2.  
    3. Private Declare Function GetPrivateProfileString Lib "kernel32.dll" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
    4. Private Declare Function WritePrivateProfileString Lib "kernel32.dll" Alias "WritePrivateProfileStringA" (ByVal lpSectionName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long
    5.  
    6. Private Sub Form_Load()
    7.     Dim j As Long
    8.     For j = 1 To 35
    9.         List1.AddItem j
    10.     Next
    11.    
    12.     Dim strSelection() As String
    13.     strSelection = Split(ReadValue("SAVEDList", "Selection", vbNullString, App.Path & "\SAVEDList.ini"), ";")
    14.     If UBound(strSelection) > 0 Then
    15.         For j = 0 To UBound(strSelection)
    16.             List1.Selected(j) = CBool(strSelection(j))
    17.         Next
    18.     End If
    19. End Sub
    20.  
    21. Private Sub Form_Unload(Cancel As Integer)
    22.     ' You may move this code to List1_ItemCheck to save instantly.
    23.     Dim j As Long
    24.     Dim strSelection As String
    25.  
    26.     For j = 0 To List1.ListCount - 1
    27.         strSelection = strSelection & ";" & CInt(List1.Selected(j))
    28.     Next
    29.     WriteValue "SAVEDList", "Selection", Mid$(strSelection, 2), App.Path & "\SAVEDList.ini"
    30. End Sub
    31.  
    32. Private Function ReadValue(ByVal strSectionName As String, ByVal strKeyName As String, ByVal strDefaultValue As String, ByVal strFileName As String) As String
    33.     Dim lngBufferSize As Long
    34.     Dim lngLength As Long
    35.     Dim strBuffer As String
    36.     strBuffer = String$(2000, 0)
    37.     lngBufferSize = Len(strBuffer)
    38.     lngLength = GetPrivateProfileString(strSectionName, strKeyName, strDefaultValue, strBuffer, lngBufferSize, strFileName)
    39.     strBuffer = Left$(strBuffer, lngLength)
    40.     ReadValue = strBuffer
    41. End Function
    42.  
    43. Private Sub WriteValue(ByVal strSectionName As String, ByVal strKeyName As String, ByVal strValue As String, ByVal strFileName As String)
    44.     WritePrivateProfileString strSectionName, strKeyName, strValue, strFileName
    45. End Sub



  3. #3
    Fanatic Member Spooman's Avatar
    Join Date
    Mar 2017
    Posts
    868

    Re: Save checked selections from a listbox?

    Justin

    As an alternative, maybe something like this ...

    First, I created and saved an empty text file named "D:\VBForums\SAVEDList.txt"
    Then, on a form, I placed

    Check2(0) .. A CheckBox named Check2, with the Index set to 0, to make it a control array.
    Command1 .. CommandButton to load 9 other CheckBoxes and read the file (assumed empty at first)
    Command2 .. CommandButton that saves "marked" CheckBoxes to the file.

    The first time I ran the app, I checked boxes 2, 7, 8, 9

    Here is the code snippet

    Code:
    Private Sub Command1_Click()
        '
        Check2(0).Top = 1000
        Check2(0).Left = 500
        ' load 9 more
        For ii = 1 To 9
            Load Check2(ii)
            With Check2(ii)
                .Left = 500
                .Top = Check2(ii - 1).Top + Check2(ii - 1).Height
                .Caption = "Check2(" & ii & ")"
                .Visible = True
            End With
        Next ii
        ' read the file
        ' mark "as checked" as applies
        fpath = "D:\VBForums\SAVEDList.txt"
        Open fpath For Input As #1
        Do While Not EOF(1)
            Line Input #1, xtr
            With Check2(CInt(xtr))
                .Value = 1
                .BackColor = vbCyan
            End With
        Loop
        '
    End Sub
    '
    '
    Sub Command2_Click()
        '
        ' save to txt file
        If v = 30 Then
            fpath = "D:\VBForums\SAVEDList.txt"
            Close #1
            Open fpath For Output As #1
            For ii = 0 To 9
                If Check2(ii).Value = 1 Then
                    Write #1, ii
                End If
            Next ii
            Close #1
        End If
        '
    End Sub
    Here is an image when I re-run the app.

    Name:  ckbxtxtfile.png
Views: 215
Size:  6.6 KB

    Here is an image of the text file.

    Name:  ckbxtxtfile2.png
Views: 214
Size:  3.1 KB

    Natch, this is concept only.
    You'll need to modify it to meet your needs.

    Spoo
    Last edited by Spooman; Jul 6th, 2017 at 04:49 PM.

  4. #4
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,940

    Re: Save checked selections from a listbox?

    Hmmm, I was thinking he was wanting something like the following:

    Name:  Image1.gif
Views: 218
Size:  8.3 KB

    It's a List1 listbox with its Style property set to "Checkbox". And then a Command1 button ("Save Data" as caption). And a Command2 button ("Load Data" as caption).

    And then, this code:

    Code:
    
    Option Explicit
    
    Private Sub Command1_Click()
        Dim iFle As Long
        Dim i As Long
        '
        iFle = FreeFile
        Open App.Path & "\SavedData.txt" For Output As iFle
            For i = 0 To List1.ListCount - 1
                If List1.Selected(i) Then Print #iFle, List1.List(i)
            Next i
        Close iFle
    End Sub
    
    Private Sub Command2_Click()
        Dim iFle As Long
        Dim s As String
        Dim i As Long
        '
        ' Un-select everything.
        For i = 0 To List1.ListCount - 1
            List1.Selected(i) = False
        Next i
        List1.ListIndex = -1
        '
        iFle = FreeFile
        Open App.Path & "\SavedData.txt" For Input As iFle
            Do While Not EOF(iFle)
                Line Input #iFle, s
                For i = 0 To List1.ListCount - 1
                    If List1.List(i) = s Then List1.Selected(i) = True
                Next i
            Loop
        Close iFle
    End Sub
    
    Private Sub Form_Load()
        List1.AddItem "Alpha"
        List1.AddItem "Beta"
        List1.AddItem "Gamma"
        List1.AddItem "Delta"
        List1.AddItem "Epsilon"
    End Sub
    
    
    You run the program, make your selections, and save your data. Exit the program, run it again, and load the data, and voila.

    I'm assuming the words are always pre-loaded, as they're not defined by this process.

    Also, the ListBox is doing something funky with it's colors, but I didn't spend the time to suss that out.

    Enjoy,
    Elroy

    EDIT1: Also, just to say it, if it were me doing this, I wouldn't put my data file in with my program, but that was just convenient for the demo. Also, I didn't check for the file's existence on loading the data. If you haven't saved first, you'll get an error.

    EDIT2: I also fixed the little color selection problem. It's the somewhat larger line of code in the above that fixes it.
    Last edited by Elroy; Jul 6th, 2017 at 06:05 PM.
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  5. #5

    Thread Starter
    PowerPoster
    Join Date
    May 2006
    Posts
    2,295

    Re: Save checked selections from a listbox?

    Thank you all for your help on this!!

  6. #6
    Fanatic Member Spooman's Avatar
    Join Date
    Mar 2017
    Posts
    868

    Re: Save checked selections from a listbox?

    Quote Originally Posted by Elroy View Post
    And then, this code:

    Code:
    
    Option Explicit
    
    Private Sub Command1_Click()
        Dim iFle As Long
        Dim i As Long
        '
        iFle = FreeFile
        Open App.Path & "\SavedData.txt" For Output As iFle
            For i = 0 To List1.ListCount - 1
                If List1.Selected(i) Then Print #iFle, List1.List(i)
            Next i
        Close iFle
    End Sub
    
    Private Sub Command2_Click()
        Dim iFle As Long
        Dim s As String
        Dim i As Long
        '
        ' Un-select everything.
        For i = 0 To List1.ListCount - 1
            List1.Selected(i) = False
        Next i
        List1.ListIndex = -1
        '
        iFle = FreeFile
        Open App.Path & "\SavedData.txt" For Input As iFle
            Do While Not EOF(iFle)
                Line Input #iFle, s
                For i = 0 To List1.ListCount - 1
                    If List1.List(i) = s Then List1.Selected(i) = True
                Next i
            Loop
        Close iFle
    End Sub
    
    Private Sub Form_Load()
        List1.AddItem "Alpha"
        List1.AddItem "Beta"
        List1.AddItem "Gamma"
        List1.AddItem "Delta"
        List1.AddItem "Epsilon"
    End Sub
    
    
    EDIT2: I also fixed the little color selection problem. It's the somewhat larger line of code in the above that fixes it.
    FWIW, an alternative way to highlight the code is to use the VE wrapper.
    In this case, I just clicked OK, and it defaults to the yellow background.

    BTW, I've always been impressed by the visual effects of the colors you use in your code posts.
    How do you accomplish that?
    Do you manually edit them line by line?

    EDIT:

    If it's a trade secret, I'll understand ..

    Spoo
    Last edited by Spooman; Jul 9th, 2017 at 07:08 AM.

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