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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
käµfm³d 👽Commented:
Call the ToString() method:
textbox2.Text = listbox9.SelectedItem.ToString()

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
lazyberezovskyCommented:
MaxDes101:
Then just call ToString() or cast ot string selected item.
textBox2.Text = (string)listBox1.SelectedItem;  

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.