Click here to Skip to main content
16,005,141 members
Home / Discussions / C#
   

C#

 
AnswerRe: Thread's Pin
Ed.Poore14-Mar-06 10:41
Ed.Poore14-Mar-06 10:41 
GeneralRe: Thread's Pin
CiberwizZ14-Mar-06 10:52
CiberwizZ14-Mar-06 10:52 
QuestionListView problems Pin
Authorof2214-Mar-06 8:27
Authorof2214-Mar-06 8:27 
QuestionRe: ListView problems Pin
Ed.Poore14-Mar-06 9:47
Ed.Poore14-Mar-06 9:47 
AnswerRe: ListView problems Pin
Authorof2214-Mar-06 10:11
Authorof2214-Mar-06 10:11 
GeneralRe: ListView problems Pin
Dan Neely14-Mar-06 10:35
Dan Neely14-Mar-06 10:35 
GeneralRe: ListView problems Pin
Ed.Poore14-Mar-06 10:39
Ed.Poore14-Mar-06 10:39 
GeneralRe: ListView problems Pin
Ed.Poore14-Mar-06 10:39
Ed.Poore14-Mar-06 10:39 
Create a simple structure like so:

public struct Car
{
    private string name;
    public string Name
    {
        get
        {
            return this.name;
        }
    }
    private string license;
    public string License
    {
        get
        {
            return this.license;
        }
    }

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

    public Car(string name, string license)
    {
        this.name = name;
        this.license = license;
    }
}


By default the listbox will use the Car.ToString() function for the text to display. Using this example it will display the car registration. However, either you can change the ToString to return the name and whatever else you want (very useful if you need text from multiple properties or to apply formatting to the text). Or if you just want to display the name then you can set the DisplayMember property of the listbox to "Name" and it will use the Name property to look up the data (note it cannot use fields, they must be properties.

This method also allows you to expand the data later on (sometimes I use the DrawItem event to write text from the structure in different places on the list item). But that's another complication. Big Grin | :-D

To access the data in the listbox you must first cast the data, so for example to get the license of the selected item in the list box you need something like:

this.Text = ((Car)this.listbox.SelectedItem).License;

Hope it helps Ed
GeneralRe: ListView problems Pin
Authorof2215-Mar-06 5:04
Authorof2215-Mar-06 5:04 
GeneralRe: ListView problems Pin
Ed.Poore15-Mar-06 5:40
Ed.Poore15-Mar-06 5:40 
QuestionTreeView index Pin
Authorof2214-Mar-06 8:23
Authorof2214-Mar-06 8:23 
AnswerRe: TreeView index Pin
Ed.Poore14-Mar-06 9:54
Ed.Poore14-Mar-06 9:54 
GeneralRe: TreeView index Pin
Authorof2214-Mar-06 10:05
Authorof2214-Mar-06 10:05 
GeneralRe: TreeView index Pin
Ed.Poore14-Mar-06 10:25
Ed.Poore14-Mar-06 10:25 
GeneralRe: TreeView index Pin
Authorof2215-Mar-06 2:46
Authorof2215-Mar-06 2:46 
GeneralRe: TreeView index Pin
Ed.Poore15-Mar-06 5:38
Ed.Poore15-Mar-06 5:38 
GeneralRe: TreeView index Pin
Authorof2215-Mar-06 5:53
Authorof2215-Mar-06 5:53 
GeneralRe: TreeView index Pin
Ed.Poore15-Mar-06 6:26
Ed.Poore15-Mar-06 6:26 
QuestionRemove duplicate Items from textbox. Pin
Rajendra Rana14-Mar-06 6:11
Rajendra Rana14-Mar-06 6:11 
AnswerRe: Remove duplicate Items from textbox. Pin
malharone15-Mar-06 3:12
malharone15-Mar-06 3:12 
QuestionUsing Reflection Pin
JuanAlbertoMD14-Mar-06 5:15
JuanAlbertoMD14-Mar-06 5:15 
AnswerRe: Using Reflection Pin
Tom Larsen14-Mar-06 8:29
Tom Larsen14-Mar-06 8:29 
AnswerRe: Using Reflection Pin
Vikram A Punathambekar14-Mar-06 20:17
Vikram A Punathambekar14-Mar-06 20:17 
GeneralRe: Using Reflection Pin
JuanAlbertoMD15-Mar-06 3:31
JuanAlbertoMD15-Mar-06 3:31 
Question1000! Pin
rockxuyenmandem14-Mar-06 4:54
rockxuyenmandem14-Mar-06 4:54 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.