469,572 Members | 1,159 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,572 developers. It's quick & easy.

Easy(?) one about Label.Text

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?

Apr 6 '07 #1
2 2351
On Fri, 06 Apr 2007 10:18:02 -0700, cashdeskmac
<ca*********@discussions.microsoft.comwrote:
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
Apr 6 '07 #2
Many thanks Peter,

refresh did the trick.
Apr 6 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Tim Mulholland | last post: by
6 posts views Thread by Joe | last post: by
1 post views Thread by Antonio Policelli | last post: by
1 post views Thread by mouac01 | last post: by
3 posts views Thread by Dominique | last post: by
4 posts views Thread by stevewy | last post: by
8 posts views Thread by Allen Maki | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.