Solved

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

Posted on 2004-10-22
210 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
Question by:kevon
    3 Comments
     
    LVL 8

    Accepted Solution

    by:
    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
    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
    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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone. Privacy Policy Terms of Use

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    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…
    Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
    This video is in connection to the article "The case of a missing mobile phone (https://www.experts-exchange.com/articles/28474/The-Case-of-a-Missing-Mobile-Phone.html)". It will help one to understand clearly the steps to track a lost android phone.
    This video discusses moving either the default database or any database to a new volume.

    875 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

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now