0

I have list, listbox, button and a textbox.

My idea is to make that by clicking on the button, the content of the textbox is added to the list, and then pass the data to a listbox.

My problem is, if you add what I write, but the items that are in the listbox are overwritten by the new one that you insert. and I want only more items added. to the list and to go to the listbox. Thank you very much for your answers. This is the code of my button:

private void button54_Click(object sender, RoutedEventArgs e)
{
    List<Playlists> List1 = new List<Playlists>();
    List1.Add(new Playlists(textBox3.Text, @rutaalbum));
    lbListbox.ItemsSource = List1;
}
5
  • Everytime you click that button you recreate the list, add an item to it and replace the previous list stored in the ItemsSource. You need to check if the ItemsSource is null, if not use the list stored there to add your new object or create the list just one time Commented Apr 14, 2018 at 7:10
  • Could you show me an example code please? Commented Apr 14, 2018 at 7:30
  • Just make List1 field rather than local variable Commented Apr 14, 2018 at 7:34
  • Everytime you are clicking button54, you are reinitializing the List1. That's why you are losing previously added item. You need to declare the List1 as a private static List in the class and not in the method. Commented Apr 14, 2018 at 7:47
  • Could you show me an example code please? I would like to try with some Commented Apr 14, 2018 at 7:57

2 Answers 2

2

I am just making a demo stand in for your Playlists class.

ToString has been overridden in order to display some text in the listbox. Without it, you will only display the class name.

ObservableCollection is used instead of List, so that the listbox updates when a new item is added.

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        MyList = new ObservableCollection<Playlists>();
        MyListBox.ItemsSource = MyList;
    }

    private ObservableCollection<Playlists> MyList { get; }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        MyList.Add(new Playlists(textBox3.Text));
    }
}

public class Playlists
{
    public Playlists(string title) { Title = title; }
    public string Title { get; }
    public override string ToString() { return Title; }
}
Sign up to request clarification or add additional context in comments.

2 Comments

and how did you add the data that I write from my textbox to the list?
Ok, I changed the answer to use textBox3.Text, as you did in the question.
1

It seems , you are always creating items with new list. need to add items to existing list.

use below code. it would be useful.

private void button54_Click(object sender, RoutedEventArgs e) {            
     lbListBox.Items.Add(new Playlists(textBox3.Text, @rutaalbum));       

}

9 Comments

downvoters. pls add a comment . that would help to improve the answer
@PeterBruins yes. but it will have existing items if list has any.. any ways. i updated answer. pls feel free to remove downvote
The truth is that I skip the exception. I do not know how to do that please if anyone knows.
@MartinaPrincente what exception you are getting
rather the errors are "existingItems" and "Any". How do I solve that?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.