[RESOLVED] Get info from listbox datasource
If I have a listbox and it's datasource is an array of objects. How can I get the text value of an element "title" of the selected object in the listbox? Title is one of the elements I created for the object.
Each object in the datasource has various elements to it.
Title
Year
Thanks in advance!
Re: Get info from listbox datasource
I'm going to assume you've got an array of your class and title is the property you want to get information from when an item is selected. In this case I'm setting it to a TextBox's Text property.
Code:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Book[] bookArray = new Book[]
{
new Book { title = "Title 1", isbn = "1234567890" },
new Book { title = "Title 2", isbn = "2345678901" },
new Book { title = "Title 3", isbn = "3456789012" },
new Book { title = "Title 4", isbn = "4567890123" },
new Book { title = "Title 5", isbn = "5678901234" }
};
BooksListBox.DataSource = bookArray;
BooksListBox.DisplayMember = "title";
BooksListBox.ValueMember = "isbn";
}
private void BooksListBox_SelectedIndexChanged(object sender, EventArgs e)
{
SelectedBookTextBox.Text = ((Book)BooksListBox.SelectedItem).title;
}
}
public class Book
{
public string title { get; set; }
public string isbn { get; set; }
}
Re: Get info from listbox datasource