4

I want to be able to set any label's visibility to true or false with a method. I have this code:

private void Change_Visible(Label toBeChanged)
{
    if (toBeChanged.Visible == false)
    {
        toBeChanged.Visible = true;
    }
    else
    {
        toBeChanged.Visible = false;
    }
    toBeChanged.Refresh();
}

I call to this code with:

    Change_Visible(myLabel);
    //
    // Do other things
    //
    Change_Visible(myLabel);

In my specific example, myLabel is set to not be visible at the load of my form. After the first call to Change_Visible it becomes visible, but after the second call to Change_Visible nothing happens. Can you help me make it disappear?

I have already tried some other logics looking for a solution - for example:

private void Change_Visible(Label toBeChanged)
{
    if (toBeChanged.Visible == false)
    {
        toBeChanged.Visible = true;
        toBeChanged.Refresh();
        return;
    }
    if (toBeChanged.Visible == true)
    {
        toBeChanged.Visible = false;
        toBeChanged.Refresh();
        return;
    }
}

I have not come to any conclusions. I am learning C# by myself and started just a few weeks ago, so maybe I am missing something obvious. Thanks for your help!

dandan78
  • 13,328
  • 13
  • 64
  • 78
Elder Taylor
  • 53
  • 1
  • 1
  • 6
  • make sure you are not blocking UI thread after second call to change_visible – Parv Sharma Oct 19 '13 at 19:29
  • Everything appears to be fine - could you introduce some more context?. The Label will actually be invalidated when the `Label.Visible` property is _set_ which means calling `Label.Refresh` is actually redundant here. You could actually replace the entire method body with `return toBeChanged.Visible = !toBeChanged.Visible;`. – User 12345678 Oct 19 '13 at 19:31
  • It didn't show the label the first time until I added the Label.Refresh() line, that is why it is there. – Elder Taylor Oct 19 '13 at 19:34

4 Answers4

8

The label won't actually become invisible to the user until its Parent redraws its background, painting over the pixels formerly drawn by the label control. This doesn't happen in your code, painting only occurs when the main thread goes idle and re-enters the message loop. It isn't idle, it is executing those "other things".

A workaround is to ask the parent to paint itself early:

myLabel.Visible = false;
this.Update();
//
// Do other things
//
myLabel.Visible = true;

Code that runs long enough to require feedback like this ought to be run in a worker thread instead. Use a BackgroundWorker or Task.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • Adding this.Update(); worked perfectly. I also looked into BackgroundWorker and think that would help a lot with what I am doing. Thanks for the answer! – Elder Taylor Oct 20 '13 at 17:32
0

Maybe try passing the label into your method by reference?

Change_Visible(ref myLabel);
//
// Do other things
//
Change_Visible(ref myLabel);
stayinwett
  • 79
  • 2
  • 8
0

i think the only problem with this code is that since the label is being refreshed in the UI thread it will wait for the paint event again.

for answer look at this link Why won't control update/refresh mid-process

Community
  • 1
  • 1
Parv Sharma
  • 12,581
  • 4
  • 48
  • 80
0

Just execute the following lines after your Visible/Enable changes and the job is done!

//Explicit call for render
this.Update(); 
Joel
  • 7,401
  • 4
  • 52
  • 58