I want to run a progress bar on a form through the use of a timer.
I have tried multiple ways and have not been able to get it to work.
I hope someone here can help me with this.
private void SplashScreen_Load(object sender, EventArgs e)
{
splashScreenTimer.Enabled = true;
splashScreenTimer.Start();
splashScreenTimer.Interval = 1000;
progressBar.Maximum = 100;
splashScreenTimer.Tick += new EventHandler(timer1_Tick);
}
private void timer_Tick(object sender, EventArgs e)
{
if (progressBar.Value != 10)
{
progressBar.Value++;
}
else
{
splashScreenTimer.Stop();
}
}
you are assigning event_handler like
splashScreenTimer.Tick += new EventHandler(timer1_Tick);
and you are changing the progressBar value in
private void timer_Tick(object sender, EventArgs e)
{
if (progressBar.Value != 10)
{
progressBar.Value++;
}
else
{
splashScreenTimer.Stop();
}
}
change event handler to
splashScreenTimer.Tick += new EventHandler(timer_Tick);
or move codes to the other event handler timer1_Tick which should be in your form
For running the progressBar full in 4 seconds you can do like this
private void Form1_Load(object sender, EventArgs e)
{
splashScreenTimer.Enabled = true;
splashScreenTimer.Start();
splashScreenTimer.Interval = 30;
progressBar.Maximum = 100;
splashScreenTimer.Tick += new EventHandler(timer_Tick);
}
int waitingTime = 0;
private void timer_Tick(object sender, EventArgs e)
{
if (progressBar.Value < 100)
{
progressBar.Value++;
}
else
{
if (waitingTime++ > 35)
this.Close();
}
}
Related
private void OnTimer(object sender, EventArgs e)
{
int i = 0;
i += 1;
}
I tried to make the timer
private void Form1_Load(object sender, EventArgs e)
{
int i = 0;
FontFamily[] fontlar = FontFamily.Families;
fontlar.GetUpperBound(i);
Timer timer1 = new Timer
{
Interval = 750
};
timer1.Enabled = true;
timer1.Tick += new System.EventHandler(OnTimer);
}
I put the code in form1 but I tried it in textbox as well it didn't work
private void button1_Click(object sender, EventArgs e)
{
timer1.Enabled = true;
}
button
I guess you are new to this.
Your Timer does nothing
Your Timer is local, it should be public
You didn't create your Textbox
I think this would be your answer:
Timer timer1;
int i = 0;
private void Form1_Load(object sender, EventArgs e)
{
timer1 = new Timer
{
Interval = 750
};
timer1.Enabled = true;
timer1.Tick += new System.EventHandler(OnTimer);
}
private void OnTimer(object sender, EventArgs e)
{
i += 1;
FontFamily[] fontlar = FontFamily.Families;
//fontlar.GetUpperBound(i);
textBox1.Font = new Font(fontlar[i], 16.0f);
}
private void button1_Click(object sender, EventArgs e)
{
timer1.Enabled = true;
}
Don't forget to create a textbox on your form.
hi guys i tried to copy some files with this Code everything is good and the app will copy files but in copy progress i cant move my app or do anything
i tried to use thread but its not works i also use backgroundWorker but still nothing the only control that doesnt get stuck is progressBar its works fine here is my code :
public Form1()
{
InitializeComponent();
backgroundWorker1.Dispose();
backgroundWorker1.DoWork += BackgroundWorker_DoWork;
backgroundWorker1.RunWorkerCompleted += BackgroundWorker_RunWorkerCompleted;
backgroundWorker1.ProgressChanged += BackgroundWorker_ProgressChanged;
backgroundWorker1.WorkerReportsProgress = true;
backgroundWorker2.DoWork += BackgroundWorker2_DoWork;
backgroundWorker2.WorkerReportsProgress = true;
}
private void BackgroundWorker2_DoWork(object sender, DoWorkEventArgs e)
{
File.Copy(sourcePath, targetPath);
}
private void BackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
for (int i = 0; i < fileSize; i++)
{
int p = (i + 1) * 100 / Convert.ToInt32(fileSize);
backgroundWorker1.ReportProgress(p);
}
}
private void BackgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
}
private void BackgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
lbProgress.Text = e.ProgressPercentage.ToString();
progressBar1.Value = e.ProgressPercentage;
}
private void btnTarget_Click(object sender, EventArgs e)
{
folderBrowser = new FolderBrowserDialog();
if (folderBrowser.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
targetPath += folderBrowser.SelectedPath + #"\" + fileName;
lbTarget.Text = targetPath;
}
}
private void btnSource_Click(object sender, EventArgs e)
{
op = new OpenFileDialog();
if (op.ShowDialog() == DialogResult.OK)
{
sourcePath += op.FileName;
lbSource.Text = sourcePath;
fileInfo = new FileInfo(sourcePath);
fileSize = fileInfo.Length / 1024;
fileName = fileInfo.Name;
MessageBox.Show(string.Format("File size is: {0} KB", fileSize));
}
}
private void btnCopy_Click(object sender, EventArgs e)
{
backgroundWorker1.RunWorkerAsync();
backgroundWorker2.RunWorkerAsync();
}
You're updating the progress bar faster than the UI can update, for every single byte of the file being copied in a tight loop. You're flooding the UI thread with pointless work.
Remove backgroundWorker1, it's not doing anything useful anyway. If you don't have a way to track the progress (which you don't with File.Copy), just use a progress bar without progress (set Style to Marquee).
For testing I created a simple winform application with a button, a label and a background worker and added the following corresponding events:
private void OnBackgroundWorkerDoWork(object sender, DoWorkEventArgs e)
{
var worker = (BackgroundWorker)sender;
for (int i = 0; i < 10; i++)
{
Thread.Sleep(500);
worker.ReportProgress(i * 10);
}
}
private void OnBackgroundWorkerProgressChanged(object sender, ProgressChangedEventArgs e)
{
labelProgress.Text = e.ProgressPercentage.ToString();
}
private void OnBackgroundWorkerRunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
labelProgress.Text = "Done";
}
private void OnButtonProgressClick(object sender, System.EventArgs e)
{
backgroundWorker.RunWorkerAsync();
}
Works as expected.
Could You try to update Your DoWork to this:
private void BackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
int mainProgress = 0;
for (int i = 0; i < fileSize; i++)
{
int calculatedProgress = (i + 1) * 100 / Convert.ToInt32(fileSize);
if(calculatedProgress > mainProgress)
{
mainProgress = calculatedProgress;
backgroundWorker1.ReportProgress(mainProgress);
}
}
}
maybe You are doing so many updates that simply Window Thread is all the time updating only progress and don't have a time to make anything else?
I have 5 Forms, 1 main form, and 4 forms I want them to switch between each other Automatically every couple of seconds (take turns, each form x seconds and switch to the next).
I have 2 forms so far switching between each other every 2 seconds.
void mytimer_Tick(object sender, EventArgs e)
{
if (!frm2.Focused)
frm2.Focus();
else
frm3.Focus();
}
private void Form1_Load_1(object sender, EventArgs e)
{
Timer mytimer = new Timer();
mytimer.Tick += mytimer_Tick;
mytimer.Interval = 2000;
mytimer.Start();
}
Thankyou.
Crude format. But you will get the idea.
private void HideAllForms()
{
frm1.Hide();
frm2.Hide();
frm3.Hide();
frm4.Hide();
}
void mytimer_Tick(object sender, EventArgs e)
{
if (frmSrl == 1)
{
frmSrl++;
HideAllForms();
frm1.Show();
}
else if (frmSrl == 2)
{
frmSrl++;
HideAllForms();
frm2.Show();
}
else if (frmSrl == 3)
{
frmSrl++;
HideAllForms();
frm3.Show();
}
else if (frmSrl == 4)
{
frmSrl =1;
HideAllForms();
frm4.Show();
}
else
frmSrl = 1;
}
int frmSrl = 1;
private void Form1_Load_1(object sender, EventArgs e)
{
Timer mytimer = new Timer();
mytimer.Tick += mytimer_Tick;
mytimer.Interval = 2000;
mytimer.Start();
}
I am working on a simple mediaplayer application. It works great but I want to add some extra features. I have added a trackbar control.How can i set trackbar length the same as the music's length ?
Like if the song is halfways the trackbars halfways.This is what I have so far
string[] files, indexed_files;
private void button3_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Multiselect = true;
if (ofd.ShowDialog() == DialogResult.OK) {
files = ofd.SafeFileNames;
indexed_files = ofd.FileNames;
for (int i = 0; i < files.Length; i++)
{
listBox1.Items.Add(files[i]);
}
}
button4.Enabled = true;
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
axWindowsMediaPlayer1.URL = indexed_files[listBox1.SelectedIndex];
progressBar1.Maximum =(int) axWindowsMediaPlayer1.currentMedia.duration;
axWindowsMediaPlayer1.PlayStateChange += axWindowsMediaPlayer1_PlayStateChange;
}
void axWindowsMediaPlayer1_PlayStateChange(object sender, AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e)
{
trackBar1.Value = (int)axWindowsMediaPlayer1.Ctlcontrols.currentPosition;
}
int index = 0;
private void button4_Click(object sender, EventArgs e)
{
if (listBox1.Items.Count != 0) {
axWindowsMediaPlayer1.URL = indexed_files[index];
trackBar1.Maximum = (int)axWindowsMediaPlayer1.currentMedia.duration;
index++;
index = (index % listBox1.Items.Count);
}
}
This will bring you the desired outcome.In my example i just placed the url in the form load for demonstration purposes.The openstatechanged event its to set the trackbar maximum since you need to wait for the player to load the file,after that the code its pretty self-explanatory:
public partial class Form1 : Form
{
Timer t;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
axWindowsMediaPlayer1.URL = "YourUrlHere";
t = new Timer();
t.Interval = 1000;
t.Tick += new EventHandler(t_Tick);
}
void t_Tick(object sender, EventArgs e)
{
trackBar1.Value = (int)this.axWindowsMediaPlayer1.Ctlcontrols.currentPosition;
}
private void axWindowsMediaPlayer1_OpenStateChange(object sender, AxWMPLib._WMPOCXEvents_OpenStateChangeEvent e)
{
if (axWindowsMediaPlayer1.openState == WMPLib.WMPOpenState.wmposMediaOpen)
{
trackBar1.Maximum = (int)axWindowsMediaPlayer1.currentMedia.duration;
t.Start();
}
}
}
Yes its a timer:),and probably it is best to set it bellow 1000 for reasons of delay.
So you should now add a timer and insert the following code in timer Tick event handler:
trackbar.Value = this.axWindowsMediaPlayer1.ctlControls.CurrentPosition;
Can you help? In C#, after clicking on button1, checkBoxWMVFile (the time interval)
should be switched on and off.
private void button1_Click(object sender, EventArgs e)
{
if (timercheckbox.Enabled == true)
{
timercheckbox = new Timer();
timercheckbox.Start();
timercheckbox.Interval = 10000; // 10 second
if(timercheckbox.Enabled)
{
timercheckbox.Start();
checkBoxWMVFile.Checked = true;
}
else
{
timercheckbox.Stop();
checkBoxWMVFile.Checked = false;
}
}
}
As I understood, you need something like this
private Timer _timer = new Timer();
public Form1()
{
InitializeComponent();
_timer.Interval = 10000;
_timer.Tick += new EventHandler(_timer_Tick);
}
private void button1_Click(object sender, EventArgs e)
{
if (checkBox1.Checked)
{
checkBox1.Checked = false;
if(_timer.Enabled)
_timer.Stop();
}
else
{
checkBox1.Checked = true;
if (!_timer.Enabled)
_timer.Start();
}
}
void _timer_Tick(object sender, EventArgs e)
{
//do something here
throw new NotImplementedException();
}
In order for your code to work, you have to reverse the logic i.e.
private void button1_Click(object sender, EventArgs e)
{
if(checkBoxWMVFile.Checked == false)//if the textbox is not checked
{
if (timercheckbox == null)//If this is the first time
{
timercheckbox = new Timer();//Create a timer
timercheckbox.Interval = 10000; // Set the interval, before starting it.
timercheckbox.Start();//Start it
}
else timercheckbox.Start();//If it is not the first time, just start it
checkBoxWMVFile.Checked = true;//Check the checkbox
}
else//the checkbox is checked
{
timercheckbox.Stop();//Stop the timer
checkBoxWMVFile.Checked = false;//Uncheck the checkbox
}
}
This code works correctly:
// InitializeComponent
this.timercheckbox.Interval = 5000;
this.timercheckbox.Tick += new System.EventHandler(this.timercheckbox_Tick);
private void timercheckbox_Tick(object sender, EventArgs e)
{
checkBoxWMVFile.Checked = false;
checkBoxWMVFile.Checked = true;
}
private void button1_Click(object sender, EventArgs e)
{
if( timercheckbox.Enabled == true )
{
timercheckbox.Enabled = false;
button1.Text = "Start Auto save";
}
else
{
timercheckbox.Interval = (int)numericChnageTime.Value;
timercheckbox.Enabled = true;
checkBoxWMVFile.Checked = true;
button1.Text = "Stop Auto save";
}
}