问题描述
我正在制作Windows应用.
首先从服务器获取listBox1中项目的button1.
按钮2启动计时器1.
一个timer1,它从listBox1中删除项目.
progressBar1,显示此过程的进度.
这是代码.
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.
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;
}
}
上面的方法可以很好地工作,直到listBox1中只剩下一项为止.
错误是
未处理System.ArgumentOutOfRangeException消息= InvalidArgument =值"0"对索引"无效.参数名称:index
当listBox1到达0时,它引发一个错误,当listbox1没有任何项目或0个项目或为空时,我想停止计时器.
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.
推荐答案
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;
}
}
这篇关于如果列表框为空或项目小于0,如何停止计时器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!