Dave
Dave

Reputation: 71

How do I make a textbox update as an event happens not when the code has finished?

 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?

Upvotes: 0

Views: 572

Answers (3)

t0mm13b
t0mm13b

Reputation: 34592

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.

Upvotes: 0

Michael G
Michael G

Reputation: 6755

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.

Upvotes: 3

Adriaan Stander
Adriaan Stander

Reputation: 166486

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

Upvotes: 2

Related Questions