如何根据ListBox控件的值不同触发事件
比如说每增加15行就触发执行事件!!

解决方案 »

  1.   

    怎么样监视它已经增加了15行了呢?
    C#的API没有提供此类事件!
      

  2.   

    又用Timer除此之外还有其他办法没?因为我的程序目前已经用了4到5个Timer了!
      

  3.   

    在ListBox的事件中加判断,if( listBox1.Items.Count>15){你要做的操作}else{}
      

  4.   

    也不行,因为我有很多地方调用了ListBox.Items.Add(),
    每个调用的地方都有可能是结尾!
    故此方法行不通,因为如果这样的话,我很多地方都要添加这一句if( listBox1.Items.Count>15){你要做的操作}else{}
      

  5.   

    看来如果实在不行只能用Timer了!
    目前还没有想到比这个更好的办法。
      

  6.   

    在ListBox的事件中加判断:1.加个变量记录a=0,每增加一个item则a=a+1。接着判断if( a>15){你的操作;a=0}else{}
      

  7.   

    用Timer的话只需要在自带的这个Tick()函数下面
    每执行一次Tick就统计一次就可以了!_Tick(object sender, EventArgs e)
    {
        listBox1.Items.Count>15
    }
      

  8.   

    重写Add()方法,然后再Add里面做判断,触发一个事件!
      

  9.   

    在ListBox的事件中加判断????
    什么事件呢?
    现在不是数的问题,而是我要什么时候去判断这个数是否已经满足15。
    这位仁兄还是没明白我的问题,
    之所以用Timer是因为这样的话系统就会在固定的时间内去帮我判断是否已经满足15
    ListBox的所有事件中,并没有提供什么时候去判断a的数目的实践,例如
    有些控件CheckBox有一个事件叫做CheckedChanged()每次checkBox的状态改变,它就会自动触发执行这个事件。
    同理我也需要ListBox数量变化的时候有提供一个事件可以自动触发。但是可惜没有!
      

  10.   

    listBox中有CollectionChanged事件,这事件的具体用法我也没用过,你可以试下。
      

  11.   

    没办法重写了!我试过了
    所有控件都是继承Control类
    但是可惜没有找到基类的Add方法,没有办法重写咯!
      

  12.   

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;namespace WindowsApplication
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }        private void button1_Click(object sender, EventArgs e)
            {
                myListBox1.Items.Add("1");
                myListBox1.Items.Insert(0,"1");            myListBox1.Items.RemoveAt(0);
            }        private void myListBox1_CollectionChanged(object sender, EventArgs e)
            {
                MessageBox.Show(myListBox1.Items.Count.ToString());
            }  
        }    public class MyListBox : ListBox
        {
            private const int LB_ADDSTRING = 0x180;
            private const int LB_INSERTSTRING = 0x181;
            private const int LB_DELETESTRING = 0x182;        public event EventHandler CollectionChanged;        protected override void WndProc(ref Message m)
            {
                switch (m.Msg)
                {
                    case LB_ADDSTRING:
                    case LB_INSERTSTRING:
                    case LB_DELETESTRING:
                        if (CollectionChanged != null)
                        {
                            EventArgs e = new EventArgs();
                            CollectionChanged(this,e);
                        }
                        break;            }            base.WndProc(ref m);
            }
        }
    }