Convert Listbox Selected Item to String

MaxDes101 used Ask the Experts™
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

Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
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


I populated my listbox with filenames including full path.



How would my textbox populate that information?
Glanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015
Call the ToString() method:
textbox2.Text = listbox9.SelectedItem.ToString()

Open in new window

Then just call ToString() or cast ot string selected item.
textBox2.Text = (string)listBox1.SelectedItem;  

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial