On February 3, I posted a solution here for a "generic stack" that you could easily adapt ... without modification ... to your task: [
^].
Using the code in that example, your stack might be declared like this:
private const int StringStackDepth = 20;
private StackT<string> stringStack = new StackT<string>(StringStackDepth);
private BindingSource bindSource = new BindingSource();
private void Form1_Load(object sender, EventArgs e)
{
foreach (string s in ListaServizi1)
{
StringStack.Push(s);
}
bindSource = new BindingSource();
bindSource.DataSource = StringStack;
listBox1.DataSource = bindSource;
}
Now the "hard part" here is getting the ListBox to update when you have modified the Stack. Here's an example of what you have to do:
StringStack.Push("item 21");
bindSource = new BindingSource();
bindSource.DataSource = StringStack;
listBox1.DataSource = bindSource;
I suggest you consider the possible "cost" (computation ? memory use ?) making many frequent updates of the Stack ... note that I have never tried to really evaluate this technically.
If I were using this generic Stack in this way, I'd probably modify its code to raise an Event when the data changed; then the user would subscribe to that event, and perform the UI update in that EventHandler. This would be easy to do.
Of course, I might also be associating stack values with some other object/entity in a Dictionary, or other data structure, and then setting the 'ValueMember of the ListBox.
If you are using a ListView, binding is much more difficult, but you can find resources on how to do it, like here, on CodeProject: [
^], or here: [
^].
I assume this is a Windows Forms Project, and you are using a ListBox, or ListView Control: if that's not correct please revise your post.
Have you studied how to do binding, and use 'DataSource, 'ValueMember, and 'DisplayMember tools ?