-
Sep 18th, 2017, 11:04 PM
#1
Thread Starter
PowerPoster
How to load a textfile into the first column of a listview?
Hi there folks. I am working on a program to keep track of student marks for report cards. I am using a listview, and have it set up the way I want it.
The first column will have the student names. I have a behavior tracking program that keeps their names in a textfile, and I would like to use that textfile. I am used to loading a textfile into a listbox, but this time I want to load them as the list items for the first column of the listview.
This is what I have so far.
VB Code:
'Set up the listview
lvwbook.View = lvwReport
lvwbook.GridLines = True
Dim li As ListItem
With lvwbook
.ColumnHeaders.Add , , "Student Name"
Set li = .ListItems.Add(, , "Row1Item1")
End With
So in this case of just adding Row1Item1, I would like to load the textfile Roster6.txt
Would anyone know how I can do that?
Thanks!!
-
Sep 19th, 2017, 05:15 AM
#2
Re: How to load a textfile into the first column of a listview?
Last edited by Spooman; Sep 19th, 2017 at 06:14 AM.
-
Sep 19th, 2017, 06:12 PM
#3
Thread Starter
PowerPoster
Re: How to load a textfile into the first column of a listview?
Originally Posted by Spooman
Justin
You've got most of it .. well presented ..
Just need the "populate from text file" bit
Maybe something like this..
Code:
'Set up the listview
lvwbook.View = lvwReport
lvwbook.GridLines = True
Dim li As ListItem
With lvwbook
.ColumnHeaders.Add , , "Student Name"
'
' Populate col 1 with Roster6.txt
fpath = "D:\VBForums\Roster6.txt"
Open fpath For Input As #1
Do While Not EOF(1)
Line Input #1, xtr
Set li = .ListItems.Add(, , xtr)
Loop
Close #1
End With
(where xtr is the "extracted" line from the text file)
(I modified this line from your code snippet)
.. and you get this
BTW, I added a few extra cols .. for demo purposes
I imagine you can do that bit.
EDIT-1.
FWIW, here is what the text file looks like
EDIT-2:
I wasn't aware of the Gridlines property, but they are really hard to see.
Sadly, I don't off-hand see a way to darken them.
HTH
Spoo
Thank you so much that works great!
Sorry to bother you, and this is not technically related, but .....
So the program will randomly pick a student from the same Roster6.txt file to ask a question. I want to keep their work for marking. The answer they give is saved in a StudentAnsLBL label caption. The student who is randomly picked is saved in StudentPickLBL.caption. I am unsure how to do this, but I want to search the first column of the listview now that the student names are in the column using the caption in StudentPickLBL. When a match is found, the caption of StudentAnsLBL is added on the same row as their name.
Earlier, I learned that I can add things to rows, but the trick is getting the new data on the same row as the student name. I hope to do this by finding a match between the listview first column, and the StudentPickLBL.caption which has the specific student's name.
Would you know how to do that? : )Thanks a ton!
-
Sep 20th, 2017, 04:31 AM
#4
Re: How to load a textfile into the first column of a listview?
Justin
Glad you got issue 1 working.
As for issue 2, let me see if I understand your needs ..
- your app randomly picks a student
- the pick is saved in StudentPickLBL.Caption .. eg, Wellin, Stephanie
- the answer is saved in StudentAnsLBL.Caption .. eg .. George Washington
- search ListView col-1, find Wellin, Stephanie, post answer in col 2
Seems like you'll need to ..
1. add col-2 (if not already present)
2. give col-2 header a "name" (if desired)
3. set the width of col-2,
4. loop thru col-1 names and find match with StudentPickLBL.Caption
5. "post" in col-2 StudentAnsLBL.Caption
Does that get you started?
Holler if you need more.
Spoo
Last edited by Spooman; Sep 20th, 2017 at 04:37 AM.
-
Sep 20th, 2017, 12:34 PM
#5
Re: How to load a textfile into the first column of a listview?
Justin
OK, I'll do some hollering ..
It was a little more obtuse than I expected, but I came up with something.
If you want to try on your own first, avert your eyes
I've posted the code below the image
Here is an image
And here's the code
Code:
Sub Command2_click()
'
Dim li As ListItem
With ListView1
' Post Answer
For ii = 1 To .ListItems.Count
b = .ListItems.Item(ii)
If b = Label1.Caption Then ' StudentPickLBL
Set li = ListView1.ListItems.Item(ii)
li.SubItems(1) = Label2(0).Caption ' StudentAnsLBL
Exit For
End If
Next ii
End With
'
End Sub
The names of my labels are different that yours .. I've noted the relationships above.
HTH
Spoo
Last edited by Spooman; Sep 20th, 2017 at 02:33 PM.
-
Sep 20th, 2017, 06:12 PM
#6
Thread Starter
PowerPoster
Re: How to load a textfile into the first column of a listview?
Thank you again Spoo!
I am using your code now, but I must have left something out.
I am getting an 'element not found" error and it highlights this line
b = .ListItems.Item(ii)
Would that mean that the code isn't able to find a match between the StudentPickLBL.caption and the first column of the listview with all of the student names?
Thank you!
-
Sep 20th, 2017, 08:30 PM
#7
Re: How to load a textfile into the first column of a listview?
No it means that you probably did something wrong and ii has a value that does not correspond to a valid index number within the listview.
You should show the code you are using, exactly as it is in your program. The problem should be easy to spot.
The only issue I see with the code posted above is that ii is not defined.
-
Sep 21st, 2017, 03:15 AM
#8
Re: How to load a textfile into the first column of a listview?
Justin
In addition to DM's suggestions, put a break-point on that line.
What is the value of
Spoo
Last edited by Spooman; Sep 21st, 2017 at 03:18 AM.
-
Sep 21st, 2017, 03:16 AM
#9
Thread Starter
PowerPoster
Re: How to load a textfile into the first column of a listview?
Sure, my code is pretty much the same as Spooman, but with these differences.
Vb Code:
' Dim li As ListItem With lvwbook ' Post Answer For i = 1 To lvwbook.ListItems.Count b = lvwbook.ListItems.Item(ii) If b = MapForm.StudentPickLBL.Caption Then ' StudentPickLBL Set li = lvwbook.ListItems.Item(ii) li.SubItems(1) = MapForm.StudentAnsLBL.Caption ' StudentAnsLBL Exit For End If Next i End With '[/B]
-
Sep 21st, 2017, 03:19 AM
#10
Thread Starter
PowerPoster
Re: How to load a textfile into the first column of a listview?
Sure, is that when you put a red dot on the left side of code?
-
Sep 21st, 2017, 03:20 AM
#11
Re: How to load a textfile into the first column of a listview?
Yup
Then move your mouse over the variable and the value should pop up in a little yellow box
-
Sep 21st, 2017, 03:32 AM
#12
Thread Starter
PowerPoster
Re: How to load a textfile into the first column of a listview?
Ok , it says i = empty.
-
Sep 21st, 2017, 04:10 AM
#13
Re: How to load a textfile into the first column of a listview?
Justin
OK, this may be the issue
Code:
'
Dim li As ListItem
With lvwbook
' Post Answer
For i = 1 To lvwbook.ListItems.Count
b = lvwbook.ListItems.Item(ii)
If b = MapForm.StudentPickLBL.Caption Then ' StudentPickLBL
Set li = lvwbook.ListItems.Item(ii)
li.SubItems(1) = MapForm.StudentAnsLBL.Caption ' StudentAnsLBL
Exit For
End If
Next i
End With
Your loop is based on i
But, your index is based on ii
Pick one or the other !!
EDIT-1
If you check my post #5, you'll see that ii was used in both cases.
While most people use i for counters (single letter), my inclination is to use ii (double letters).
Neither is better than the other, but consistence counts ..
Spoo
Last edited by Spooman; Sep 21st, 2017 at 04:14 AM.
-
Sep 21st, 2017, 04:35 AM
#14
Thread Starter
PowerPoster
Re: How to load a textfile into the first column of a listview?
Oh you are awesome! That is working now.
The last thing I am working on is this...
The same listview has 14 columns. The first column has the student names. The other 13 are all of the provinces and territory names in Canada. When a student answers a question, it saves the answer by their name on the correct row, but is there a way to search the column header names, ex: NB, PEI, ONT, etc for a match between the StudentAnsLBL.caption and the column header? That way, it will put it their answer on the correct row under the correct column? The caption in StudentAnsLBL and the column will be the same if they answered the question correctly, so under example Northwest Territories (NWT), if they get the question right, StudentAnsLBL will have NWT in the caption. Do you know where I could add it to look through the columns for a match? I already have the columns set up at form_load to have the names in the column headers.
Thanks again for all your help!
-
Sep 21st, 2017, 05:01 AM
#15
Re: How to load a textfile into the first column of a listview?
Justin
I think I get what you want, but I gotta run for a bit.
In the meantime, could you list the 13 abbreviations you use for the provinces?
Spoo
-
Sep 21st, 2017, 05:42 AM
#16
Thread Starter
PowerPoster
Re: How to load a textfile into the first column of a listview?
Originally Posted by Spooman
Justin
I think I get what you want, but I gotta run for a bit.
In the meantime, could you list the 13 abbreviations you use for the provinces?
Spoo
Absolutely! : )
AB
BC
MB
NB
NL
NT
NS
NU
ON
PE
QC
SK
YT
-
Sep 21st, 2017, 02:48 PM
#17
Re: How to load a textfile into the first column of a listview?
Justin
OK, seems that it is similar .. just need a nested loop ..
For kk = 1 To .ColumnHeaders.Count .. Next kk
Code:
Sub Command2_click()
'
Dim li As ListItem
With ListView1
' Post Answer
For ii = 1 To .ListItems.Count
b = .ListItems.Item(ii)
If b = Label1.Caption Then ' StudentPickLBL
' find col
For kk = 1 To .ColumnHeaders.Count
c = .ColumnHeaders.Item(kk)
If c = Label2(0).Caption Then ' StudentAnsLBL
Exit For
End If
Next kk
'
Set li = ListView1.ListItems.Item(ii)
li.SubItems(kk - 1) = Label2(0).Caption ' StudentAnsLBL
Exit For
End If
Next ii
End With
'
End Sub
.. and you get this
BTW, I put your list of 13 into a text file, and used that to populate the ColumnHeaders.
I imagine you've got that bit done already.
HTH
Spoo
Last edited by Spooman; Sep 21st, 2017 at 02:52 PM.
-
Sep 21st, 2017, 10:47 PM
#18
Thread Starter
PowerPoster
Re: How to load a textfile into the first column of a listview?
Thank you so much for your help! This is soo cool!
I hate to bother you again, I am very happy with the way this is working out. I wonder if there is a way to check to see when a student's name is called, instead of calling it from a listbox which is what I do now, is there a way to choose the student from the first column of the listview where the students names are?
The reason I ask, is I also have their questions in a listbox, which I use like this...
VB Code:
Dim lIndex As Long If QList.ListCount Then lIndex = Int(Rnd * QList.ListCount) QuestionLBL.Caption = QList.List(lIndex) 'QList.RemoveItem lIndex End If
Qlist is my Question listbox. It has all of the 13 territories and provinces, but rather than ask the same question over and over for each student. I would like it so when a student name is called, check the column headers for a row item that isn't filled in yet for that student, that way, they only get the questions they havent answered yet. Does that make sense?
Is there a way to check for empty rowitems and then pick a random empty one and put that in QuestionLBL.caption?
Thank you soooo much!!
-
Sep 22nd, 2017, 05:54 AM
#19
Re: How to load a textfile into the first column of a listview?
Justin
I think something like this might do the trick.
Let's say Stephanie has answered some questions ..
I modified your Rnd snippet to accomplish that
Code:
With ListView1
' Post Answer
For ii = 1 To .ListItems.Count
b = .ListItems.Item(ii)
If b = Label1.Caption Then ' StudentPickLBL
Set li = ListView1.ListItems.Item(ii)
' generate random question
rr = Int(Rnd * 13) ' preset to 13
q = li.SubItems(rr)
Label2(1).Caption = aaQList(rr) ' put in cyan label
' is New question
If q = Empty Then
li.SubItems(rr) = aaQList(rr) ' Qlist
' has been Asked
Else
b = b
End If
Exit For
End If
Next ii
End With
Comments
- I modified my code such that repeated clicking of Command2 would trigger the above snippet
- I created an array aaQList to match your QList, and populated it with the 13 choices
- A random number was generated as you did (I had to preset it to 13)
- The result is posted in the cyan Label2(1)
- If not previously encountered, then the ListView is populated .. MB, NT, NS, ON
- If it WAS previously used, then the break-point is hit
- That is what happened when I did the screen-shot
- MB was the new randomly selected item (in cyan label), but it already is in ListView.
I imagine that you can make the necessary modifications.
Spoo
-
Sep 22nd, 2017, 08:01 PM
#20
Thread Starter
PowerPoster
Re: How to load a textfile into the first column of a listview?
Thank you very much Spoo. I think I messed up somewhere though. I get a "wrong number of arguments or invalid property assignment" error. It then highlights Public Sub Command2_Click() in yellow and highlights aaQList in blue.
You mentioned you made an array called aaQList and put the 13 choices in there. I am having trouble making an array, could you give me the code you made your array with? : )
-
Sep 23rd, 2017, 03:23 AM
#21
Re: How to load a textfile into the first column of a listview?
Justin
To create and populate aaQList I did the following
Code:
' declarations
Dim aaQList(13) As String
' the sub
Private Sub Command1_Click()
'
fpath = "D:\VBForums\JustinPROV.txt"
Open fpath For Input As #1
nn = 1
Do While Not EOF(1)
Line Input #1, xtr
aaQList(nn) = xtr
nn = nn + 1
Loop
Close #1
'
End Sub
A few comments
- The declarations "region" is the portion of your code above your 1st sub
- The statement Dim aaQList(13) As String does several things
- it creates the array
- it sets the "type" to String
- it makes it "public" to all subs, functions, etc on your Form
- it presets it to have 14 elements (arrays are "numbered" from 0 to 13 .. see pic below)
- that is, they are typically considered "0-based"
- To populate it
- I copied the list from your post #16 into NotePad
- The fact that you listed them vertically made life earier ..
- I initiate nn = 1 .. this creates a "1-based" array .. a little easier to put your arms around
Here is what it looks like in the Locals Window
Hope that gets you going again.
BTW, your question prompted me to do a little more testing.
I found 3 errors on my part in this code frag
Code:
' generate random question
rr = Int(Rnd * 13) ' preset to 13
q = li.SubItems(rr)
- rr can possibly = 0, which will throw an error on the next line
- rr can NOT = 13
- I had not issued a Randomize statement .. I got the same results each time I tried it ..
So, there are two possible solutions to deal with the first 2 issues:
Code:
' generate random question
rr = Int(Rnd * 14) ' preset to 13
rr = IIf(rr = 0, 1, rr) ' trap for 0
q = li.SubItems(rr)
or
Code:
' generate random question
lob = 1 ' lower bounds
upb = 13 ' upper bounds
rr = Int((upb - lob + 1) * Rnd + lob)
q = li.SubItems(rr)
HTH
Spoo
-
Sep 23rd, 2017, 07:06 PM
#22
Thread Starter
PowerPoster
Re: How to load a textfile into the first column of a listview?
Thank you Spoo. I am not getting any errors when I press the Command2 to pick a question. However, the only thing I can't figure out is I am not getting a question in cyan label caption. It's cyan but blank.
At first I thought it was my test.txt loading into the array being empty, but I just checked and the 13 abbreviations are there in the actual textfile.
Edit. I just did a line break on..
Label1.Caption = aaQList(rr) ' put in cyan label
The yellow box tells me on that line that aaQList(rr) = "", so would that mean it didn't pick the province?
Edit2: Wait nevermind lol, I forgot to call the sub to load the questions into the array. Ok, now the Command2 will load a question. I am wondering though. The array will call a question, but seems to automatically put the question answer in the column row for the student. That is definitely what I am looking for if the student answers the question correctly. For example, when the student gets the answer correct, the same abbreviation will show up in the StudentAnsLBL.caption and the label1.caption, and if THAT is the case, then I would like to put it in the row for that student.
So what I did about that is add if the StudentAnsLBL.caption and the label1.caption is the same, then add it to the listview column row spot for that student.
Like this..
If MapForm.StudentAnsLBL.Caption = Label1.Caption Then
So my command2 looks like this now.
VB Code:
Public Sub Command2_Click()
'Dim aaQList(13) As String
With Lvwbook
' Post Answer
For ii = 1 To .ListItems.Count
b = .ListItems.Item(ii)
If b = MapForm.StudentPickLBL.Caption Then ' StudentPickLBL
Set li = Lvwbook.ListItems.Item(ii)
' generate random question
rr = Int(Rnd * 14) ' preset to 13
rr = IIf(rr = 0, 1, rr) ' trap for 0
q = li.SubItems(rr)
Label1.Caption = aaQList(rr) ' put in cyan label
' is New question
If q = Empty Then
If MapForm.StudentAnsLBL.Caption = Label1.Caption Then
li.SubItems(rr) = aaQList(rr) ' Qlist
' has been Asked
End If
Else
b = b
End If
Exit For
End If
Next ii
End With
End Sub
The only issue I have now is it appears the array of questions has duplicates in it, or isn't able to see what the student has already answered because if I click on command2 enough, eventually I will see some repeats.
Last edited by Justin M; Sep 23rd, 2017 at 07:50 PM.
-
Sep 24th, 2017, 04:13 AM
#23
Re: How to load a textfile into the first column of a listview?
Justin
If I understand properly, it seems that you might be hitting this branch
Code:
' is New question
If q = Empty Then
If MapForm.StudentAnsLBL.Caption = Label1.Caption Then
li.SubItems(rr) = aaQList(rr) ' Qlist
End If
' has been Asked
Else
b = b
End If
You can put a break-point to confirm.
If that is so, then you've hit the spot that I did not know what you wanted to do
In your post #18, you had the equivalent line commented out
Code:
Dim lIndex As Long
If QList.ListCount Then
lIndex = Int(Rnd * QList.ListCount)
QuestionLBL.Caption = QList.List(lIndex)
' QList.RemoveItem lIndex
End If
.. so I left that branch unfinished as I assumed that you had a plan in mind.
If you still have an issue with that, describe what you want to do, and I'll take a look.
Actually, now that I look at it further, maybe just moving the Exit For statement will do the trick
Code:
With Lvwbook
' Post Answer
For ii = 1 To .ListItems.Count
b = .ListItems.Item(ii)
If b = MapForm.StudentPickLBL.Caption Then ' StudentPickLBL
Set li = Lvwbook.ListItems.Item(ii)
' generate random question
rr = Int(Rnd * 14) ' preset to 13
rr = IIf(rr = 0, 1, rr) ' trap for 0
q = li.SubItems(rr)
Label1.Caption = aaQList(rr) ' put in cyan label
' is New question
If q = Empty Then
If MapForm.StudentAnsLBL.Caption = Label1.Caption Then
li.SubItems(rr) = aaQList(rr) ' Qlist
Exit For
End If
' has been Asked
Else
b = b
End If
End If
Next ii
End With
The loop will still be exited if it is a new question.
But, if the line b = b is encountered, the Loop will continue, generating another random question.
Let me know if that does the trick.
EDIT-1.
Oops, something just occurred to me.
There is the distinct possibility that you will encounter an endless-loop !!
Let me tinker with this a bit .. I have a potential solution in mind.
Spoo
Last edited by Spooman; Sep 24th, 2017 at 04:28 AM.
-
Sep 24th, 2017, 05:36 AM
#24
Thread Starter
PowerPoster
Re: How to load a textfile into the first column of a listview?
Hi there Spoo! Yes! I totally forgot about that line of code commented out. Yes, just to review things quickly in the classroom, I would like it if once a student answers a question like NB, then they wouldn't get that question again.
Thanks a ton!
-
Sep 24th, 2017, 06:34 AM
#25
Re: How to load a textfile into the first column of a listview?
Justin
You're up early ..
I'm now really glad I added my EDIT-1 to alert you.
Your input confirms my thinking, so here goes as a possible solution.
Note that it is a modification of my code.
I see you have been adjusting it to meet your needs (which is just fine)
So, you'll need to be careful (re the endless loop issue), but I trust you to do so.
Code:
With ListView1
' Post Answer
For ii = 1 To .ListItems.Count
b = .ListItems.Item(ii)
If b = Label1.Caption Then ' StudentPickLBL
Set li = ListView1.ListItems.Item(ii)
' count answered
ans = 0
For aa = 1 To 13
zz = li.SubItems(aa)
ans = ans + IIf(li.SubItems(aa) <> Empty, 1, 0)
Next aa
' Done .. all answered .. PREVENT endless loop
If ans = 13 Then
With Label2(0)
.Caption = "Done" ' put in other label
.Width = 800
.BackColor = RGB(255, 180, 180) ' reddish
End With
Exit For ' ALL answered
' generate random question
Else
Do
Randomize
rr = Int(Rnd * 14) ' preset to 13
rr = IIf(rr = 0, 1, rr) ' trap for 0
q = li.SubItems(rr)
Label2(1).Caption = aaQList(rr) ' put in cyan label
' is New question
If q = Empty Then
li.SubItems(rr) = aaQList(rr) ' StudentAnsLBL
Exit Do
' has been Asked
Else
b = b ' LOOP again
End If
Loop
End If
End If
Next ii
End With
Comments
- My changes in the previous post did NOT solve the issue of repeated "asks" of same question
- To do that, I've added a Do .. Loop, and moved Randomize into it.
- Now, each Command2 click will produce a "new" answer
- However, to prevent the endless loop issue, I also added a "Done" test
- this involves the count answered step
- the Do .. Loop is NOT entered if ALL 13 have been answered
- In the event that all 13 have been entered
- the last cyan entry is preserved
- the label above that is set to Done and the backcolor is set to red
The image below captures that Done condition.
HTH
Spoo
Last edited by Spooman; Sep 24th, 2017 at 06:37 AM.
-
Sep 25th, 2017, 05:09 PM
#26
Thread Starter
PowerPoster
Re: How to load a textfile into the first column of a listview?
This is great! Thank you! My last question is since it will add the abbreviation automatically once it picks the question. Is there a way to have the caption of label1 compared to the caption of StudentAnsLBL.caption and if they are equal/same, then add it to the listview. And if the student gets the question incorrect, leave it blank, and move onto the next question?
Does that make sense?
-
Sep 26th, 2017, 02:16 AM
#27
Re: How to load a textfile into the first column of a listview?
No, not really
My confusion arises from the fact that the names of my objects (Label1, etc) are different from yours.
Could you post a code snippet and a screenshot of yours .. that should clarify things
Spoo
Last edited by Spooman; Sep 26th, 2017 at 02:19 AM.
-
Sep 26th, 2017, 05:54 AM
#28
Thread Starter
PowerPoster
Re: How to load a textfile into the first column of a listview?
VB Code:
Public Sub CmdPickQ_Click() With Lvwbook ' Post Answer For ii = 1 To .ListItems.Count b = .ListItems.Item(ii) If b = MapForm.StudentPickLBL.Caption Then ' StudentPickLBL Set Li = Lvwbook.ListItems.Item(ii) ' count answered ans = 0 For aa = 1 To 13 zz = Li.SubItems(aa) ans = ans + IIf(Li.SubItems(aa) <> Empty, 1, 0) Next aa ' Done .. all answered .. PREVENT endless loop If ans = 13 Then With Label2 .Caption = "Done" ' put in other label .Width = 800 .BackColor = RGB(255, 180, 180) ' reddish End With Exit For ' ALL answered ' generate random question Else Do Randomize rr = Int(Rnd * 14) ' preset to 13 rr = IIf(rr = 0, 1, rr) ' trap for 0 q = Li.SubItems(rr) Label1.Caption = aaQList(rr) ' put in cyan label ' is New question If q = Empty Then Li.SubItems(rr) = aaQList(rr) ' StudentAnsLBL Exit Do ' has been Asked Else b = b ' LOOP again End If Loop End If End If Next ii End With End Sub
So I think in this case label 1 which is the cyan one, has the question to ask the students after it picks one of the questions they haven't done yet. But this line..
Li.SubItems(rr) = aaQList(rr) ' StudentAnsLBL
Is that where the answer is put in the listview? If so, could it be changed so it checks to see if the student has the same answer in StudentAnsLBL.caption, and if it is the same answer/ the correct answer, then put it in the listivew row?
Thanks!
-
Sep 26th, 2017, 08:15 AM
#29
Re: How to load a textfile into the first column of a listview?
OK, just to be sure
Code:
' is New question
If q = Empty Then
Li.SubItems(rr) = aaQList(rr) ' StudentAnsLBL
Exit Do
' has been Asked
Else
b = b ' LOOP again
End If
.. you're talking about this line, right?
If so, then ..
Yes .. that's "where the answer is put in the listview"
To do this
If so, could it be changed so it checks to see if the student has the same answer in StudentAnsLBL.caption, and if it is the same answer/ the correct answer, then put it in the listivew row?
.. I suppose you could add a branch something like this
Code:
' is New question
If q = Empty Then
' "same answer check" branch
If aaQList(rr) = StudentAnsLBL Then
Li.SubItems(rr) = aaQList(rr)
Exit Do
Else
z = z ' do something else here, or do nothing
End If
' has been Asked
Else
b = b ' LOOP again
End If
.. but I'm not sure.
A little trial and error on your part seems to be needed.
Spoo
-
Sep 26th, 2017, 11:46 AM
#30
Thread Starter
PowerPoster
Re: How to load a textfile into the first column of a listview?
Awesome, thank you! I am wondering if I did something wrong. My code now looks like...
VB Code:
Public Sub CmdPickQ_Click()
With Lvwbook
' Post Answer
For ii = 1 To .ListItems.Count
b = .ListItems.Item(ii)
If b = MapForm.StudentPickLBL.Caption Then ' StudentPickLBL
Set Li = Lvwbook.ListItems.Item(ii)
' count answered
ans = 0
For aa = 1 To 13
zz = Li.SubItems(aa)
ans = ans + IIf(Li.SubItems(aa) <> Empty, 1, 0)
Next aa
' Done .. all answered .. PREVENT endless loop
If ans = 13 Then
With Label2
.Caption = "Done" ' put in other label
.Width = 800
.BackColor = RGB(255, 180, 180) ' reddish
End With
Exit For ' ALL answered
' generate random question
Else
Do
Randomize
rr = Int(Rnd * 14) ' preset to 13
rr = IIf(rr = 0, 1, rr) ' trap for 0
q = Li.SubItems(rr)
Label1.Caption = aaQList(rr) ' put in cyan label
' is New question
If q = Empty Then
' "same answer check" branch
If aaQList(rr) = StudentAnsLBL Then
Li.SubItems(rr) = aaQList(rr)
Exit Do
Else
z = z ' do something else here, or do nothing
End If
' has been Asked
Else
b = b ' LOOP again
End If
Loop
End If
End If
Next ii
End With
End Sub
However my vb will then hang and freeze. Do you know where I went wrong?
-
Sep 26th, 2017, 11:59 AM
#31
Re: How to load a textfile into the first column of a listview?
Where?
Line 33?
If so, put a break-point (F9) on line 31, then press F8 and step thru line by line.
Report what happens.
EDIT-1
Just occurred to me that you might be getting to line 37 .. z = z.
If so, perhaps that induces an endless loop
So, change
z = z
to
Exit Do
That will prevent the endless loop, but I'm not sure it will produce the results you want.
That is what I meant by the trial and error bit
Spoo
Last edited by Spooman; Sep 26th, 2017 at 12:07 PM.
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
|