How to load a textfile into the first column of a listview?-VBForums
Results 1 to 31 of 31

Thread: How to load a textfile into the first column of a listview?

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    May 2006
    Posts
    2,270

    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:
    1. 'Set up the listview
    2. lvwbook.View = lvwReport
    3. lvwbook.GridLines = True
    4.  
    5. Dim li As ListItem
    6.  
    7. With lvwbook
    8.     .ColumnHeaders.Add , , "Student Name"
    9.  
    10.  
    11.     Set li = .ListItems.Add(, , "Row1Item1")
    12.  
    13.  
    14.  
    15.  
    16. 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!!

  2. #2
    Fanatic Member Spooman's Avatar
    Join Date
    Mar 2017
    Posts
    750

    Re: How to load a textfile into the first column of a listview?

    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

    Name:  Justin LV.png
Views: 108
Size:  6.1 KB

    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

    Name:  Justin LV txt.png
Views: 109
Size:  6.7 KB

    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
    Last edited by Spooman; Sep 19th, 2017 at 06:14 AM.

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    May 2006
    Posts
    2,270

    Re: How to load a textfile into the first column of a listview?

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

    Name:  Justin LV.png
Views: 108
Size:  6.1 KB

    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

    Name:  Justin LV txt.png
Views: 109
Size:  6.7 KB

    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!

  4. #4
    Fanatic Member Spooman's Avatar
    Join Date
    Mar 2017
    Posts
    750

    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.

  5. #5
    Fanatic Member Spooman's Avatar
    Join Date
    Mar 2017
    Posts
    750

    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

    Name:  Justin LV2.png
Views: 86
Size:  7.3 KB

    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.

  6. #6

    Thread Starter
    PowerPoster
    Join Date
    May 2006
    Posts
    2,270

    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!

  7. #7
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    12,141

    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.

  8. #8
    Fanatic Member Spooman's Avatar
    Join Date
    Mar 2017
    Posts
    750

    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
    • .ListItems.Count
    • ii


    Spoo
    Last edited by Spooman; Sep 21st, 2017 at 03:18 AM.

  9. #9

    Thread Starter
    PowerPoster
    Join Date
    May 2006
    Posts
    2,270

    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:
    1. '
    2.     Dim li As ListItem
    3.  
    4.    
    5.     With lvwbook
    6.         ' Post Answer
    7.         For i = 1 To lvwbook.ListItems.Count
    8.             b = lvwbook.ListItems.Item(ii)
    9.             If b = MapForm.StudentPickLBL.Caption Then                  ' StudentPickLBL
    10.                 Set li = lvwbook.ListItems.Item(ii)
    11.                 li.SubItems(1) = MapForm.StudentAnsLBL.Caption      ' StudentAnsLBL
    12.                 Exit For
    13.             End If
    14.         Next i
    15.     End With
    16.     '[/B]

  10. #10

    Thread Starter
    PowerPoster
    Join Date
    May 2006
    Posts
    2,270

    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?

  11. #11
    Fanatic Member Spooman's Avatar
    Join Date
    Mar 2017
    Posts
    750

    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

  12. #12

    Thread Starter
    PowerPoster
    Join Date
    May 2006
    Posts
    2,270

    Re: How to load a textfile into the first column of a listview?

    Ok , it says i = empty.

  13. #13
    Fanatic Member Spooman's Avatar
    Join Date
    Mar 2017
    Posts
    750

    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.

  14. #14

    Thread Starter
    PowerPoster
    Join Date
    May 2006
    Posts
    2,270

    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!

  15. #15
    Fanatic Member Spooman's Avatar
    Join Date
    Mar 2017
    Posts
    750

    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

  16. #16

    Thread Starter
    PowerPoster
    Join Date
    May 2006
    Posts
    2,270

    Re: How to load a textfile into the first column of a listview?

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

  17. #17
    Fanatic Member Spooman's Avatar
    Join Date
    Mar 2017
    Posts
    750

    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

    Name:  Justin LV13.jpg
Views: 66
Size:  21.4 KB

    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.

  18. #18

    Thread Starter
    PowerPoster
    Join Date
    May 2006
    Posts
    2,270

    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:
    1. Dim lIndex As Long
    2.    If QList.ListCount Then
    3.       lIndex = Int(Rnd * QList.ListCount)
    4.       QuestionLBL.Caption = QList.List(lIndex)
    5.         'QList.RemoveItem lIndex
    6.     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!!

  19. #19
    Fanatic Member Spooman's Avatar
    Join Date
    Mar 2017
    Posts
    750

    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 ..

    Name:  Justin LV3.jpg
Views: 59
Size:  21.9 KB

    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

    1. I modified my code such that repeated clicking of Command2 would trigger the above snippet
    2. I created an array aaQList to match your QList, and populated it with the 13 choices
    3. A random number was generated as you did (I had to preset it to 13)
    4. The result is posted in the cyan Label2(1)
    5. If not previously encountered, then the ListView is populated .. MB, NT, NS, ON
    6. 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

  20. #20

    Thread Starter
    PowerPoster
    Join Date
    May 2006
    Posts
    2,270

    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? : )

  21. #21
    Fanatic Member Spooman's Avatar
    Join Date
    Mar 2017
    Posts
    750

    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
      1. it creates the array
      2. it sets the "type" to String
      3. it makes it "public" to all subs, functions, etc on your Form
      4. it presets it to have 14 elements (arrays are "numbered" from 0 to 13 .. see pic below)
      5. that is, they are typically considered "0-based"
    • To populate it
      1. I copied the list from your post #16 into NotePad
      2. The fact that you listed them vertically made life earier ..
      3. 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

    Name:  Justin aaQList.png
Views: 46
Size:  12.0 KB

    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)
    1. rr can possibly = 0, which will throw an error on the next line
    2. rr can NOT = 13
    3. 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

  22. #22

    Thread Starter
    PowerPoster
    Join Date
    May 2006
    Posts
    2,270

    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:
    1. Public Sub Command2_Click()
    2.  
    3. 'Dim aaQList(13) As String
    4.     With Lvwbook
    5.             ' Post Answer
    6.             For ii = 1 To .ListItems.Count
    7.                 b = .ListItems.Item(ii)
    8.                 If b = MapForm.StudentPickLBL.Caption Then                      ' StudentPickLBL
    9.                     Set li = Lvwbook.ListItems.Item(ii)
    10.                     ' generate random question
    11.                     rr = Int(Rnd * 14)                          ' preset to 13
    12.                     rr = IIf(rr = 0, 1, rr)                     ' trap for 0
    13.                     q = li.SubItems(rr)
    14.                     Label1.Caption = aaQList(rr)             ' put in cyan label
    15.                     ' is New question
    16.                     If q = Empty Then
    17.                         If MapForm.StudentAnsLBL.Caption = Label1.Caption Then
    18.                         li.SubItems(rr) = aaQList(rr)           ' Qlist
    19.                     ' has been Asked
    20.                         End If
    21.                     Else
    22.                         b = b
    23.                     End If
    24.                     Exit For
    25.                 End If
    26.             Next ii
    27.         End With
    28. 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.

  23. #23
    Fanatic Member Spooman's Avatar
    Join Date
    Mar 2017
    Posts
    750

    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.

  24. #24

    Thread Starter
    PowerPoster
    Join Date
    May 2006
    Posts
    2,270

    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!

  25. #25
    Fanatic Member Spooman's Avatar
    Join Date
    Mar 2017
    Posts
    750

    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
    1. 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
    2. 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
    3. 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.

    Name:  Justin LV4.jpg
Views: 43
Size:  23.1 KB

    HTH

    Spoo
    Last edited by Spooman; Sep 24th, 2017 at 06:37 AM.

  26. #26

    Thread Starter
    PowerPoster
    Join Date
    May 2006
    Posts
    2,270

    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?

  27. #27
    Fanatic Member Spooman's Avatar
    Join Date
    Mar 2017
    Posts
    750

    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.

  28. #28

    Thread Starter
    PowerPoster
    Join Date
    May 2006
    Posts
    2,270

    Re: How to load a textfile into the first column of a listview?

    VB Code:
    1. Public Sub CmdPickQ_Click()
    2.          With Lvwbook
    3.             ' Post Answer
    4.             For ii = 1 To .ListItems.Count
    5.                 b = .ListItems.Item(ii)
    6.                 If b = MapForm.StudentPickLBL.Caption Then                      ' StudentPickLBL
    7.                     Set Li = Lvwbook.ListItems.Item(ii)
    8.                     ' count answered
    9.                     ans = 0
    10.                     For aa = 1 To 13
    11.                         zz = Li.SubItems(aa)
    12.                         ans = ans + IIf(Li.SubItems(aa) <> Empty, 1, 0)
    13.                     Next aa
    14.                     ' Done .. all answered .. PREVENT endless loop
    15.                     If ans = 13 Then
    16.                         With Label2
    17.                             .Caption = "Done"                   ' put in other label
    18.                             .Width = 800
    19.                             .BackColor = RGB(255, 180, 180)     ' reddish
    20.                         End With
    21.                         Exit For                                ' ALL answered
    22.                     ' generate random question
    23.                     Else
    24.                         Do
    25.                             Randomize
    26.                             rr = Int(Rnd * 14)                  ' preset to 13
    27.                             rr = IIf(rr = 0, 1, rr)             ' trap for 0
    28.                             q = Li.SubItems(rr)
    29.                             Label1.Caption = aaQList(rr)     ' put in cyan label
    30.                             ' is New question
    31.                             If q = Empty Then
    32.                                 Li.SubItems(rr) = aaQList(rr)   ' StudentAnsLBL
    33.                                 Exit Do
    34.                             ' has been Asked
    35.                             Else
    36.                                 b = b                           ' LOOP again
    37.                             End If
    38.                         Loop
    39.                     End If
    40.                 End If
    41.             Next ii
    42.         End With
    43. 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!

  29. #29
    Fanatic Member Spooman's Avatar
    Join Date
    Mar 2017
    Posts
    750

    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

  30. #30

    Thread Starter
    PowerPoster
    Join Date
    May 2006
    Posts
    2,270

    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:
    1. Public Sub CmdPickQ_Click()
    2.              With Lvwbook
    3.                 ' Post Answer
    4.                 For ii = 1 To .ListItems.Count
    5.                     b = .ListItems.Item(ii)
    6.                     If b = MapForm.StudentPickLBL.Caption Then                      ' StudentPickLBL
    7.                         Set Li = Lvwbook.ListItems.Item(ii)
    8.                         ' count answered
    9.                         ans = 0
    10.                         For aa = 1 To 13
    11.                             zz = Li.SubItems(aa)
    12.                             ans = ans + IIf(Li.SubItems(aa) <> Empty, 1, 0)
    13.                         Next aa
    14.                         ' Done .. all answered .. PREVENT endless loop
    15.                         If ans = 13 Then
    16.                             With Label2
    17.                                 .Caption = "Done"                   ' put in other label
    18.                                 .Width = 800
    19.                                 .BackColor = RGB(255, 180, 180)     ' reddish
    20.                             End With
    21.                             Exit For                                ' ALL answered
    22.                         ' generate random question
    23.                         Else
    24.                             Do
    25.                                 Randomize
    26.                                 rr = Int(Rnd * 14)                  ' preset to 13
    27.                                 rr = IIf(rr = 0, 1, rr)             ' trap for 0
    28.                                 q = Li.SubItems(rr)
    29.                                 Label1.Caption = aaQList(rr)     ' put in cyan label
    30.                                                         ' is New question
    31.                             If q = Empty Then
    32.                                 ' "same answer check" branch
    33.                                 If aaQList(rr) =  StudentAnsLBL Then
    34.                                     Li.SubItems(rr) = aaQList(rr)  
    35.                                     Exit Do
    36.                                 Else
    37.                                     z = z            ' do something else here, or do nothing
    38.                                 End If
    39.                             ' has been Asked
    40.                             Else
    41.                                 b = b                           ' LOOP again
    42.                             End If
    43.                             Loop
    44.                         End If
    45.                     End If
    46.                 Next ii
    47.             End With
    48.     End Sub

    However my vb will then hang and freeze. Do you know where I went wrong?

  31. #31
    Fanatic Member Spooman's Avatar
    Join Date
    Mar 2017
    Posts
    750

    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
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.