如果列表框为空或项目小于0,如何停止计时器 [英] How to Stop timer if listbox empty or items less than 0 c#

查看:72
本文介绍了如果列表框为空或项目小于0,如何停止计时器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作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.

推荐答案

立即禁用计时器输入timer1_Tick步骤.仅当列表框中有剩余项目时,才重新启用计时器.在您的代码中,如果jabber.Send花费的时间超过四秒钟,则另一个timer1_Tick过程将在上一个过程结束之前开始运行.我相信,这就是导致您出错的原因.
另外,在设置间隔之前,请勿启动计时器.


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.


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,如何停止计时器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆