I have created a C# windows application in that I inserted the progress bar.
When a button is clicked the progress bar should appear and then it should start the process for some 2 to 3 seconds and when the process bar is completed it should be hidden.
I have used this code to solve this but its not working.
While the Progress bar is running, the label box that should be like "Generating... 45%" and after completing the label box should be "Generated 100%..", but when I insert the label its showing some errors.
Here is the picture before clicking the Generate button..
On Processing I Should get like this..
On Final Process id should be like this and the progress bar should hidden..
ProgressBar1.Visible = true;
if (isProcessRunning)
{
MessageBox.Show("A process is already running.");
return;
}
Thread backgroundThread = new Thread(
new ThreadStart(() =>
{
isProcessRunning = true;
for (int n = 0; n < 100; n++)
{
Thread.Sleep(1);
progressBar1.BeginInvoke(new Action(() => progressBar1.Value = n));
}
MessageBox.Show("Generated!!!");
if (progressBar1.InvokeRequired)
progressBar1.BeginInvoke(new Action(() => progressBar1.Value = 0));
isProcessRunning = false;
}
));
// Start the background process thread
backgroundThread.Start();
I suggest you to use BackgroundWorker to show progress bar in C# winform .
Here is an example ,
public partial class Form1 : Form
{
BackgroundWorker bgw = new BackgroundWorker();
public Form1()
{
InitializeComponent();
label1.Text = "";
label2.Text = "";
}
private void button1_Click_1(object sender, EventArgs e)
{
bgw.DoWork += new DoWorkEventHandler(bgw_DoWork);
bgw.ProgressChanged += new ProgressChangedEventHandler(bgw_ProgressChanged);
bgw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bgw_RunWorkerCompleted);
bgw.WorkerReportsProgress = true;
bgw.RunWorkerAsync();
}
void bgw_DoWork(object sender, DoWorkEventArgs e)
{
int total = 57; //some number (this is your variable to change)!!
for (int i = 0; i <= total; i++) //some number (total)
{
System.Threading.Thread.Sleep(100);
int percents = (i * 100) / total;
bgw.ReportProgress(percents, i);
//2 arguments:
//1. procenteges (from 0 t0 100) - i do a calcumation
//2. some current value!
}
}
void bgw_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
progressBar1.Value = e.ProgressPercentage;
label1.Text = String.Format("Progress: {0} %", e.ProgressPercentage);
label2.Text = String.Format("Total items transfered: {0}", e.UserState);
}
void bgw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
//do the code when bgv completes its work
}
}
You can set your progress bar's visible to false in bgw_RunWorkerCompleted .
The following links will show how to use backgroundworker
DotNetPerls
MSDN Reference
CodeProject
Good Luck :)
This is the Code i am using backgroundWorker..
public partial class Form1 : Form
{
BackgroundWorker bgw = new BackgroundWorker();
public Form1()
{
InitializeComponent();
label3.Text = "";
this.StartPosition = FormStartPosition.CenterScreen;
}
private void button2_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void btn_generate_Click(object sender, EventArgs e)
{
progressBar1.Visible = true;
bgw.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);
bgw.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged);
bgw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker1_RunWorkerCompleted);
bgw.WorkerReportsProgress = true;
bgw.RunWorkerAsync();
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
int total = 100; //some number (this is your variable to change)!!
for (int i = 0; i <= total; i++) //some number (total)
{
System.Threading.Thread.Sleep(10);
int percents = (i * 100) / 100;
bgw.ReportProgress(percents, i);
}
}
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
progressBar1.Value = e.ProgressPercentage;
label3.Text = String.Format("Progress: {0} %", e.ProgressPercentage);
if (e.ProgressPercentage == 100)
{
label3.Text = String.Format("Generated.. {0} %", e.ProgressPercentage);
}
// label2.Text = String.Format("Total items transfered: {0}", e.UserState);
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
progressBar1.Visible = false;
}
}`
In this There is a problem in the below code when i click the button for first time the progress bar runs one time and if i clicked second time it runs for 2 times and so on.. other wise the code is perfectly working..
public partial class Form1 : Form {
BackgroundWorker bgw = new BackgroundWorker();
public Form1()
{
InitializeComponent();
label3.Text = "";
this.StartPosition = FormStartPosition.CenterScreen;
}
private void button2_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void btn_generate_Click(object sender, EventArgs e)
{ new BackgroundWorker();
progressBar1.Visible = true;
bgw.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);
bgw.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged);
bgw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker1_RunWorkerCompleted);
bgw.WorkerReportsProgress = true;
bgw.RunWorkerAsync();
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
int total = 100; //some number (this is your variable to change)!!
for (int i = 0; i <= total; i++) //some number (total)
{
System.Threading.Thread.Sleep(10);
int percents = (i * 100) / 100;
bgw.ReportProgress(percents, i);
}
}
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
progressBar1.Value = e.ProgressPercentage;
label3.Text = String.Format("Progress: {0} %", e.ProgressPercentage);
if (e.ProgressPercentage == 100)
{
label3.Text = String.Format("Generated.. {0} %", e.ProgressPercentage);
}
// label2.Text = String.Format("Total items transfered: {0}", e.UserState);
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
progressBar1.Visible = false;
}
}
Related
I need to use progressbar.value property at different locations. But the problem is, while executing it shows only maximum value given. I need to stop at 25% and 75% and after some delay, 100%. How can I overcome this problem. Thanks in Advance...
C#
namespace ProgressBarWindowForm
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public void Form1_Load(object sender, System.EventArgs e)
{
label1.Hide();
progressBar1.Minimum = 0;
progressBar1.Maximum = 100;
}
private void button1_Click(object sender, EventArgs e)
{
progressBar1.Value = 25;
if (progressBar1.Value == 25)
{
label1.Show();
label1.Text = "Process Complete 25%";
}
progressBar1.Value = 75;
if (progressBar1.Value == 75)
{
label1.Show();
label1.Text = "Process Complete 75%";
}
}
}
}
Progressbar control name is progressBar1,
Label name is label1 and
Button name is button1
When I Clicked the Button, progressbar value is directly filling with 75%. I want to stop it at 25% and after some delay it should fill 75% and then 100%...Can anyone help..Can I use "progressBar1.value" only Once or as many times I need?
try this,Drag and drop background worker in windows form
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
backgroundWorker1.WorkerReportsProgress = true;
// This event will be raised on the worker thread when the worker starts
backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);
// This event will be raised when we call ReportProgress
backgroundWorker1.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged);
}
private void button1_Click(object sender, EventArgs e)
{
// Start the background worker
backgroundWorker1.RunWorkerAsync();
}
// On worker thread so do our thing!
void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
// Your background task goes here
for (int i = 0; i <= 100; i++)
{
// Report progress to 'UI' thread
backgroundWorker1.ReportProgress(i);
// Simulate long task
if (label1.InvokeRequired)
{
label1.Invoke(new MethodInvoker(delegate
{
label1.Show();
label1.Text = "Process Complete " + progressBar1.Value + "%";
}));
}
if (progressBar1.Value == 25 || progressBar1.Value == 75)
{
System.Threading.Thread.Sleep(1000);
}
System.Threading.Thread.Sleep(100);
}
}
// Back on the 'UI' thread so we can update the progress bar
void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
// The progress percentage is a property of e
progressBar1.Value = e.ProgressPercentage;
}
}
Use a Timer to update the progress bar after a delay:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
timer.Tick += Timer_Tick;
timer.Interval = 1000; // delay: 1000 milliseconds
}
Timer timer = new Timer();
private void Timer_Tick(object sender, EventArgs e)
{
if (progressBar1.Value == 100)
{
timer.Stop();
return;
}
progressBar1.Value += 25;
}
private void button1_Click(object sender, EventArgs e)
{
progressBar1.Value = 25;
timer.Start();
}
}
Its simple to update progressBar values in button click, you can initialize the properties in the page load or else use the designer, in page load it would be like the following:
private int ProgressPercentage = 10;
public void Form1_Load(object sender, System.EventArgs e)
{
progressBar1.Minimum = 0;
progressBar1.Maximum = 100;
progressBar1.Value = 0;
}
So the initialization completed, now you can code the button click like the following, through which you can update the progress bar in every button click:
private void button1_Click(object sender, EventArgs e)
{
progressBar1.Value += ProgressPercentage;
label1.Text = String.Format("Process Complete {0}%",progressBar1.Value);
}
If you want the update to be happens automatically in a particular interval means you can make use of a timer and enable the timer in the button click. Here you can find a similar thread which can be used to implement timer to your scene.
Update as per your comment, calling a delay will not be a best practice, you can make use a timer here as like the following:
System.Windows.Forms.Timer proTimer = new System.Windows.Forms.Timer();
private void Form1_Load(object sender, EventArgs e)
{
proTimer.Interval = 1000;
progressBar1.Minimum = 0;
progressBar1.Maximum = 100;
progressBar1.Value = 0;
proTimer.Tick += new EventHandler(proTimer_Tick);
}
private void button1_Click(object sender, EventArgs e)
{
proTimer.Enabled = true;
proTimer.Start();
}
// Timer event
void proTimer_Tick(object sender, EventArgs e)
{
progressBar1.Value += ProgressPercentage;
label1.Text = String.Format("Process Complete {0}%",progressBar1.Value);
if (progressBar1.Value == 100)
{
proTimer.Stop();
proTimer.Enbled = false;
}
}
You need to add a delay inbetween the changes. As of now, the button advance the bar to 25, sets the label, then advances the bar to 75 without pausing.
System.Threading.Thread.Sleep(n); will sleep n milliseconds, which you will need after the statement setting the 25 percent marker.
EDIT
If you want it the value to only progress on a button click, you will need to check the value of the progress bar before you advance it.
In pseudo code, something like:
onclick() {
if (progress == 0) {
progress = 25
label = the25MarkText
} else if (progress == 25) {
progress = 75
label = the75MarkText
}
}
I want to sync my progress bar in my project with the timer countdown.
This is what I has now:
namespace Timer1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private int counter=80;
DateTime dt = new DateTime();
private void button1_Click(object sender, EventArgs e)
{
int counter = 80;
timer1 = new System.Windows.Forms.Timer();
timer1.Tick += new EventHandler(timer1_Tick);
timer1.Interval = 1000;
timer1.Start();
textBox1.Text = counter.ToString();
}
private void timer1_Tick(object sender, EventArgs e)
{
counter--;
progressBar1.Increment(1);
if (counter == 0)
{
timer1.Stop();
}
textBox1.Text = dt.AddSeconds(counter).ToString("mm:ss");
}
private void Form1_Load(object sender, EventArgs e)
{
timer1.Enabled = false;
}
private void button2_Click(object sender, EventArgs e)
{
timer1.Stop();
textBox1.Clear();
progressBar1.Value = 0;
}
}
}
I want the progress bar to finish when the timer to ends.
You need to specify the progressBar1 to have its .Step and Maximum properties match the Interval and counter variable. For example:
private int counter = 80;
DateTime dt = new DateTime();
private void button1_Click(object sender, EventArgs e)
{
// The max is the total number of iterations on the
// timer tick by the number interval.
progressBar1.Max = counter * 1000;
progressBar1.Step = 1000;
timer1 = new System.Windows.Forms.Timer();
timer1.Tick += new EventHandler(timer1_Tick);
timer1.Interval = 1000;
timer1.Start();
textBox1.Text = counter.ToString();
}
private void timer1_Tick(object sender, EventArgs e)
{
counter--;
// Perform one step...
progressBar1.PerformStep();
if (counter == 0)
{
timer1.Stop();
}
textBox1.Text = dt.AddSeconds(counter).ToString("mm:ss");
}
I'm setting up a background worker for the first time. It is mostly working as the code runs and my stop/cancel button is working. However, I am also trying to report progress to update a progress bar but I cannot get this to fire at all.
I start the code from a button click which runs this code:
backgroundWorker1.WorkerSupportsCancellation = true;
backgroundWorker1.WorkerReportsProgress = true;
backgroundWorker1.RunWorkerAsync();//this invokes the DoWork event
My Do_Work method:
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker worker = sender as BackgroundWorker;
int j = 0;// Count cumulative imported files
int countDupFiles = 0;// Count number of previously imported csv files
int countImportedFiles = 0;// Count imported files
foreach (string folderPath in csvDirList)
{
string[] csvFileNames = Directory.GetFiles(#folderPath, "*.csv");
frmImportCsvData.replaceAll(csvFileNames, folderPath + "\\", "");
for (int i = 0; i < csvFileNames.Length; i++, j++)
{
string csvFilePath = folderPath + "\\" + csvFileNames[i];
if ((worker.CancellationPending == true))
{
e.Cancel = true;
break;
}
else
{
if (dataLayer.ImportCsvDataBkgrnd(this, csvFilePath, compIdValue, csvFileCount, i))//new method processes subdirectories if tick box selected
{
countImportedFiles = countImportedFiles + 1;
}
else
{
countDupFiles = countDupFiles + 1;
}
System.Threading.Thread.Sleep(500);
}
worker.ReportProgress(j);//tried using worker and backgroundWorker1 but neither works
backgroundWorker1.ReportProgress(j);
//string proj = j.ToString();
//MessageBox.Show(proj);//Displays incrementing j as expected when not commented out
}
}
if (countImportedFiles > 0)
MessageBox.Show(countImportedFiles + " files were imported.");
if (countDupFiles > 0)
MessageBox.Show(countDupFiles + " files were not imported. Matches all ready in Database.");
}
Trying to fire either of these ProgressChanged events:
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
string tbProgress = (e.ProgressPercentage.ToString() + "%");
MessageBox.Show(tbProgress + "backgroundWorker1");
importProgressBar(e.ProgressPercentage);
}
private void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
string tbProgress = (e.ProgressPercentage.ToString() + "%");
MessageBox.Show(tbProgress + "worker");
importProgressBar(e.ProgressPercentage);
}
Finally, I want the ProgressChanged event to trigger this method to update my progress bar:
public void importProgressBar(int i)
{
progressTableLayoutPanel.Visible = true;//display progress bar
int percProgress = 100 * (i + 1) / csvFileCount;
if (percProgress <= 99)// Required to prevent values above 100 that crash the code
progressBar.Value = percProgress + 1;//hack that makes the progress bar update when progress value decreases
progressBar.Value = percProgress;
percProgressLabel.Text = percProgress.ToString();
progressTableLayoutPanel.Update();//Required to display all progress bar table contents
//Thread.Sleep(200);
if (percProgress >= 100)
{
Thread.Sleep(200);
progressTableLayoutPanel.Visible = false;
}
}
The cancel button code, which works, looks like this:
private void stopImportButton_Click(object sender, EventArgs e)
{
backgroundWorker1.CancelAsync();
}
The messageboxes in my ProgressChanged events never show up and my progress bar is never set to visible. Any ideas what the problem could be?
Check this example:
BackgroundWorker bgw = new BackgroundWorker();
public Form1()
{
InitializeComponent();
label1.Text = "";
label2.Text = "";
}
private void button1_Click_1(object sender, EventArgs e)
{
if (bgw == null)
{
bgw = new BackgroundWorker();
bgw.DoWork += new DoWorkEventHandler(bgw_DoWork);
bgw.ProgressChanged += new ProgressChangedEventHandler(bgw_ProgressChanged);
bgw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bgw_RunWorkerCompleted);
}
bgw.WorkerReportsProgress = true;
bgw.WorkerSupportsCancellation = true;
bgw.RunWorkerAsync();
}
void bgw_DoWork(object sender, DoWorkEventArgs e)
{
int total = 57; //some number (this is your variable to change)!!
for (int i = 0; i <= total; i++) //some number (total)
{
System.Threading.Thread.Sleep(100);
int percents = (i * 100) / total;
bgw.ReportProgress(percents, i);
//2 arguments:
//1. procenteges (from 0 t0 100) - i do a calcumation
//2. some current value!
}
}
void bgw_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
progressBar1.Value = e.ProgressPercentage;
label1.Text = String.Format("Progress: {0} %", e.ProgressPercentage);
label2.Text = String.Format("Total items transfered: {0}", e.UserState);
}
void bgw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
//do the code when bgv completes its work
}
}
Maybe this helps you with your problem...
And try to put the progress to visible just after you call the background.doWork in the button click event.
i wanted to make every 30 percent in progress bar, the text changes.
What i wanted to do is, when the progress bar hit 25 percent, the text changes and it stop for a second, and it goes back to 50, and the text changes again, it keep going until it hit 100 percent.
Here is my code:
public WelcomeScreen()
{
InitializeComponent();
_timer.Interval = 2000;
label1.ForeColor = Color.White;
}
private void WelcomeScreen_Load(object sender, EventArgs e)
{
var worker = new BackgroundWorker();
worker.WorkerReportsProgress = true;
worker.DoWork += worker_DoWork;
worker.RunWorkerCompleted += worker_RunWorkerCompleted;
worker.ProgressChanged += worker_ProgressChanged;
worker.RunWorkerAsync();
}
void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
var percentComplete = e.ProgressPercentage;
var userState = (string)e.UserState;
progressBar1.Minimum = 0;
progressBar1.Maximum = 100;
progressBar1.Value = 100;
progressBar1.Step = 25;
if (progressBar1.Step <= 25)
{
label1.Text = "Preparing Setup";
}
else if (progressBar1.Step <= 50)
{
label1.Text = "Preparing Application";
}
else if (progressBar1.Step <= 75)
{
label1.Text = "Preparing Database";
}
else if (progressBar1.Step <= 100)
{
label1.Text = "Preparing Contents";
}
else
{
label1.Text = "Launch Application";
}
}
void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
_timer.Enabled = true;
_timer.Tick += new EventHandler(Timer_Tick);
}
void worker_DoWork(object sender, DoWorkEventArgs e)
{
var currentWorker = (BackgroundWorker)sender;
currentWorker.ReportProgress(0, "Preparing Setup");
currentWorker.ReportProgress(25, "Preparing Application");
currentWorker.ReportProgress(50, "Preparing Database");
currentWorker.ReportProgress(75, "Preparing Contents");
currentWorker.ReportProgress(100, "Launch Application");
}
void Timer_Tick(object sender, EventArgs e)
{
_timer.Enabled = false;
this.Hide();
_login.ShowDialog();
this.Close();
}
When the progress bar hit 100 percent, i order the application to wait until 2 seconds before show another form after the text changes to "Launch Application".
All the code in your WelcomeScreen_Load will block the UI thread until it is complete. This means that no matter what you do to your progress bar, it will never show it's changes until it is complete (which it will "jump" to the last settings).
You will want to look into Background Workers. These let you do your code async, and report back every-so-often with what the current state is (ie: the percent complete).
So, a really short example:
private void WelcomeScreen_Load(object sender, EventArgs e)
{
var worker = new BackgroundWorker();
worker.WorkerReportsProgress = true;
worker.DoWork += worker_DoWork;
worker.RunWorkerCompleted += worker_RunWorkerCompleted;
worker.ProgressChanged += worker_ProgressChanged;
worker.RunWorkerAsync();
}
void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
var percentComplete = e.ProgressPercentage;
var userState = (string)e.UserState;
//do something with these values, like moving your progress bar
progressBar1.Minimum = 0;
progressBar1.Maximum = 100;
progressBar1.Value = percentComplete;
}
void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
// do something when the worker completes, like start your timer
}
void worker_DoWork(object sender, DoWorkEventArgs e)
{
// do the "work" for the background worker
var currentWorker = (BackgroundWorker)sender;
currentWorker.ReportProgress(0, "Just Starting");
// do your first task
currentWorker.ReportProgress(25, "Finish First Task");
// ...
}
How to display some messages on a C# form application with different time intervals with buttons?
Something like:
private void button1_Click(object sender, EventArgs e)
{
label1.Text = "string1";
[wait 3 seconds]
label1.Text = "string2";
[wait 5 sec]
label1.text="string 3";
[end]
}
You can create a new thread, change the label text, sleep that thread and so on so forth:
using System.Threading;
// Somewhere in your Form, for example in Form_Load event:
new Thread(new ThreadStart(delegate {
var d = new setLabelTextDelegate(setLabelText);
label1.Invoke(d, new object[] { "string 1" });
Thread.Sleep(3000); // sleep 3 seconds
label1.Invoke(d, new object[] { "string 2" });
Thread.Sleep(5000); // sleep 5 seconds
label1.Invoke(d, new object[] { "string 3" });
})).Start();
private delegate void setLabelTextDelegate(string text);
private void setLabelText(string text)
{
this.label1.Text = text;
}
Use a Timer with an interval of X milliseconds and update the UI each Timer Tick. Keep track of the number of Timer Ticks received so you'll know which string to use. After each update has been processed stop the Timer.
Other solutions posted might be wiser, but this one is pretty simple.
Form1 contains a simple Label called Label1 and a button called Button1
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
t.Interval = 100;
t.Tick += new EventHandler(t_Tick);
t.Start();
}
Timer t = new Timer();
int counter = 0;
private void Button1_Click(object sender, EventArgs e)
{
t.Start();
}
void t_Tick(object sender, EventArgs e)
{
try
{
t.Enabled = false; //Disable timer so we don't start t_Tick when t_Tick is still runnnig
if (counter == 0)
{
label1.Text = "string1";
t.Interval = 3000;
}
if (counter == 1)
{
label1.Text = "string2";
t.Interval = 5000;
}
if (counter == 2)
{
label1.Text = "string3";
t.Stop(); //Stop timer
}
else
{
t.Enabled = true; //Resume timer
}
counter++;
}
catch (Exception ex)
{
MessageBox.Show("Never throw exception from timer..." + ex.Message);
}
}
To do exactly as your pseudo code suggest simply use Thread.Sleep() in place of your [wait x] lines. Note that it will likely make UI unresponsive for the duration of waiting.
Alternatively you can create a thread that does the same thing but doesn't block the UI thread. The only issue there is that you have to define delegate in UI thread otherwise it wont work.
private void button1_Click(object sender, EventArgs e)
{
ThreadPool.QueueUserWorkItem(delegate()
{
label1.Text = "string1";
Thread.Sleep(3000);
label1.Text = "string2";
Thread.Sleep(5000);
//etc...
});
}
private void button1_Click(object sender, EventArgs e)
{
label1.Text = "string1";
System.Threading.Thread.Sleep(3*1000);
label1.Text = "string2";
System.Threading.Thread.Sleep(5*1000);
label1.text="string 3";
}
Thank you sooo much.
Oh i love stakoverflow.com
I did something like this.:)
Thank you all gentle mens :)
Please comment how did i do? I actually wanted it all happen in click of button.
public partial class mainForm : Form {
public mainForm()
Timer myTimer = new Timer();
private void button1_Click(object sender, EventArgs e)
{
myTimer.Tick += new EventHandler(myTimer_Tick);
myTimer.Interval = 2000;
myTimer.Start();
}
int counter=0;
void myTimer_Tick(object sender, EventArgs e)
{
if (counter == 0)
{
label4.Text = "string1";
myTimer.Interval = 2000;
}
if (counter == 1)
{
label4.Text = "string2";
myTimer.Interval = 2000;
}
if (counter == 2)
{
label4.Text = "string3";
myTimer.Stop();
}
else
{
myTimer.Enabled = true;
}
counter++;
} }
I made it all work with valuable examples you all provided . i put it all together and got it working as i wanted.
Once again Thank you all :)