 |
 |

07-10-2012, 08:00 AM
|
 |
Newcomer
|
|
Join Date: Jun 2008
Posts: 20
|
|
A ListBox conundrum
|
My apologies guys, I'm trying to learn vb.net as I go (I was semi-literate in VB6) what with all its nuances and all.
Ok, here's what I want to do and where I've come into a bit of confusion about listboxes.
Ok, I have a set of players in an array called Player(x,y) - x being the players ID, y being a set attribute of the player - in this case element 1 being the last name.
I wish to set up a listbox on launch will populate any given number of Players, that will allow the user to pick a player using the listbox - by name, rather than by ID# - and have it show the selected players stats.
My issue is the population and retrieving.
Yes, I am aware of how to attach individual items to a listbox (listbox.items.add), but is there anyway to populate the listbox items with an additional value?
E.g Player(1,1) Might be ID#1 and the name might be Reynolds - is it possible for an item to be added as 'Reynolds' with a value of 1?
Thanks in advance?
|
|

07-10-2012, 08:15 AM
|
 |
Senior Contributor
* Expert *
|
|
Join Date: Apr 2003
Location: Never where I want to be
Posts: 1,275
|
|
You can put anything in a listbox. Not just a string.
So you can add any class you want to a listbox and the listbox will use the class's ToString method to populate the listbox with.
Code:
Public Class Player
Public ID as Integer
Public FirstName as String
Public LastName as string
Public Function FullName as String
Return FirstName & " " & LastName
End Sub
Public Overrides Function ToString() As String
Return FullName
End Function
Public Sub New(byval id as integer, byval firstName as string, byval lastName as string)
me.id = id
me.firstname = firstname
me.lastname = lastname
End Sub
End Class
Then, a form with a listbox showing how it's used...
Code:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim p As Player
p = New Player(1, "Bob", "Smith")
ListBox1.Items.Add(p)
p = New Player(2, "Ted", "Rogers")
ListBox1.Items.Add(p)
p = New Player(3, "John", "Manford")
ListBox1.Items.Add(p)
End Sub
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
Dim p As Player = CType(ListBox1.SelectedItem, Player)
MessageBox.Show(p.ID & ", " & p.FirstName & ", " & p.LastName)
End Sub
End Class
The messagebox on selecting an item demonstrates that you can get to all the properties of the class that was added to the listbox.
|
__________________
There are no computers in heaven!
|

07-10-2012, 08:36 AM
|
 |
Newcomer
|
|
Join Date: Jun 2008
Posts: 20
|
|
|
Thanks so much for that, very insightful!
|
|

07-10-2012, 10:45 AM
|
 |
Newcomer
|
|
Join Date: Jun 2008
Posts: 20
|
|
|
Object reference not set to an instance of an object
I keep getting that however when I'm trying to access the messagebox though. Albeit, I've used the code on a button, not a listbox - albeit on the same form.
|
|

07-10-2012, 04:31 PM
|
 |
Senior Contributor
Forum Leader * Expert *
|
|
Join Date: Apr 2005
Location: USA
Posts: 866
|
|
|
You should post the code that's throwing the error. I would imagine the null reference is within the expression that you pass to the message box.
Or better yet, set a breakpoint on the line that throws the error, and examine the variables to see which one is null.
|
__________________
C# _VB.NET _
|

07-10-2012, 10:44 PM
|
 |
Newcomer
|
|
Join Date: Jun 2008
Posts: 20
|
|
Quote:
Originally Posted by snarfblam
You should post the code that's throwing the error. I would imagine the null reference is within the expression that you pass to the message box.
Or better yet, set a breakpoint on the line that throws the error, and examine the variables to see which one is null.
|
I've set up the listbox to be populated, and the PlayerList Class is in a public module.
Below is the code pulling up the error however when I'm clicking a button to retrieve the information from the listbox.
Dim p As PlayerList = CType(LBP.SelectedItem, PlayerList)
MessageBox.Show(p.ID & ", " & p.FirstName & ", " & p.LastName)
It would seem p is totally NULL, after setting the breakpoint, which is confusing.
|
|

07-10-2012, 10:47 PM
|
 |
Newcomer
|
|
Join Date: Jun 2008
Posts: 20
|
|
|
I've also traced this back to LBP.SelectedItem coming up as 'Nothing' - I don't know how this is possible as there is something selected within the listbox.
EDIT: I have discovered you have to CLICK the listbox to get the value for .selected item - is there a way where you can get the value if it is not 'clicked'.
Cheers.
|
Last edited by KrisDwyer; 07-10-2012 at 11:21 PM.
|

07-11-2012, 06:05 AM
|
 |
Multi-Technologist
Super Moderator * Expert *
|
|
Join Date: May 2004
Location: Michigan
Posts: 3,739
|
|
Maybe try something like:
Code:
If LBP.SelectedItem Is Nothing Then
If LBP.Items.Count > 0 Then
LBP.SelectedIndex = LBP.TopIndex
Else
'Throw Error
End If
End If
|
__________________
"May the code that you write never work in ways that you didn't expect; and may the code that you didn't write never require you to maintain it". - Ancient Chinese Proverb
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|
|
|
|
 |
|