Listview help

If you need help with a project or need to know how to do something specific in VB.NET then please ask your questions in here.
Forum rules
Please LOCK your topics once you have found the solution to your question so we know you no longer require help with your query.
4 posts Page 1 of 1
Contributors
User avatar
muttley1968
Hardcore Programmer
Hardcore Programmer
Posts: 622
Joined: Thu Jun 17, 2010 11:54 pm

Listview help
muttley1968
Right I have a list view and I am adding info to it with a button like this
Code: Select all
        ListView1.Items.Add("richardjmanning")
        ListView1.Items(0).SubItems.Add(ram.Next(0, ListBox2.Items.Count))
        ListView1.Items(0).SubItems.Add(ram.Next(1, ListBox2.Items.Count))
        ListView1.Items(0).SubItems.Add(ram.Next(2, ListBox2.Items.Count))
        ListView1.Items(0).SubItems.Add(ram.Next(3, ListBox2.Items.Count))
        ListView1.Items(0).SubItems.Add(ram.Next(4, ListBox2.Items.Count))
        ListView1.Items(0).SubItems.Add(ram.Next(5, ListBox2.Items.Count))
        ListView1.Items(0).SubItems.Add(ram.Next(6, ListBox2.Items.Count))
But I want it to add new line every time but it doesnot it add a new richardjmanning but no new random numbers why is this and also is there a way to compare weather a list view contains numbers
soo say I have 7 labels on my form all with different numbers is there any way to see if an item in my listview contains
any of these numbers and display the username "richardjmanning" of the user that has the most numbers

If it helps its for a lotto system the man hits the button to add a user and then when ready to pick a winner hit another button that make the labels go to random number from 1 to 100 and I want to see what 5 users are clostest to the win
User avatar
comathi
Coding God
Coding God
Posts: 1242
Joined: Fri Mar 26, 2010 1:59 pm

Re: Listview help
comathi
The reason is quite simple. Every time, you're telling it to add a new ListViewItem, but then you're telling it to add the subitems to items(0), so it tries to change the same item every time. This can be solved easily by declaring a ListViewItem and adding the subitems to it before adding it to the control, like so:
Code: Select all
Dim ram As New Random()
        Dim newItem As New ListViewItem
        newItem.Text = "richardjmanning"
        newItem.SubItems.Add(ram.Next(0, ListBox2.Items.Count))
        newItem.SubItems.Add(ram.Next(1, ListBox2.Items.Count))
        newItem.SubItems.Add(ram.Next(2, ListBox2.Items.Count))
        newItem.SubItems.Add(ram.Next(3, ListBox2.Items.Count))
        newItem.SubItems.Add(ram.Next(4, ListBox2.Items.Count))
        newItem.SubItems.Add(ram.Next(5, ListBox2.Items.Count))
        newItem.SubItems.Add(ram.Next(6, ListBox2.Items.Count))

        ListView1.Items.Add(newItem)
Please note: I've re-declared ram As New Random() in the above code just so that I could use it myself and test it. If you've already declared somewhere else where you can access it, you can remove that part from the code I'm giving you ;)
User avatar
muttley1968
Hardcore Programmer
Hardcore Programmer
Posts: 622
Joined: Thu Jun 17, 2010 11:54 pm

Re: Listview help
muttley1968
That works AMAZING thank is there a way now were I can display the Top 5 people clostest to hitting the right numbers ill get screen shots and all now to make it easier to understand
User avatar
muttley1968
Hardcore Programmer
Hardcore Programmer
Posts: 622
Joined: Thu Jun 17, 2010 11:54 pm

Re: Listview help
muttley1968
Right soo here is the first page the balls will pick random numbers and everything is well just look and see
Image

Then here you add users to the draw now what I want to do is on page 1 there are winners 1 to 5 I want to put the 5 people from this list box with the closest numbers to see who won the money
Image
4 posts Page 1 of 1
Return to “Coding Help & Support”