How to Change Form label Quickly
i am developing an application but i am unable to change the text of label quickly
i am using following code
for(int i=0;i<199;i++)
label1.text=Convert.ToString(i);
onlinessp
Junior Poster in Training
80 posts since Apr 2010
Reputation Points: 7
Solved Threads: 0
Instead of Convert.ToString(i) you could use i.ToString()
ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661
For the new text to show on your label you may need to invalidate the label and will need to give the ui time to refresh.
nick.crane
Nearly a Posting Virtuoso
1,230 posts since Feb 2010
Reputation Points: 375
Solved Threads: 187
i try this code but both these codes hang up the form and don't shows the changing label
onlinessp
Junior Poster in Training
80 posts since Apr 2010
Reputation Points: 7
Solved Threads: 0
Can you give me some source code from which i can learn how to change the label and give ui time
Thanks on your kind reply
onlinessp
Junior Poster in Training
80 posts since Apr 2010
Reputation Points: 7
Solved Threads: 0
Well that is the correct rusult you will hang the label like that
try adding a timer with a one second delay instead of using a for loop.
finito
Nearly a Posting Virtuoso
1,321 posts since May 2010
Reputation Points: 60
Solved Threads: 135
finito
Nearly a Posting Virtuoso
1,321 posts since May 2010
Reputation Points: 60
Solved Threads: 135
The problem is that i have to change 10 values with in one second not one value.so i am unable to use time.And its becoming great challenge for me.
onlinessp
Junior Poster in Training
80 posts since Apr 2010
Reputation Points: 7
Solved Threads: 0
This works, but it lock the UI thread.
for (int i = 0; i < 200; i++)
{
label1.Text = i.ToString();
label1.Refresh();
System.Threading.Thread.Sleep(10);
}
Using a System.Windows.Form.Timer gets arround that.
int i = 0;
private void timer1_Tick(object sender, EventArgs e)
{
label1.Text = i.ToString();
label1.Refresh();
if (i++ > 199)
timer1.Stop();
}
private void button1_Click(object sender, EventArgs e)
{
i = 0;
timer1.Interval = 10;
timer1.Start();
}
nick.crane
Nearly a Posting Virtuoso
1,230 posts since Feb 2010
Reputation Points: 375
Solved Threads: 187
can another way through which UI thread remain running
onlinessp
Junior Poster in Training
80 posts since Apr 2010
Reputation Points: 7
Solved Threads: 0
Yes. You have to yield CPU time to UI thread in order to keep its message pump working:
for (int i = 0; i < 200; i++)
{
label1.Text = i.ToString();
label1.Refresh();
Application.DoEvents();
System.Threading.Thread.Sleep(10);
}
Teme64
Veteran Poster
1,031 posts since Aug 2008
Reputation Points: 218
Solved Threads: 203
i try it but it's not working UI is also unable to move
Thanks on your kind reply
onlinessp
Junior Poster in Training
80 posts since Apr 2010
Reputation Points: 7
Solved Threads: 0
Here's a way to do it with multithreading:
public partial class Form1 : Form
{
Thread Updater;
public Form1()
{
InitializeComponent();
}
public void threadMain()
{
for (int i = 0; i < 200; i++)
{
this.UpdateLabel(i.ToString());
System.Threading.Thread.Sleep(10);
}
}
// Make a thread-safe update
public delegate void SetUpdateLabelCallback(string text);
public void UpdateLabel(string text)
{
if (this.label1.InvokeRequired)
{
SetUpdateLabelCallback upd = new SetUpdateLabelCallback(UpdateLabel);
this.Invoke(upd, new object[] { text });
}
else
{
label1.Text = text;
label1.Refresh();
}
Application.DoEvents();
}
private void button1_Click(object sender, EventArgs e)
{
Updater = new Thread(new ThreadStart(threadMain));
Updater.Start();
}
}
Now you have two threads (UI and for-loop) running at the same time so this should be as responsive as it can be.
Teme64
Veteran Poster
1,031 posts since Aug 2008
Reputation Points: 218
Solved Threads: 203