如何扩展MouseEventHandler函数

时间:2016-01-10 00:15:44

标签: c# winforms mouseevent eventhandler extending

首先,我想为我缺乏知识而道歉,但我是C#的新手,我缺少一些基本原则。

以下是我目前的情况: 在我的场景中,我创建了像pictureboxes的棋桌那样的东西。它们被置于控制之内" Panel"在 Winform 表单中 - 面板可滚动! 每个图片框都有唯一的名称,如构造函数中生成的pbR1_C1

R - 代表桌面上的行

C - 代表桌上的专栏

所有这些都是在运行时进行的,因为在程序启动后加载了国际象棋桌面大小。使用过的对象如下所示:

/* Simple preview of object with public variables - just for preview  */

public class ptFcElement                    
{
    public string stName;                   /* "pbR1_C1", "pbR1_C2" */
    public int iRow;                        /* 1                    */
    public int iColumn;                     /* 4                    */
    public PictureBox pbPictureBox;         /* using System.Drawing;*/                                          

    public ptFcElement()                                                               
    {
        stName = sGenerateName();
    }

}

然后我为每个图片框分配事件处理程序

ptFcElementTemp.pbPictureBox.MouseClick += new MouseEventHandler(MouseButton_Click);

此时我能够确定我已按下了一些图片框,但我不知道哪一个。

以下是问题: 由于面板是可滚动的 - 我不能简单地识别按下的图片框 - 它总是调用相同的功能。

void MouseButton_Click(object sender, MouseEventArgs e)
{
    //Do some stuff....
    //In case panel is not scrollable, 
    //I can identify pressed picture box by coordinates of mouse click.  
    //But if panel is scrollable, I am screwed.
} 

理想的想法:

是否有可能扩展MouseEventHandler事件功能?我知道简单的类很容易扩展,但我不知道如何使用事件函数。

ptFcElementTemp.pbPictureBox.MouseClick += 
        new MouseEventHandler(MouseButton_Click, "pbR1_C1");

void MouseButton_Click(object sender, MouseEventArgs e, string sUniqueName)
{
    //Here I am able to identify pressed bisturebox by sUniqueName
     if (sUniqueName == "pbR1_C1")
     {
         //do something
     }

     if (sUniqueName == "pbR2_C3")
     {
         //do something different
     }
} 

谢谢,见到你。 并请,尽可能简单地解释它,对于虚拟。 : - )

3 个答案:

答案 0 :(得分:2)

你可以这样试试:

void MouseButton_Click(object sender, MouseEventArgs e)
{
    var pb = sender as PictureBox;
    if(pb!=null){
        //Do something with the instance the PictureBox which fired the event
    }
} 

在与Yacoub Massad讨论后,我想建议您进行以下改进:

声明您自己的事件,在单击PictureBox时触发该事件。

public class ptFcElement {
    public string stName;                   /* "pbR1_C1", "pbR1_C2" */
    public int iRow;                        /* 1                    */
    public int iColumn;                     /* 4                    */
    public PictureBox pbPictureBox;         /* using System.Drawing;*/

    public event EventHandler PictureBoxWasClicked;
    protected virtual void OnPictureBoxWasClicked(){
        if (this.PictureBoxWasClicked != null)
            this.PictureBoxWasClicked(this, new EventArgs());
    }

    public ptFcElement() {
        stName = sGenerateName();
        this.pbPictureBox.Click += pbPictureBox_Click;
    }

    private void pbPictureBox_Click(object sender, EventArgs e) {
        this.OnPictureBoxWasClicked();
    }

}

外面你可以对这个事件做出反应而不是PictureBox'点击事件。 "发件人"将直接成为您自己班级的实例。

如果你想要你甚至可以定义一个自己的EventArgs类来传递你喜欢的参数...

答案 1 :(得分:1)

作为您问题的直接答案,您可以执行以下操作:

ptFcElementTemp.pbPictureBox.MouseClick +=
    (object sndr, MouseEventArgs m_args)
        => MouseButton_Click(sndr, m_args, "pbR1_C1");

您可以传递任何您想要的参数。

如果您想要更多的上下文而不仅仅是点击的PictureBox,这将非常有用,因为该对象可以使用sender参数进行访问。例如,您可能希望访问相应的ptFcElement

答案 2 :(得分:0)

基本上你是在试图确定哪个控件被点击了?我希望这是你要求的。 sender参数包含您的控件。 例如:

if (sender==ptFcElementTemp.pbPictureBox){
//Do stuff
}
else if(sender==ptFcElementTemp.otherControl){
//Do other stuff
}