I've a project in which i got a listBox1 a timer1 a button1 and a progressBar1.
When i click button1 the timer1 starts.
private void button1_Click(object sender, EventArgs e)
{
timer1.Enabled = true;
timer1.Interval = 500;
progressBar1.Maximum = listBox1.Items.Count;
progressBar1.Value = 0;
}
When timer1 ticks it removes one item from listBox1 and progressBar1 must show the progress of Removal.
private void timer1_Tick(object sender, EventArgs e)
{
timer1.Enabled = false;
if (listBox1.Items.Count > 0)
{
listBox1.Items.RemoveAt(0);
progressBar1.Increment(1);
groupBox1.Text = listBox1.Items.Count.ToString();
}
if (listBox1.Items.Count > 0)
{
timer1.Enabled = true;
progressBar1.Maximum = listBox1.Items.Count;
progressBar1.Value = 0;
}
}
But i think the above code got some bug with progressBar1 as it dont show progress while removal of items and it is full when the listBox1 items = 0.
There is a better way than this, So set the step property like this in click event :
this.progressBar1.Minimum = 0;
this.progressBar1.Value = 0;
this.progressBar1.Maximum = this.listBox1.Items.Count;
progressBar1.Step = 1;
Now you can do the following:
void timer1_Tick(object sender, EventArgs e)
{
if (listBox1.Items.Count > 0)
{
listBox1.Items.RemoveAt(0);
progressBar1.PerformStep();
groupBox1.Text = listBox1.Items.Count.ToString();
}
else
{
this.timer1.Enabled = false;
}
}
Complete Code:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
PopulateList();
}
private void PopulateList()
{
for (int i = 0; i < 10; i++)
{
listBox1.Items.Add(i);
}
}
private void timer1_Tick(object sender, EventArgs e)
{
if (listBox1.Items.Count > 0)
{
listBox1.Items.RemoveAt(0);
progressBar1.PerformStep();
groupBox1.Text = listBox1.Items.Count.ToString();
}
else
{
timer1.Enabled = false;
}
}
private void button1_Click(object sender, EventArgs e)
{
timer1.Tick += timer1_Tick;
timer1.Enabled = true;
timer1.Interval = 500;
progressBar1.Enabled = true;
progressBar1.Visible = true;
progressBar1.Minimum = 0;
progressBar1.Value = 0;
progressBar1.Maximum = listBox1.Items.Count;
progressBar1.Step = 1;
}
}
You are setting progressbar value to 0 after incrementing it...
Try this:
private void timer1_Tick(object sender, EventArgs e)
{
timer1.Enabled = false;
if (listBox1.Items.Count > 0)
{
listBox1.Items.RemoveAt(0);
progressBar1.Increment(1);
groupBox1.Text = listBox1.Items.Count.ToString();
timer1.Enabled = true;
}
}
Related
The problem is the label gets displayed before the progress-bar is completed. How do I make the label display only after the progress-bar is fully complete?
I tried changing the max values to a higher number but it didn't work.
public partial class dload : Form
{
public dload()
{
InitializeComponent();
}
private void dload_Load(object sender, EventArgs e)
{
label1.Visible = false;
}
private void button1_Click(object sender, EventArgs e)
{
int i = 0;
progressBar1.Minimum = 0;
progressBar1.Maximum = 5000;
for (i = 0; i <= 5000; i++)
{
progressBar1.Value = i;
if (i == 5000)
{
label1.Visible = true;
}
}
}
}
Actually, Your code run very vast, it is about less than a second to set value from 0% to 100% ! But
ProgressBar has two styles for displaying the current status (Classic and Continues).
In Continues mode if the progress value went to 100% from 0% the control will show an animation, which is not display the real and accurate progress. You can set a delay via Thread.Sleep() and show your label immediately after the for loop to find out to what i happening !
The Below code will work:
private void button1_Click(object sender, EventArgs e)
{
int i = 0;
progressBar1.Minimum = 0;
progressBar1.Maximum = 5000;
for (i = 0; i <= 5000; i++)
{
Thread.Sleep(1);
progressBar1.Value = i;
}
label1.Visible = true;
}
This is an animation problem. A "hack" around it is to actually decrease the progress value by 1:
progressBar1.Minimum = 0;
progressBar1.Maximum = 5000;
for (int i = 0; i < progressBar1.Maximum; i++) {
progressBar1.Value = i;
progressBar1.Value = Math.Max(i - 1, progressBar1.Minimum);
}
label1.Visible = true;
use progressBar1.Refresh:
public partial class dload : Form
{
public dload()
{
InitializeComponent();
}
private void dload_Load(object sender, EventArgs e)
{
label1.Visible = false;
}
private void button1_Click(object sender, EventArgs e)
{
int i = 0;
progressBar1.Minimum = 0;
progressBar1.Maximum = 5000;
for (i = 0; i <= 5000; i++)
{
progressBar1.Value = i;
progressBar1.Refresh();
if (i == 5000)
{
label1.Visible = true;
}
}
}
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();
}
}
I am making a windows app.
A button1 which gets the items in listBox1 from server at the start.
A button2 which starts the timer1.
A timer1 which removes items from listBox1 .
A progressBar1 which shows the progress of this process.
Here is the code:
private void button1_Click(object sender, EventArgs e)
{
jabber.Send("<iq type='get' to='" + textBox1.Text + "#conference.jabber.com' id='qip_1026'><query xmlns='http://jabber.org/protocol/muc#admin'><item affiliation='outcast' /></query></iq>");
}
private void button2_Click(object sender, EventArgs e)
{
progressBar1.Maximum = listBox1.Items.Count;
timer1.Start();
timer1.Interval = 4000;
}
private void timer1_Tick(object sender, EventArgs e)
{
if (listBox1.Items.Count > 0)
{
jabber.Send("<iq type='set' to='" + textBox7.Text + "#conference.jabber.com'><query xmlns='http://jabber.org/protocol/muc#admin'><item jid='" + listBox1.Items[0].ToString() + "' affiliation='none'/></query></iq>");
listBox1.Items.RemoveAt(0);
progressBar1.Value += 1;
label.Text = listBox1.Items.Count.ToString();
}
else
{
timer1.Enabled = False;
}
}
The above code works well till there is one item left in listBox1.
The error is:
System.ArgumentOutOfRangeException was unhandled
Message=InvalidArgument=Value of '0' is not valid for 'index'.
Parameter name: index
It raises an error when listBox1 reaches 0. I want to stop the timer when listbox1 is empty or gets no items or 0 items.
The problem is in this code:
private void timer1_Tick(object sender, EventArgs e)
{
if (listBox1.Items.Count > 0)
{
jabber.Send("<iq type='set' to='" + textBox7.Text + "#conference.jabber.com'><query xmlns='http://jabber.org/protocol/muc#admin'><item jid='" + listBox1.Items[0].ToString() + "' affiliation='none'/></query></iq>");
listBox1.Items.RemoveAt(0);
progressBar1.Value += 1;
label.Text = listBox1.Items.Count.ToString();
}
else
{
timer1.Enabled = False;
}
}
So what is happening is that you are using count to check >0 then calling jabber to do the work, It the call becomes slow- you will see multiple timers getting fired back. So a big queue will be collected there. You need to modify the code a bit here using lock to hold the list and allow jabber to do its work:
private void timer1_Tick(object sender, EventArgs e)
{
lock (listBox1)
{
if (listBox1.Items.Count > 0)
{
jabber.Send("<iq type='set' to='" + textBox7.Text +
"#conference.jabber.com'><query xmlns='http://jabber.org/protocol/muc#admin'><item jid='" +
listBox1.Items[0].ToString() + "' affiliation='none'/></query></iq>");
listBox1.Items.RemoveAt(0);
progressBar1.Value += 1;
label.Text = listBox1.Items.Count.ToString();
}
else
{
timer1.Enabled = False;
}
}
}
Lock will also ensure that the items are removed correctly.
To save the file as per comment below :
public class ChatHistoryManager
{
private readonly RichTextBox richTextBox;
private Timer timer = new Timer();
public ChatHistoryManager(RichTextBox richTextBox)
{
this.richTextBox = richTextBox;
this.InitializeTimer();
}
public string Location { get; set; }
private void InitializeTimer()
{
this.timer.Tick += timer_Tick;
this.timer.Enabled = true;
this.timer.Interval = (int) TimeSpan.FromHours(1).TotalMilliseconds;
}
void timer_Tick(object sender, EventArgs e)
{
this.SaveFile();
}
public void SaveFile()
{
//Save the file to the location
this.richTextBox.SaveFile(this.Location,RichTextBoxStreamType.RichText);
}
public void Stop()
{
this.timer.Stop();
}
}
Now we need to set in form like this:
private void Form1_Load(object sender, EventArgs e)
{
ChatHistoryManager chatHistoryManager = new ChatHistoryManager(this.richTextBox1);
chatHistoryManager.Location = #"C:\Development\test.txt";
}
try this
int count = City.Items.Count - 1;
for (int i = count; i > 0; i--){
City.Items.RemoveAt(i);
}
Here is what worked for me.
private void button1_Click(object sender, EventArgs e)
{
jabber.Send("<iq type="get" to="" + textBox1.Text + "#conference.jabber.com" id="qip_1026"><query xmlns="http://jabber.org/protocol/muc#admin"><item affiliation="outcast" /></query></iq>");
}
private void button2_Click(object sender, EventArgs e)
{
progressBar1.Maximum = listBox1.Items.Count;
progressBar1.Value = 0;
// Set the timer interval to four seconds
timer1.Interval = 4000;
// Start the timer
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
// Disable the timer while we are working in this procedure so
// it doesn't tick while we are working in this procedure
timer1.Enabled = False;
// Send only if there are items in the ListBox
if (listBox1.Items.Count > 0)
{
jabber.Send("<iq type="set" to="" + textBox7.Text + "#conference.jabber.com"><query xmlns="http://jabber.org/protocol/muc#admin"><item jid="" + listBox1.Items[0].ToString() + "" affiliation="none" /></query></iq>");
listBox1.Items.RemoveAt(0);
progressBar1.Value += 1;
label.Text = listBox1.Items.Count.ToString();
}
// Re-enable only if there are items left in the ListBox
if (listBox1.Items.Count > 0)
{
timer1.Enabled = True;
}
}
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";
}
}