-
Jul 6th, 2017, 01:19 PM
#1
Thread Starter
PowerPoster
[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!
-
Jul 6th, 2017, 03:37 PM
#2
Re: Save checked selections from a listbox?
You can save/read check state in INI file
vb Code:
Option Explicit 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 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 Private Sub Form_Load() Dim j As Long For j = 1 To 35 List1.AddItem j Next Dim strSelection() As String strSelection = Split(ReadValue("SAVEDList", "Selection", vbNullString, App.Path & "\SAVEDList.ini"), ";") If UBound(strSelection) > 0 Then For j = 0 To UBound(strSelection) List1.Selected(j) = CBool(strSelection(j)) Next End If End Sub Private Sub Form_Unload(Cancel As Integer) ' You may move this code to List1_ItemCheck to save instantly. Dim j As Long Dim strSelection As String For j = 0 To List1.ListCount - 1 strSelection = strSelection & ";" & CInt(List1.Selected(j)) Next WriteValue "SAVEDList", "Selection", Mid$(strSelection, 2), App.Path & "\SAVEDList.ini" End Sub Private Function ReadValue(ByVal strSectionName As String, ByVal strKeyName As String, ByVal strDefaultValue As String, ByVal strFileName As String) As String Dim lngBufferSize As Long Dim lngLength As Long Dim strBuffer As String strBuffer = String$(2000, 0) lngBufferSize = Len(strBuffer) lngLength = GetPrivateProfileString(strSectionName, strKeyName, strDefaultValue, strBuffer, lngBufferSize, strFileName) strBuffer = Left$(strBuffer, lngLength) ReadValue = strBuffer End Function Private Sub WriteValue(ByVal strSectionName As String, ByVal strKeyName As String, ByVal strValue As String, ByVal strFileName As String) WritePrivateProfileString strSectionName, strKeyName, strValue, strFileName End Sub
-
Jul 6th, 2017, 04:40 PM
#3
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.
Here is an image of the text file.
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.
-
Jul 6th, 2017, 05:54 PM
#4
Re: Save checked selections from a listbox?
Hmmm, I was thinking he was wanting something like the following:
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.
-
Jul 9th, 2017, 12:55 AM
#5
Thread Starter
PowerPoster
Re: Save checked selections from a listbox?
Thank you all for your help on this!!
-
Jul 9th, 2017, 02:09 AM
#6
Re: Save checked selections from a listbox?
Originally Posted by Elroy
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|