我建了一个网页,里面有200多个BUTTON,他们的ID取名是BUTTON1,BUTTON2……BUTTON199,BUTTON200
用什么简单的方法能使这些BUTTON的事件都可以触发,
不要写成这样子:
private void BUTTON1_Click(object sender, System.EventArgs e)
{
}
……
……
private void BUTTON200_Click(object sender, System.EventArgs e)
{
}
有什么方法吗?
或者能将这么多BUTTON换成数组,再进行触发事件,不过知道怎么搞,请高手指点

解决方案 »

  1.   

    http://community.csdn.net/Expert/topic/4180/4180440.xml?temp=.8962366
      

  2.   

    this.button1.Click += new System.EventHandler(this.button_Click)
    this.button2.Click += new System.EventHandler(this.button_Click)
    this.button3.Click += new System.EventHandler(this.button_Click)
    this.button4.Click += new System.EventHandler(this.button_Click)
    .
    .
    .
    this.button200.Click += new System.EventHandler(this.button_Click)在button_Click里去判断是哪个button触发了事件,再做出相应的处理。
      

  3.   

    初始化的时候
    this.button1.Click += new System.EventHandler(BUTTON1_Click);
    this.button2.Click += new System.EventHandler(BUTTON1_Click);
    this.button3.Click += new System.EventHandler(BUTTON1_Click);
    this.button4.Click += new System.EventHandler(BUTTON1_Click);
    .
    .
    .
      

  4.   

    这样子太烦了
    this.button1.Click += new System.EventHandler(BUTTON1_Click);
    this.button2.Click += new System.EventHandler(BUTTON1_Click);
    this.button3.Click += new System.EventHandler(BUTTON1_Click);
    this.button4.Click += new System.EventHandler(BUTTON1_Click);再好能用数组解决,老兄,有两200个呀如果一定要这样,能不能用循环语句写呢,因为都差不多嘛,
    如果循环语句也不行的话,
    那么怎样:
    在button_Click里去判断是哪个button触发了事件,再做出相应的处理?
      

  5.   

    string sName = "button";
    for (int i = 1;i <= 200;i++)
    {
      Button mybut = this.Controls[1].FindControl(sName+i.ToString());
      mybut.Click += new System.EventHandler(this.button_Click);
    }
    //这样就给你的200个Button加好事件了不过this.button_Click这个事件就得你自己写了
    //其中sName+i.ToString()是Button的Name属性的规律,这一部分可以根据实际情况改一下
      

  6.   

    conan1211(柯楠) 的办法在winform似乎不行
    winform里有没有类似的方法呢?
      

  7.   

    回复人: dahaig(HarryPottor) ( ) 信誉:100  2005-08-02 14:08:00  得分: 0  
     
     
       conan1211(柯楠) 的办法在winform似乎不行
    winform里有没有类似的方法呢?
      
     
    =================
    谁告诉你没有啊
    这不但在WebForm和WinForm就连Java里都有
    这个东东都一样的啊
      

  8.   


    你说的是
    Button mybut = this.Controls[1].FindControl(sName+i.ToString());
    这个方法吗?
    这是当然的了
    不过在WinForm里也有WinForm里的办法啊
      

  9.   

    在WinForm里可以用
    for (int i = 0;i < this.Controls.Count;i++)//通过循环得到界面上的所有控件
    {
    if (this.Controls[i].Name.StartsWith("btn"))//判断当前控件是不是自己想操作的控件,判断的条件可以通过实际情况来做修改
    {
    Button mybtn = (Button )this.Controls[i];
    mybtn.Click += new System.EventHandler(this.button_Click);
    }
    }
      

  10.   

    using System.Windows.Forms;class Test : Form
    {
      Button button1, button2, button3;  Test()
      {
        button1        = new Button();
        button1.Parent = this;
        button1.Text   = "按钮1";    button2        = new Button();
        button2.Parent = this;
        button2.Text   = "按钮2";
        button2.Top    = 30;    button3        = new Button();
        button3.Parent = this;
        button3.Text   = "按钮3";
        button3.Top    = 60;    foreach (Control ctl in Controls)
        {
          if (ctl is Button)
            ctl.Click += new System.EventHandler(BUTTON_Click);
        }
      }  void BUTTON_Click(object sender, System.EventArgs e)
      {
        if ((Button)sender == button1)
        {
          MessageBox.Show("单击了按钮1");
        }
        if ((Button)sender == button2)
        {
          MessageBox.Show("单击了按钮2");
        }
        if ((Button)sender == button3)
        {
          MessageBox.Show("单击了按钮3");
        }
      }  static void Main()
      {
        Application.Run(new Test());
      }
    }
      

  11.   

    using System.Windows.Forms;class Test : Form
    {
      Test()
      {
        const int N = 5;
        Button [] button = new Button [N];
        for (int i = 0; i < N; i++)
        {
          button[i]        = new Button();
          button[i].Parent = this;
          button[i].Top    = 28*i;
          button[i].Text   = "按钮" + (i+1);
          button[i].Tag    = i+1;
          button[i].Click += new System.EventHandler(BUTTON_Click);
        }
      }  void BUTTON_Click(object sender, System.EventArgs e)
      {
        MessageBox.Show("单击了按钮" + ((Button)sender).Tag);
      }  static void Main()
      {
        Application.Run(new Test());
      }
    }
      

  12.   

    using System.Windows.Forms;class Test : Form
    {
      Test()
      {
        int N = 26;  // 总共几个按钮
        int n = 3;   // 每行几个按钮
        Button [] button = new Button [N];
        for (int i = 0; i < N; i++)
        {
          button[i]        = new Button();
          button[i].Parent = this;
          button[i].Top    = 28*(i/n);
          button[i].Left   = 82*(i%n);
          button[i].Text   = "按钮" + (i+1);
          button[i].Tag    = i+1;
          button[i].Click += new System.EventHandler(BUTTON_Click);
        }
      }  void BUTTON_Click(object sender, System.EventArgs e)
      {
        MessageBox.Show("单击了按钮" + ((Button)sender).Tag);
      }  static void Main()
      {
        Application.Run(new Test());
      }
    }
      

  13.   

    for (int i = 0;i < this.Controls.Count;i++)//获取界面上的所有控件
    {
    if (this.Controls[i].Name.IndexOf("btn") != -1)//判断当前控件是不是需要对基操作的控件,这个判断条件可以根据实际情况而定
    {
    Button mybtn = (Button)this.Controls[i];
    mybtn.Click += new System.EventHandler(this.button_Click);
    }
    }