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
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");
 }

Open in new window

mlow01Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

p_davisCommented:
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
0
mlow01Author Commented:
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");

        }
0
JimBrandleyCommented:
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
0
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

p_davisCommented:
use the BackgroundWorker class

and add the items in the ReportProgress function.
0
JimBrandleyCommented:
I would try lb.Invalidete(0 after each item is added.

Jim
0
monarch_ilhanCommented:
Here is the Form2.cs codes.
calling form is same,

From2 frm=new Form2();
frm2.Show();
public partial class Form2 : Form
    {
        private delegate void AddItem(object item);
        private bool Loaded = false;
        public Form2()
        {
            InitializeComponent();
        }
 
 
 
        private void Form2_Activated(object sender, EventArgs e)
        {
            if (Loaded) return;
 
            Loaded = true;
            AddItem add = new AddItem(AddItemToListBox);
            
            for (int i = 0; i < 300; i++)
            {
                listBox1.Invoke(add,i);
                
            }
 
        }
 
        private void AddItemToListBox(object item)
        {
            listBox1.Items.Insert(0,item);
            Thread.Sleep(30);
            Application.DoEvents();
        }
    }

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mlow01Author Commented:
Thanks Monarch! I would have NEVER came up with this solution on my own.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.