Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2004-10-22
3
Medium Priority
?
212 Views
Last Modified: 2010-04-15
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
0
Comment
Question by:kevon
3 Comments
 
LVL 8

Accepted Solution

by:
Razzie_ earned 500 total points
ID: 12385451
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
 
LVL 4

Assisted Solution

by:Farshid-Zaker
Farshid-Zaker earned 500 total points
ID: 12385503
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
 

Author Comment

by:kevon
ID: 12385845
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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question