Go Back  Xtreme Visual Basic Talk > Visual Basic .NET (2002/2003/2005/2008, including Express editions) > .NET General > A ListBox conundrum


Reply
 
Thread Tools Display Modes
  #1  
Old 07-10-2012, 08:00 AM
KrisDwyer's Avatar
KrisDwyer KrisDwyer is offline
Newcomer
 
Join Date: Jun 2008
Posts: 20
Default 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?
Reply With Quote
  #2  
Old 07-10-2012, 08:15 AM
DrPunk's Avatar
DrPunk DrPunk is offline
Senior Contributor

* Expert *
 
Join Date: Apr 2003
Location: Never where I want to be
Posts: 1,275
Default

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!
Reply With Quote
  #3  
Old 07-10-2012, 08:36 AM
KrisDwyer's Avatar
KrisDwyer KrisDwyer is offline
Newcomer
 
Join Date: Jun 2008
Posts: 20
Default

Thanks so much for that, very insightful!
Reply With Quote
  #4  
Old 07-10-2012, 10:45 AM
KrisDwyer's Avatar
KrisDwyer KrisDwyer is offline
Newcomer
 
Join Date: Jun 2008
Posts: 20
Default

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.
Reply With Quote
  #5  
Old 07-10-2012, 04:31 PM
snarfblam's Avatar
snarfblam snarfblam is offline
Senior Contributor

Forum Leader
* Expert *
 
Join Date: Apr 2005
Location: USA
Posts: 866
Default

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 _
Reply With Quote
  #6  
Old 07-10-2012, 10:44 PM
KrisDwyer's Avatar
KrisDwyer KrisDwyer is offline
Newcomer
 
Join Date: Jun 2008
Posts: 20
Default

Quote:
Originally Posted by snarfblam View Post
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.
Reply With Quote
  #7  
Old 07-10-2012, 10:47 PM
KrisDwyer's Avatar
KrisDwyer KrisDwyer is offline
Newcomer
 
Join Date: Jun 2008
Posts: 20
Default

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.
Reply With Quote
  #8  
Old 07-11-2012, 06:05 AM
Cerian Knight's Avatar
Cerian Knight Cerian Knight is offline
Multi-Technologist

Super Moderator
* Expert *
 
Join Date: May 2004
Location: Michigan
Posts: 3,739
Default

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
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->