【问题标题】:C# cloned MouseHover/MouseLeave applies to original control & not currentC# 克隆的 MouseHover/MouseLeave 适用于原始控件而不是当前控件
【发布时间】:2016-05-01 09:54:44
【问题描述】:

我有一个表单,我在面板中添加控件

其中一个是 PictureBox 包含一个 MouseHover/MouseLeave 事件,例如 这个

void PropAction_pBoxMouseLeave(object sender, EventArgs e)
{
    PropAction_pBox.ImageLocation = @"PicButtons\PropertiesBtn2.png";
}
void PropAction_pBoxMouseHover(object sender, EventArgs e)
{
    PropAction_pBox.ImageLocation = @"PicButtons\PropertiesBtn2White.png";
}

在添加按钮中我有这个代码

创建 按钮基于 newPropAction(原始)和添加 它在一个列表中 *

" newPropAction_pBox 代表新的PictureBox & PropAction_pBox 代表原来的PictureBox "*

        PictureBox newPropAction_pBox = new PictureBox();
        newPropAction_pBox.Image = PropAction_pBox.Image;
        newPropAction_pBox.Click += PropAction_pBoxClick;
        newPropAction_pBox.MouseHover += PropAction_pBoxMouseHover;
        newPropAction_pBox.MouseLeave += PropAction_pBoxMouseLeave;
        this.Controls.Add(newPropAction_pBox);// add to controls
        ActionPictures.Add(newPropAction_pBox); //Add to btn to list    

最终效果就是这个(下图)

鼠标还没有在图片框上:http://prnt.sc/axt8b9

鼠标在新的 PictureBox 上:http://prnt.sc/axt9ul

【问题讨论】:

    标签: c# mouseover mouseleave mousehover


    【解决方案1】:

    修改代码如下

    void PropAction_pBoxMouseLeave(object sender, EventArgs e)
    {
        var pictureBox = (PictureBox)sender;
        pictureBox.ImageLocation = @"PicButtons\PropertiesBtn2.png";
    }
    
    void PropAction_pBoxMouseHover(object sender, EventArgs e)
    {
        var pictureBox = (PictureBox)sender;
        pictureBox.ImageLocation = @"PicButtons\PropertiesBtn2White.png";
    }
    

    对象sender 是事件源。参数sender 指的是引发事件的实例。
    因此事件处理程序接收到信息,这个事件的来源是什么类型的对象。

    【讨论】:

    • 哇,它起作用了,但请以原始 PictureBox is PropAction_pBox 的名称向我解释一下 var pictureBox.ImageLocation 如何像 PropAction_pBox.ImageLocation 一样工作
    • @ehem - 查看更新。用英文写太难了。这么容易写代码。
    • 所以(PictureBox)sender返回到object sender,简单来说。无论如何我明白它是如何工作的,祝你有美好的一天
    猜你喜欢
    • 2012-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多