How do I add items from a list to a listbox one at a time?

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
<span style="font-family:Verdana,sans-serif; font-size:9pt Im using a dispatcher timer to add an item from a list to a listbox every three seconds once a button has been clicked. When I click the button every item in the list is displayed in the listbox,
but what I want is for the listbox to be populated one at a time every three seconds.
<span style="font-family:Verdana,sans-serif; font-size:9pt I hope I have explained this well.

<span style="font-family:Verdana,sans-serif; font-size:9pt Heres my code
<span style="font-family:Consolas; color:#2b91af; font-size:10pt List
<span style="font-family:Consolas; font-size:10pt <<span style="color:#2b91af Names> name =
<span style="color:blue new <span style="color:#2b91af List<<span style="color:#2b91af Names>();
<span style="font-family:Consolas; color:blue; font-size:10pt private<span style="font-family:Consolas; font-size:10pt
<span style="color:#2b91af DispatcherTimer time;
<span style="font-family:Consolas; color:blue; font-size:10pt int<span style="font-family:Consolas; font-size:10pt x = 0;
<span style="font-family:Consolas; color:blue; font-size:10pt public<span style="font-family:Consolas; font-size:10pt MainWindow()
<span style="font-family:Consolas; font-size:10pt {
<span style="font-family:Consolas; font-size:10pt InitializeComponent();
<span style="font-family:Consolas; font-size:10pt time =
<span style="font-family:Consolas; color:blue; font-size:10pt new<span style="font-family:Consolas; font-size:10pt
<span style="color:#2b91af DispatcherTimer();
<span style="font-family:Consolas; font-size:10pt time.Interval =
<span style="font-family:Consolas; color:#2b91af; font-size:10pt TimeSpan<span style="font-family:Consolas; font-size:10pt .FromMilliseconds(3000);
<span style="font-family:Consolas; font-size:10pt time.Tick +=
<span style="font-family:Consolas; color:blue; font-size:10pt new<span style="font-family:Consolas; font-size:10pt
<span style="color:#2b91af EventHandler(timer_Tick);
<span style="font-family:Consolas; font-size:10pt time.Start();
<span style="font-family:Consolas; font-size:10pt }
<span style="font-family:Consolas; color:blue; font-size:10pt private<span style="font-family:Consolas; font-size:10pt
<span style="color:blue void time _Tick(<span style="color:blue object sender,
<span style="color:#2b91af EventArgs e)
<span style="font-family:Consolas; font-size:10pt {
<span style="font-family:Consolas; font-size:10pt x++;
<span style="font-family:Consolas; font-size:10pt name.Add(
<span style="font-family:Consolas; color:blue; font-size:10pt new <span style="color:#2b91af
Names <span style="font-family:Consolas; font-size:10pt () { newName = <span style="color:#a31515
"John " });
<span style="font-family:Consolas; font-size:10pt name.Add(
<span style="font-family:Consolas; color:blue; font-size:10pt new <span style="color:#2b91af
Names <span style="font-family:Consolas; font-size:10pt () { newName = <span style="color:#a31515
"James " });
<span style="font-family:Consolas; font-size:10pt name.Add(
<span style="font-family:Consolas; color:blue; font-size:10pt new <span style="color:#2b91af
Names <span style="font-family:Consolas; font-size:10pt () { newName = <span style="color:#a31515
"Jack " });
<span style="font-family:Consolas; font-size:10pt name.Add(
<span style="font-family:Consolas; color:blue; font-size:10pt new <span style="color:#2b91af
Names <span style="font-family:Consolas; font-size:10pt () { newName = <span style="color:#a31515
"Jim " });
<span style="font-family:Consolas; color:blue; font-size:10pt foreach<span style="font-family:Consolas; font-size:10pt (<span style="color:#2b91af Names aName
<span style="color:blue in name)
<span style="font-family:Consolas; font-size:10pt listbox1.Items.Add(aName);
<span style="font-family:Consolas; color:blue; font-size:10pt if<span style="font-family:Consolas; font-size:10pt (x == 4)
<span style="font-family:Consolas; font-size:10pt time.Stop();
<span style="font-family:Consolas; font-size:10pt }
<span style="font-family:Consolas; color:blue; font-size:10pt private<span style="font-family:Consolas; font-size:10pt
<span style="color:blue void button1_Click(<span style="color:blue object sender,
<span style="color:#2b91af RoutedEventArgs e)
<span style="font-family:Consolas; font-size:10pt {
<span style="font-family:Consolas; font-size:10pt time.Start();
<span style="font-family:Consolas; font-size:10pt }

View the full article
 
Back
Top