Page 2 of 2 FirstFirst 12
Results 41 to 56 of 56

Thread: Confused about listbox. how to read one variable at a time

  1. #41

    Thread Starter
    PowerPoster
    Join Date
    Feb 2016
    Location
    Tennessee
    Posts
    2,437

    Re: Confused about listbox. how to read one variable at a time

    First let me say that there has been and incredible amount of stupidity going on here <laughs>

    I Always have a folder opened that's the debug folder of the project. But this time it was in a backup folder. I forgot. I was there looking at some older code. So that's why even deleting the file had no effect. I've changed the XML file to one that has normal names and not one's where the first and last names are "asdf" (Sitten's favorite).

    So, I'm back to only having one problem. The only thing that gets put into the listView is the one I manually put in before clicking the button to bring up the listview. I'll figure that out eventually, and when I do.... I'm going to have a beer

    So, panic handled, I'm calm(er) and heading back to the issue.

  2. #42

    Thread Starter
    PowerPoster
    Join Date
    Feb 2016
    Location
    Tennessee
    Posts
    2,437

    Re: Confused about listbox. how to read one variable at a time

    Hi again everyone. Well, I'm still working on this. And for something that should be so easy, it's sure is taking a while to get it to work. So I have come back with recent code for you to see.

    I have verified that: _serial_result(i).FirstName has the correct information because the first names keep changing for every time through the loop. But down at the bottom where
    itm = New ListViewItem(_serial_result(i).FirstName) is, the information is not entered into the ListView.

    Would someone mind taking a look at this. I said I was going to give up on it, but I can't. I've done other applications with ListViews and none of them have taken so long as this one. Not even close.

    So... here's the code:


    Code:
       Private Sub lView_Shown(sender As Object, e As EventArgs) Handles Me.Shown
            Dim i As Integer = 0
            For Each item In VersionOne.lstContacts.Items
    
                'lstView.Items.Add(_serial_result(i).FirstName)
                'lstView.Items.Add(_serial_result(i).LastName)
                'lstView.Items.Add(_serial_result(i).PhoneNumber)
                'lstView.Items.Add(_serial_result(i).Birthday.ToShortDateString)
    
                 Dim itm As ListViewItem
                itm = New ListViewItem(_serial_result(i).FirstName)
                lstView.Items.Add(itm)
                i += 1
                If i = 5 Then
                    i = 1
                End If
    
            Next item
            doneOnce = True
    
        End Sub
    Thank you all for your time. I apologize for this going on for so long.

  3. #43

    Thread Starter
    PowerPoster
    Join Date
    Feb 2016
    Location
    Tennessee
    Posts
    2,437

    Re: Confused about listbox. how to read one variable at a time

    I'm happy to announce I'm the proud father of a new working ListView. It is so simple that I'm embarrassed. I've had some things going on in my personal life and I just couldn't concentrate.

    So here it is. About a third of forth of what it was...
    Code:
     Private Sub lView_Shown(sender As Object, e As EventArgs) Handles Me.Shown
            Dim incCheck = 0
            For Each item In VersionOne.lstContacts.Items
                Dim lstcount = VersionOne.lstContacts.Items.Count
    
                Dim thissub As New ListViewItem
                thissub.SubItems.Add(_serial_result(incCheck).FirstName)
                thissub.SubItems.Add(_serial_result(incCheck).LastName)
                thissub.SubItems.Add(_serial_result(incCheck).PhoneNumber)
                thissub.SubItems.Add(_serial_result(incCheck).Birthday.ToShortDateString)
                lstView.Items.Add(thissub)
                incCheck += 1
                If incCheck = lstcount Then
                    incCheck = 1
                End If
            Next item
            doneOnce = True
        End Sub
    The main thing I couldn't remember was SubItems.
    Last edited by jumper77; Apr 28th, 2016 at 11:34 AM.

  4. #44
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,196

    Re: Confused about listbox. how to read one variable at a time

    Code:
                incCheck += 1
                If incCheck = thiscount Then
                    incCheck = 1
                End If
    What's this for?

  5. #45

    Thread Starter
    PowerPoster
    Join Date
    Feb 2016
    Location
    Tennessee
    Posts
    2,437

    Re: Confused about listbox. how to read one variable at a time

    Hi wes, just thought I would use something different than "i" for a loop var. Thiscount is the number of items the listBox. And using "incCheck +=1" is really like saying i += 1, but I wanted to use a name this time. I couldn't count how many times I've used "i" in a loop over the years

    edit: the ones that everyone used for loops were: i, j, and k

  6. #46
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,196

    Re: Confused about listbox. how to read one variable at a time

    But why is it there? It's not used, it's just wasted code.

  7. #47

    Thread Starter
    PowerPoster
    Join Date
    Feb 2016
    Location
    Tennessee
    Posts
    2,437

    Re: Confused about listbox. how to read one variable at a time

    Oh it's used...
    incCheck starts out as zero before the loop

    Then it's used like an index key to access the variables

    And when incCheck reaches the number of items in the listbox, it resets itself back to zero.

    If incCheck wasn't incremented, the same info would be entered into the listbox for thiscount of times.

    Is that better? I just tested it without it, and as I said, it inserted the same item over and over.

    don't reply yet, you're right about something and I'm going to post it.

  8. #48

    Thread Starter
    PowerPoster
    Join Date
    Feb 2016
    Location
    Tennessee
    Posts
    2,437

    Re: Confused about listbox. how to read one variable at a time

    I did find some code that is not needed. incCheck does need to be incremented, but there's no reason to reset it back to the original value. That's because it will stop by itself once it reaches the number of items in the listbox.

    Here's the code I don't need:

    Code:
    If incCheck = lstcount Then
        incCheck = 1
    End If
    And here's the new code without it.
    Code:
       Private Sub lView_Shown(sender As Object, e As EventArgs) Handles Me.Shown
            Dim incCheck = 0
            For Each item In VersionOne.lstContacts.Items
                Dim lstcount = VersionOne.lstContacts.Items.Count
    
                Dim thissub As New ListViewItem
                thissub.SubItems.Add(_serial_result(incCheck).FirstName)
                MsgBox(_serial_result(incCheck).LastName)
                thissub.SubItems.Add(_serial_result(incCheck).LastName)
                thissub.SubItems.Add(_serial_result(incCheck).PhoneNumber)
                thissub.SubItems.Add(_serial_result(incCheck).Birthday.ToShortDateString)
                lstView.Items.Add(thissub)
                incCheck += 1
            Next item
            doneOnce = True
        End Sub
    Thanks for pointing that out wes

    edit: I think I'm going to rename "incCheck" to plain "inc"

  9. #49
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,196

    Re: Confused about listbox. how to read one variable at a time

    Code:
            For Each item In VersionOne.lstContacts.Items
                Dim lstcount = VersionOne.lstContacts.Items.Count
    
                Dim thissub As New ListViewItem
                thissub.SubItems.Add(_serial_result(incCheck).FirstName)
                thissub.SubItems.Add(_serial_result(incCheck).LastName)
                thissub.SubItems.Add(_serial_result(incCheck).PhoneNumber)
                thissub.SubItems.Add(_serial_result(incCheck).Birthday.ToShortDateString)
                lstView.Items.Add(thissub)
                incCheck += 1
                If incCheck = lstcount Then
                    incCheck = 1
                End If
            Next item
    I see where your using it now but that doesn't seem to be the correct way to use a For Loop.

    If "item" represents each contact then,
    Code:
            For Each item In VersionOne.lstContacts.Items
                Dim lstcount = VersionOne.lstContacts.Items.Count
    
                Dim thissub As New ListViewItem
                thissub.SubItems.Add(item.FirstName)
                thissub.SubItems.Add(item.LastName)
                thissub.SubItems.Add(item.PhoneNumber)
                thissub.SubItems.Add(item.Birthday.ToShortDateString)
                lstView.Items.Add(thissub)
    
            Next item
    If you have to use and index for some reason then you should do it this way,
    Code:
            For incCheck = 0 to VersionOne.lstContacts.Items.Count -1
                Dim lstcount = VersionOne.lstContacts.Items.Count
    
                Dim thissub As New ListViewItem
                thissub.SubItems.Add(_serial_result(incCheck).FirstName)
                thissub.SubItems.Add(_serial_result(incCheck).LastName)
                thissub.SubItems.Add(_serial_result(incCheck).PhoneNumber)
                thissub.SubItems.Add(_serial_result(incCheck).Birthday.ToShortDateString)
                lstView.Items.Add(thissub)
    
            Next item
    There's no need to increment incCheck manually, that's what a For Loop does.

  10. #50

    Thread Starter
    PowerPoster
    Join Date
    Feb 2016
    Location
    Tennessee
    Posts
    2,437

    Re: Confused about listbox. how to read one variable at a time

    Thank you wes! That's really cool The only time I coded at the workplace it was just a few months before I retired. That was my introduction to these type of loops.

    But I've learned a number of things here about how to use them. I never understood the value of the variable that came right after the "For" keyword. But I know now.

    Thanks guy

  11. #51
    Frenzied Member
    Join Date
    Jul 2011
    Location
    UK
    Posts
    1,335

    Re: Confused about listbox. how to read one variable at a time

    Hi Jumper,

    Your code in Post #42 was actually doing what you wanted it to do. You didn't see anything being added to the ListView because there is a problem in a different part of your code

    From what you've posted so far, I'd say you still have code similar to this from Post #37:
    Code:
    If Not doneOnce Then
        With lView.lstView.Columns
            .Add("to fool First Name Column", 0, HorizontalAlignment.Left)
            .Add("First Name", fixedSize, HorizontalAlignment.Left)
            .Add("Last Name", fixedSize, HorizontalAlignment.Left)
            .Add("Phone Number", fixedSize, HorizontalAlignment.Left)
            .Add("Birthday", fixedSize, HorizontalAlignment.Left)
        End With
    End If
    There's a zero width column that you were adding your FirstNames to. So, of course, you couldn't see them.


    If you get rid of that zero width column, then your current code isn't going to work properly, as you are now skipping adding anything to the first column.

  12. #52

    Thread Starter
    PowerPoster
    Join Date
    Feb 2016
    Location
    Tennessee
    Posts
    2,437

    Re: Confused about listbox. how to read one variable at a time

    Hi inferrd, what is this, pick on me day <grin> Let me tell how this came about. When I first started adding columns to the ListView, the first column was wacky. I didn't matter what size I told it to be, I wanted to do whatever it wanted to do. So I started searching on the net. I ended up finding that this is a common problem with Listviews. And that's where I got this code from. And I have to admit, that ever since I've been using it, the name column has behaved and stayed the size I wanted it to be.

    There may be other workarounds, but this one is good for me.

    However, If you know the way to make it work right without any tricks, I'm all ears.

    edit: but if I change it, it will probably break a lot of code <grin>

  13. #53

    Thread Starter
    PowerPoster
    Join Date
    Feb 2016
    Location
    Tennessee
    Posts
    2,437

    Re: Confused about listbox. how to read one variable at a time

    Looks like I have to eat my own words. I just tried it without that column and it worked just fine. Don't know what I was doing before, but I'll take it. I did read where they said it was a common problem.

    I have to change some code now because my index is off.

    Edit: I'm not going to do it now. I've already spent too much time in the debugger lately. I'll wait on this for a while.
    Last edited by jumper77; Apr 28th, 2016 at 01:28 PM.

  14. #54
    Frenzied Member
    Join Date
    Jul 2011
    Location
    UK
    Posts
    1,335

    Re: Confused about listbox. how to read one variable at a time

    Quote Originally Posted by jumper77 View Post
    When I first started adding columns to the ListView, the first column was wacky. I didn't matter what size I told it to be, I wanted to do whatever it wanted to do. So I started searching on the net. I ended up finding that this is a common problem with Listviews. And that's where I got this code from. And I have to admit, that ever since I've been using it, the name column has behaved and stayed the size I wanted it to be.
    Aye, ListViews have a whole host of quirky behaviours

    Thing is, in Post #37 you are using fixed width columns (Dim fixedSize As Short = 140), not autosizing columns, so you don't need that trick any more.

    Also, If you do use the dummy column approach, I think you are supposed to remove the dummy column before you start populating the LV.


    Edit: sorry, didn't see your above post until after I submitted.

  15. #55

    Thread Starter
    PowerPoster
    Join Date
    Feb 2016
    Location
    Tennessee
    Posts
    2,437

    Re: Confused about listbox. how to read one variable at a time

    In the beginning, I had 2 vars, fixedSize and autoSize. And of course autoSize was -2. I can't remember exactly why, but I wasn't happy with it. Maybe I'll try it again and see how it works.

    And about removing the column.... I'm too lazy <laughs> It works great with it still in there, so who am I to argue? Plus, if you saw my last edit in the previous post, I talked about having spent too much time in the debugger the last few days

  16. #56

    Thread Starter
    PowerPoster
    Join Date
    Feb 2016
    Location
    Tennessee
    Posts
    2,437

    Re: Confused about listbox. how to read one variable at a time

    Just tried the auto size again and I remember why I didn't like it. It pushes everything real close together and ends up moving everything to the far left. When it does that the columns end up being about a 4th of the width of the Listview. Makes me think of a first year college student (no offense to any first year students here).

    When I use the fixed size, the presentation is so much better. It utilizes the width of the Listview quite nicely.

Page 2 of 2 FirstFirst 12

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