开发者

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 开发者_StackOverflowform 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?


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.


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜