开发者

Information Label

I am trying to make开发者_Go百科 an information label that should display the events the application doing.

deleting data, reading data, writing data, connection to db. etc..

I've added a label to my form and I am changing its text property for each operation like:

label1.Text = "stored procedure is being executed..";

But, in the run time the text of the label does not change. I've tried to add the method

Application.DoEvents();

to every place I've changed the label's text property. It works fine. But it looks silly. So I've tried to add this method to my label's "TextChanged" event. But it does not work! Am I doing something wrong? Or,

is there any efficient way to make an information label?


You should use a BackgroundWorker; handle the ProgressChanged Event; and call ReportProgress in your DoWork handler. With this method, you can avoid the cross-threading exceptions.


Perhaps setting the label text is happening in a separate thread than what the label is in? A delegate would solve that problem.

    public void setLabelText(string value)
    {
        if (InvokeRequired)
            Invoke(new SetTextDelegate(setLabelText), value);
        else
            label1.Text = value;
    }

    delegate void SetTextDelegate(string value); 

Whenever you want to update your label's text, you can then call the setLabelText method.


If the issues were threading related you would be getting exceptions.

The app is not updating the UI updating because its busy doing other stuff, like hitting the database.

Start with a simple method on the form like this:

private void SetStatus(string status)
{
    label1.Text = status;
    Application.DoEvents();
}

Then you just call SetStatus("executing...") etc in your code.

If you do the treading stuff you'll need to use the invoke method mentioned by @Aaron

The background worker suggestions are good too but if the above works you may not need it, workers make things a bit more interesting!

PK :-)

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜