How Do I display formatted text from an object in a list box

This is a moderately difficult problem to put into words...  However, I'll attempt to do my best.

For starters, I have a list box lbxPeople in which I am storing objects of classPeople.
I add the people to the list box with the following command, using my DataSource dsPerson and pulling records from table tblPersonRow

lbxPlayers.Items.Clear();
classPerson personEntry;
      foreach (dsPerson.tblPersonRow p in dsPerson1.tblPerson.Rows)
      {
         personEntry = new classPerson(p.Person_ID, p.Last_Name);
         lbxPlayers.Items.Add(playerEntry);
      }

I've looked in MSDN and the information on databinding sucks...

What I need to know is how to get my data out of the list box....

What I want to do is something like this...

tbxLastName.Text = lbxPlayers.Items[lbxPlayers.SelectedIndex].strLastName

but, the only option I get after SelectedIndex]. Is ToString(), GetHashCode(), Equals(),
and GetType().

So, I can see a couple ways of solving this problem.  Either figure out how to data bind the combo box, or just continue with the above reasoning and getting the info out of the list box.  Either solution would work for me.  

Oh, I am using .net 2003 if that matters

Kevon
kevonAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Razzie_Commented:
Override the ToString() method for the ClassPerson class:

public override string ToString()
{
    return this.Last_Name;
}

this will automatically show the last name value in the listbox.

Cheers,

Razzie
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Farshid-ZakerCommented:
I am agree with Razzie. But before using it, you should obtain an in-depth understanding of the problem. Take a look at the following line of code. It will help you to understand what is going wrong!

tbxLastName.Text = ((classPerson)lbxPlayers.Items[lbxPlayers.SelectedIndex]).strLastName

Items array of listbox control, is an array of objects of type System.Object. You store your System.Object inherited objects in it. So, you should first cast them to their original class.

hope to be helpful...
<Farshid/>
0
kevonAuthor Commented:
Thanks to you both.  The problem required both solutions so I split the points.

Here's what I ended up doing...

private void FillPlayerListBox()
{
      lbxPlayers.Items.Clear();
      classPlayer playerEntry;
      foreach (dsPerson.tblPersonRow p in dsPerson1.tblPerson.Rows)
      {
            playerEntry = new classPlayer(p.Person_ID, p.First_Name, p.MI, p.Last_Name, p.Address_1, p.City, p.State, p.Zip, p.Home_Phone, p.Cell_phone, p.Email_Address);
      lbxPlayers.Items.Add(playerEntry);
      }
}

and then...  to access it, exactly what Farshid said...  casting to the classPerson type..

Thanks both!!!
Kevon
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.