我用了一个timer1,间隔是三秒,当timer1被激发后,每隔三秒点击一下鼠标的左键或右键或不点击,点左键为赋值为1,点右键赋值为2,不点击赋值为3,然后将每次的值都存到定义好的一个array里边,最后点击button1结束timer1事件并显示数组array的结果。这是我想要的效果,不过我的code有很大的问题,请各位热心的朋友们帮忙修改一下,谢谢了!namespace MouseClick
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            timer1.Interval = 3000;
            timer1.Tick+=new EventHandler(timer1_Tick);
            timer1.Enabled = true;
            this.MouseClick += new MouseEventHandler(this.Form1_MouseClick);
        }        int T;
        int count = 0;
        int[] array = new int[30];        private void timer1_Tick(object sender, EventArgs e)
        {
            if (Left_click == true)
                T = 1;
            if (Right_click == true)
                T = 2;
            if (No_click == true)
                T = 3;
            array[count] = T;
            count = count + 1;
        }        private void button1_Click(object sender, EventArgs e)
        {
            timer1.Dispose();
        }
    }
}

解决方案 »

  1.   

    mouseclcik和timer1_Tick你到底是想实现哪个事件啊??????????????用mouseclick事件来存储点击的数字?
    用timer1_Tick来做什么?
    还是你想每隔三秒模拟鼠标点击
      

  2.   

    你应该用System.Collections.Generic.List<int> array = new List<int>();
            private void timer1_Tick(object sender, EventArgs e)
            {
               //..
                array.Add(T);
            }
      

  3.   

    namespace MouseClick
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
                timer1.Interval = 3000;
                timer1.Tick += new EventHandler(timer1_Tick);
                timer1.Enabled = true;
                this.MouseClick += new MouseEventHandler(this.Form1_MouseClick);
            }        int T = 3;
            ArrayList array = new ArrayList();        private void timer1_Tick(object sender, EventArgs e)
            {           
                array.Add(T);
                T = 3;
            }        private void button1_Click(object sender, EventArgs e)
            {
                timer1.Stop();
                timer1.Dispose();            
            }        private void Form1_MouseClick(object sender, MouseEventArgs e)
            {
                switch (e.Button)
                {
                    case MouseButtons.Left:
                        T = 1;
                        break;
                    case MouseButtons.Right :
                        T = 2;
                        break;
                    default:
                        break;
                }
            }
        }
    }
      

  4.   

    一维数组可以使用Array.Resize()方法,不一定要用ArrayList
      

  5.   


    恩,随便抓的个collection
    感觉比数组好的多其实应该用Collection<T>怕麻烦用的ArrayList
    我一般习惯用这个和HashTable不太喜欢用Collection和Dictionary
      

  6.   


    谢谢,我运行你在5楼的code,结果报错:The type or namespace name 'ArrayList' could not be found (...)。请问这是什么原因呢?
      

  7.   

    汗水,你要引用命名空间using System.Collections;
      

  8.   


    朋友,我用timer1_Tick来记录我每次点击的是鼠标左键还是右键还是没点击鼠标。
      

  9.   


    我是大菜鸟呵呵。这下明白了,另外我想看到结果,也就是array的所有元素,不知道怎么个弄法?用textBox来显示吗?最好能给我代码,谢谢了!
      

  10.   

    foreach(int x in array)
    {
    textBox1.Text = textBox1.Text +  x + "\r\n";
    }
      

  11.   

    直接使用MouseClick 不就完了,没必要使用timer。