tags:

views:

80

answers:

3
+2  Q: 

Information Label

I am trying to make 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?

+1  A: 

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.

Aaron
This will not always update the Text property if the application is busy.
AMissico
+2  A: 

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.

Austin Salonen
wow that is awesome. Thanks a lot Austin..
caisenm
I've realised something. BackGround worker is cool, but the RunWorkerAsync method (which calls progress_changed evetn) requires one parameter (object). But my method takes multiple parameters. I have to expand the sample to be able to use it ;)
caisenm
+1  A: 

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 :-)

Paul Kohler
I think this answer is the easiest way to do this ;)
caisenm
Did you end up needing to use the workers etc?
Paul Kohler