tags:

views:

59

answers:

4
 private void Discogs_NewStatusMessage(object sender, NewStatusMessageEventArgs e)
    {
        textBox1.Text += e.Message() + "\r\n";
    }

I have the above event handler on my form and am trying to update a textbox on the form to show messages that occur at different points in code from a class to show progress.

All the messages appear on the textbox, but not until after the class code has finished.

What am I doing wrong?

+3  A: 

You should put the operation on a separate thread and then invoke the UI thread when progress is made. See this post on how to achieve separate threads.

Michael G
DUDE! Don't just blithely recommend Application.DoEvents() there are many many many complex and utterly horrific ways you can seriously break things by using that. Caveat. CAVEAT! Your first suggestion is full of win though. +1
Quibblesome
I aggree with @Quibblesome
Fredrik Mörk
Added warning ;)
Michael G
If you want one for free: Imagine a timer tick. It does so much work in it's Tick handler that it actually takes as long as the Interval value is. Then it calls Application.DoEvents(). This makes the timer fire again. If this keeps up you have a Stackoverflow.
Quibblesome
Edit: Removed DoEvents. Thanks Quibblesome for pointing my out my lack of knowledge. :)
Michael G
You don't need to remove it tbf, its a valid way of doing things. It's just..... there are risks! :)
Quibblesome
+2  A: 

You need to try to refresh the textbox, so that the UI updates with the changes.

astander
Figured it out moments after I posted. Thank you!
Dave
A: 

Answer my own question with the textbox.refresh() method thanks.

Dave
astander beat you by a few seconds.
Mark Byers
A: 

The event that you can use is the TextChanged Event Handler, here's an example, when the textbox is empty, the background changes to crimson when it's filled, the background changes to the default colour:

private void textBox1_TextChanged(object sender, EventArgs e){
    if (this.textBox1.TextLength == 0)
    {
       this.textBox1.BackColor = System.Drawing.Color.Crimson;
    }
    else
    {
       this.textBox1.BackColor = System.Drawing.SystemColors.Window;
    }
}

You get the idea, you can even set it to display a label showing the number of characters entered by using the TextLength property, updating each time.

Hope this helps, Best regards, Tom.

tommieb75