Home All Groups Group Topic Archive Search About

Easy(?) one about Label.Text

Author
6 Apr 2007 5:18 PM
cashdeskmac
Hi,

I have a Label on a Windows form (Version 1.1.4322) and while I iterate
recursively through a method I want to show the name of the current file
being copied to another directory.

The Label is not visible until the backup begins, at which point  I resize
the form to show the Label and a ProgressBar.  As each file is copied, the
Label is updated:

foreach(FileInfo file in dir.GetFiles())
{
      file.CopyTo(fullpath + "\\" + file.Name);
      lblFileName.Text = "Copying file " + file.FullName;
}

There is other code in this loop but not relevant (increment the
progressbar, decrement a counter for the remaining files, things like that).

The problem I  have is that nothing appears in the Label.

I set the Label's Visible property to false in the Load event handler, and
only set it to true after I have resized the form during the backup.  Once
the backup is complete, I set visible to false again and reset the form to
it's original size.

I even tried setting it to false in the designer and put some dummy text in
to see what it showed.  The dummy text didn't appear.  The text appeared when
I set visible to true in the designer, but it didn't change.

Stepping through the code, the Label is definitely showing as visible and
the text reflects the current file name, but it refuses to show this on the
form.

Does anyone have any ideas?

Author
6 Apr 2007 5:34 PM
Peter Duniho
On Fri, 06 Apr 2007 10:18:02 -0700, cashdeskmac 
<cashdesk***@discussions.microsoft.com> wrote:

> I have a Label on a Windows form (Version 1.1.4322) and while I iterate
> recursively through a method I want to show the name of the current file
> being copied to another directory.
>
> [...]
> The problem I  have is that nothing appears in the Label.
>
> [...]
> Stepping through the code, the Label is definitely showing as visible and
> the text reflects the current file name, but it refuses to show this on 
> the form.
>
> Does anyone have any ideas?

I get the impression that the code doing the work is in the main form's 
thread.  If that's true, then you aren't getting to a point where the 
label is able to redraw itself.  Typically, when a control's visual status 
changes, this is handled by adding a message to the control's message 
queue telling it to redraw itself.  Until the control has a chance to 
process its messages, it won't redraw.

You can address this either by moving the processing into a different 
thread, which will allow the control's thread to continue to process 
messages, or you can use the Refresh() method to force the control to 
redraw after you change the data.  IMHO, the former is preferable, but 
it's also a little more complicated (though not by much...the worst part 
is that you'll want to look at BeginInvoke for actually changing the 
label's Text property from the processing thread).

In your particular scenario, copying files, I think that using Refresh() 
is fine.  It's hard to imagine a scenario where the disk i/o isn't 
significantly slower than the video i/o required to update the label.  But 
for other tasks, the overhead updating the label for each iteration could 
wind up becoming a major component of the total processing and so you 
wouldn't want to refresh the label each time through the loop.

Pete
Are all your drivers up to date? click for free checkup

Author
6 Apr 2007 6:24 PM
cashdeskmac
Many thanks Peter,

refresh did the trick.

Bookmark and Share