Reputation: 37
I have list, listbox, buttony textbox.
My idea is to do that by clicking on the button, the content of the text box is added to the list and then passing the data to a list box.
My problem is if you add what I write, but the elements that are in the list box are overwritten with the new one that you insert. and I just want to add more articles. to the list and to go to the list box. Thank you very much for your answers. This is the code of my button:
private void button54_Click(object sender, RoutedEventArgs e)
{
if (textBox3.Text != '')
{
List<Playlists> List1 = new List<Playlists>();
List1.Add(new Playlists(textBox3.Text, @rutaalbum));
lbPlaylist.ItemsSource = List1;
...
}
...
}
Upvotes: 2
Views: 13660
Reputation: 15091
I think you want to maintain a list and bind the list to a listbox. Then be able to add to the list with a button. I found that I had to unbind by setting the ItemSource to null and rebind to get the new items added to the list to show up in the listbox.
List<string> mylist = new List<string>();
private void btnAddToList_Click(object sender, System.Windows.RoutedEventArgs e)
{
mylist.Add(txtList.Text);
ListBox1.ItemsSource = null;
ListBox1.ItemsSource = mylist;
}
Upvotes: 4
Reputation: 5738
To create a list :
List<string> mylist = new List<string>
To add items to the list :
mylist.Add(textbox1.Text)
To add items to the ListBox
from the list :
foreach(string item in mylist)
{
ListBoxItem itm = new ListBoxItem();
itm.Content = item;
listbox.Items.Add(itm);
}
Hope this helps you :)
Upvotes: 0