Click here to Skip to main content
15,889,429 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 remove items from listBox1 .
A progressBar1 which shows the progress of this process.

Here is the code.

C#
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 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

When listBox1 reaches at 0, it raise an error, i want to stop the timer when listbox1 gets no items or 0 items or empty.
Posted
Updated 1-Jun-14 4:32am
v3
Comments
[no name] 1-Jun-14 11:22am    
Check the Item count after your RemoveAt.
Sergey Alexandrovich Kryukov 1-Jun-14 13:38pm    
Why using timer at all? What did you want to achieve with that?
—SA
Member 10579673 1-Jun-14 14:09pm    
Timer changes affiliations of listbox items.and server takes 4 seconds to change a affiliation,thats why used a timer with 4 seconds interval.

1 solution

Disable the timer as soon as the timer1_Tick procedure is entered. Re-enable the timer only if there are items left in the ListBox. In your code, if the jabber.Send takes more than four seconds, another timer1_Tick procedure will start running before the previous one has ended. I believe, that is the cause of your error.

Also, do not start the timer until the interval has been set.


C#
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;
    }
}
 
Share this answer
 
v8
Comments
Member 10579673 1-Jun-14 13:09pm    
@Mike Meinz I want to tell you that jabber server takes 4 seconds to update the affiliation list.so it will take 4 seconds to populate items in listBox1. and what timer1 do also takes 4 seconds to change the affiliation of every item in listbox1.
Mike Meinz 1-Jun-14 13:12pm    
That is why your code gets the exception. The timer1_Tick procedure was getting called every four seconds even if the previous timer1_Tick had not finished running. This would cause some listbox items to be sent twice. By using my revised version, timer1_Tick will only be called once per listbox item.

Member 10579673 1-Jun-14 13:33pm    
@Mike Meinz Your Code is working well.But if i call timer1 again after some time.I get this error.

System.ArgumentOutOfRangeException was unhandled
Message=Value of '22' is not valid for 'Value'. 'Value' should be between 'minimum' and 'maximum'.
at line :progressBar1.Value += 1;
LLLLGGGG 1-Jun-14 13:44pm    
Try to replace
Progressbar.maximum=listbox1.items.count
With
Progressbar.maximum=listbox1.items.count+1
Mike Meinz 1-Jun-14 13:55pm    
See revised Solution.

I added progressBar1.Value = 0; in button2_Click

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900