代码之家  ›  专栏  ›  技术社区  ›  Developer

如何处理按钮点击事件下的图像点击

  •  0
  • Developer  · 技术社区  · 14 年前

    2 回复  |  直到 14 年前
        1
  •  2
  •   Timwi    14 年前

    你可以通过比较 sender 参数:

    void MyButton_Click(object sender, EventArgs e) 
    { 
        if (sender == MyButton1)
        {
            // 1st image button was clicked — some values
        }
        else if (sender == MyButton2)
        {
            // 2nd one was clicked — show another
        }
    }
    
        2
  •  0
  •   Blam    14 年前

    你不能创建两个事件和一个函数吗? 前任:

    //Hook both OnClick events to these!    
    private void OnButton1Click(object sender, EventArgs e) { BeenClicked(button1); }
    private void OnButton2Click(object sender, EventArgs e) { BeenClicked(button2); }
    
    private void BeenClicked(Button ClickedButton) 
    {
        if(ClickedButton.Text == Button1) Console.WriteLine("Hi to you too!"); 
    }
    

    或者您可以使用:

    //Hook both OnClick events to this!
    private void OnButtonClick(object sender, EventArgs e) 
    { 
        ClickedButton = (Button)sender; 
        if(ClickedButton.Text == Button1) Console.WriteLine("Hi to you too!"); 
    }
    

    如果我没听错的话:)