-
Apr 27th, 2016, 05:54 PM
#41
Thread Starter
PowerPoster
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.
-
Apr 28th, 2016, 10:16 AM
#42
Thread Starter
PowerPoster
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.
-
Apr 28th, 2016, 11:28 AM
#43
Thread Starter
PowerPoster
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.
-
Apr 28th, 2016, 11:34 AM
#44
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?
-
Apr 28th, 2016, 11:41 AM
#45
Thread Starter
PowerPoster
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
-
Apr 28th, 2016, 11:56 AM
#46
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.
-
Apr 28th, 2016, 12:24 PM
#47
Thread Starter
PowerPoster
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.
-
Apr 28th, 2016, 12:32 PM
#48
Thread Starter
PowerPoster
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"
-
Apr 28th, 2016, 12:38 PM
#49
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.
-
Apr 28th, 2016, 12:52 PM
#50
Thread Starter
PowerPoster
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
-
Apr 28th, 2016, 01:09 PM
#51
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.
-
Apr 28th, 2016, 01:17 PM
#52
Thread Starter
PowerPoster
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>
-
Apr 28th, 2016, 01:25 PM
#53
Thread Starter
PowerPoster
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.
-
Apr 28th, 2016, 01:29 PM
#54
Re: Confused about listbox. how to read one variable at a time
Originally Posted by jumper77
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.
-
Apr 28th, 2016, 01:36 PM
#55
Thread Starter
PowerPoster
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
-
Apr 28th, 2016, 01:47 PM
#56
Thread Starter
PowerPoster
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.
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
|