Vlasic Vila Panda
Vlasic Vila Panda

Reputation: 15

c# listbox i have trouble with

I created a list in a button ADD:

{
List <string> Names = new List<string>();

Names.Add(textBox1.Text);
textBox1.Text = " ";
}

I created another button SHOW NAMES and i want these names I entered in the list, to be listed in the listbox? How can this be done?

Upvotes: 0

Views: 53

Answers (1)

1.618
1.618

Reputation: 1765

First, you need to move that first line outside of the button click method, because if you declare the list inside the method, it will be gone once that method returns.

For your SHOW NAMES method, if all you want to do is display the list, you could use a TextBlock instead of a listbox, and it will be a little easier:

    TextBlock tb = new TextBlock();
    tb.text = string.Concat(Names);

Upvotes: 1

Related Questions