mlow01
asked on
How do I see individual Items added to a Winform Listbox control as they're added at runtime?
I'm creating a Windows Forms application and want to create a Status window. I'm using a Listbox control and want to allow the user to see items appear as they're added at runtime. It appears that all items must first be added, then they're displayed in the Listbox.
I have a Form (Form1) that has a button and when the button is clicked a second form (Form2), which contains Listbox, s shown. Form2 will perform various operations whose statuses I'd like to display as they occur. To keep things simple for now, I'm only adding hard-coded text values to try and accomplish the desired output and can't seem to figure this out. I'd like my Listbox output to display as follows after the button on Form1 is clicked:
Item 1 added (some pause)
Item 2 added (some pause)
Item 3 added
Finished
I have a Form (Form1) that has a button and when the button is clicked a second form (Form2), which contains Listbox, s shown. Form2 will perform various operations whose statuses I'd like to display as they occur. To keep things simple for now, I'm only adding hard-coded text values to try and accomplish the desired output and can't seem to figure this out. I'd like my Listbox output to display as follows after the button on Form1 is clicked:
Item 1 added (some pause)
Item 2 added (some pause)
Item 3 added
Finished
Form1's Button click event handler:
private void Button1_Click(object sender, EventArgs e)
{
Form2 frmForm2 = new Form2();
frmForm2 .Show();
}
Form2's Load Event handler:
private void Form2_Load(object sender, EventArgs e)
{
listBox1.Items.Add("Item 1 added");
Thread.Sleep(5000);
listBox1.Items.Add("Item 2 added");
Thread.Sleep(5000);
listBox1.Items.Add("Item 3 added");
Thread.Sleep(5000);
listBox1.Items.Add("Finished");
}
you will have probably have to do this in a backgroundworker thread to release the ui so the user can see it as it is processing
ASKER
I'd thought of this as well and tried the following to no avail (Form2 opens, displays no results then closes):
private void Button1_Click(object sender, EventArgs e)
{
ThreadStart threadDelegate = new ThreadStart(DoWork);
Thread thdWorker = new Thread(threadDelegate);
thdWorker.Start();
}
private static void DoWork()
{
Form2 frmForm2 = new Form2 ();
frmForm2 .Show();
Control[] ctls = frmForm2 .Controls.Find("listbox1", false);
ListBox lb = (ListBox)ctls[0];
Thread.Sleep(1000);
lb.Items.Add("Item1");
Thread.Sleep(1000);
lb.Items.Add("Item2");
Thread.Sleep(1000);
lb.Items.Add("Item3");
lb.Items.Add("Finished");
}
private void Button1_Click(object sender, EventArgs e)
{
ThreadStart threadDelegate = new ThreadStart(DoWork);
Thread thdWorker = new Thread(threadDelegate);
thdWorker.Start();
}
private static void DoWork()
{
Form2 frmForm2 = new Form2 ();
frmForm2 .Show();
Control[] ctls = frmForm2 .Controls.Find("listbox1",
ListBox lb = (ListBox)ctls[0];
Thread.Sleep(1000);
lb.Items.Add("Item1");
Thread.Sleep(1000);
lb.Items.Add("Item2");
Thread.Sleep(1000);
lb.Items.Add("Item3");
lb.Items.Add("Finished");
}
After you issue a Start() to a worker thread, you need to issue a:
Thread.Sleep(0);
on the current thread so the worker can get up and running.
Jim
Thread.Sleep(0);
on the current thread so the worker can get up and running.
Jim
use the BackgroundWorker class
and add the items in the ReportProgress function.
and add the items in the ReportProgress function.
I would try lb.Invalidete(0 after each item is added.
Jim
Jim
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks Monarch! I would have NEVER came up with this solution on my own.