WinForms: Can a textlabel be used as a progress indicator?

  • Thread starter Thread starter Ken Arway
  • Start date Start date
K

Ken Arway

I've got a tab page on a tab control in a Windows form. The page
includes a radio button (UseURL), a URL textbox (URLTextBox), a "Load"
button (LoadButton) and a text label (LoadingLabel). The LoadingLabel
default text colour is gray, the default text is "Nothing Loaded".
When LoadButton is clicked, the URL is retrieved and its contents written
into a separate textbox (Strings), then LoadingLabel displays "Ready" in
white type. This works OK. What I want to also happen is for LoadingLabel
to display the text "Loading..." in yellow type while the URL is being
retrieved. Doesn't work -- the default text/colour remains until the URL is
finished being processed, then "Ready" is displayed. Here's the code:

private void LoadButton_Click(object sender, System.EventArgs e) {
if (UseURL.Checked == true) {
string contents = null;
string url = URLTextBox.Text;
LoadingLabel.ForeColor = System.Drawing.Color.Yellow;
LoadingLabel.Text = "Loading...";

HttpWebRequest wreq = (HttpWebRequest) WebRequest.Create(url);
using(HttpWebResponse wresp = (HttpWebResponse) wreq.GetResponse()) {
using(StreamReader sr = new StreamReader(wresp.GetResponseStream())) {
contents = sr.ReadToEnd();
sr.Close();
}
wresp.Close();
}

Strings.Text = contents;
LoadingLabel.ForeColor = System.Drawing.Color.White;
LoadingLabel.Text = "Ready";
}
}

Any ideas?

Ken
 
Hi Ken,

I havent test this, but what if you do this:
string contents = null;
string url = URLTextBox.Text;
LoadingLabel.ForeColor = System.Drawing.Color.Yellow;
LoadingLabel.Text = "Loading...";

LoadingLabel.Invalidate();
LoadingLabel.Update();

humm, now that I think about maybe a Application.DoEvents() would do the
trick too

Hope this help,
 
Hi Ignacio,
Yesssss!! LoadingLabel.Update(); was all that was needed.

Many thanks,
Ken
 
Back
Top