Convert Listbox Selected Item to String

I would like to convert the selected item in a listbox to a string.... I have tried multiple ways and I must be doing something wrong..

Just for example:

How do I have listbox9.selecteditem populate textbox2.text

??
MaxDes101Asked:
Who is Participating?
 
käµfm³d 👽Connect With a Mentor Commented:
Call the ToString() method:
textbox2.Text = listbox9.SelectedItem.ToString()

Open in new window

0
 
lazyberezovskyCommented:
Say we have User class:
class User
{
    public int ID { get; set; }
    public string Name { get; set; }
}

And populated our listbox with users:
List<User> users = new List<User>();
users.Add(new User() { ID = 1, Name = "Joe" });
users.Add(new User() { ID = 2, Name = "Bill" });
listBox1.DataSource = users;
listBox1.DisplayMember = "Name";

Populating textbox:
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    User selecteUser = listBox1.SelectedItem as User;
    textBox2.Text = String.Format("{0} {1}", selecteUser.ID, selecteUser.Name);            
}

Open in new window

0
 
MaxDes101Author Commented:
I populated my listbox with filenames including full path.

so:

C:\windows\filename.dll

How would my textbox populate that information?
0
 
lazyberezovskyCommented:
MaxDes101:
Then just call ToString() or cast ot string selected item.
textBox2.Text = (string)listBox1.SelectedItem;  

Open in new window

0
All Courses

From novice to tech pro — start learning today.