Tutorial :Information Label



Question:

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?


Solution:1

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.


Solution:2

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.


Solution:3

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


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »