简体   繁体   English

(Winform)在触发MouseHover事件后将其添加后,无法单击链接标签

[英](Winform) Cannot click the linklabel after adding it as MouseHover event fired

I have a PictureBox. 我有一个PictureBox。 I want to add automatically a LinkLabel at a specific location when the mouse hover to it. 当鼠标悬停在特定位置时,我想自动在其上添加LinkLabel。 Everything's fine but I can't click on the LinkLabel as it can't stop flickering. 一切都很好,但我无法单击LinkLabel,因为它无法停止闪烁。 This is my code: 这是我的代码:

private void ptbType1_MouseHover(object sender, EventArgs e)
    {            
        PictureBox ptb = sender as PictureBox;
        LinkLabel lkl = new LinkLabel();
        lkl.Text = "Change Image...";
        lkl.Font = new Font(lkl.Font.FontFamily, 10, FontStyle.Regular);
        lkl.BackColor = SystemColors.Window;
        lkl.AutoSize = false; lkl.TextAlign = ContentAlignment.MiddleCenter;
        lkl.Size = new Size(120, 30); lkl.BorderStyle = BorderStyle.FixedSingle;
        lkl.Location = new Point(ptb.Size.Width - 120, 5);
        ptb.Controls.Add(lkl);            
    }

**UPDATE 10/18/2016: The idea using Tooltip to avoid LOTS OF linklabel by gzaxx suggests me to implement another way: A linklabel now has already been on the picture box, its Visible property was set False. ** UPDATE 10/18/2016:使用Tooltip避免gzaxx的LOTS OF linklabel的想法建议我实现另一种方法:图片框上已经有一个linklabel,其Visible属性设置为False。 When mouse hovers the picturebox, the label appears as Visible -> true, vice versa when mouse leaves. 当鼠标悬停在图片框上时,标签显示为“可见-> true”,反之亦然。 All remain the same: flickering makes it cannot be clicked. 所有内容均保持不变:闪烁使得无法单击它。 The MouseHover Event on the picture box, certainly, is the cause. 原因是图片框上的MouseHover事件。

Any ideas? 有任何想法吗? thanks for any help! 谢谢你的帮助!

Your code have some issues. 您的代码有一些问题。 First you create A LOT of labels, each time mouse is moved by millimeter, new label is created. 首先,创建很多标签,每次鼠标移动毫米时,都会创建一个新标签。 Second you do not attach event to link label so clicking on it does nothing. 其次,您不将事件附加到链接标签,因此单击它不会执行任何操作。 My advice would be to use Tooltip to show message when hovering over PictureBox with information "Click to change image..." and handle click event. 我的建议是,将鼠标悬停在PictureBox并显示信息“单击以更改图像...”并处理单击事件时,使用Tooltip显示消息。

// should be called only once
private void AttachClickEvent(PictureBox ptb)
{
    ptb.MouseClick += (s, o) =>
    {
        // open change dialog here
    }
}

It's a straightforward solution. 这是一个简单的解决方案。

Thanks for all your support, I've solved the problem. 感谢您的支持,我已经解决了问题。 The key is 关键是

  1. When the mouse cursor enter the linklabel, it coincidentally fire the MouseLeave_Event of the picturebox => linklabel disappears 当鼠标光标进入linklabel时,它会同时触发picturebox的MouseLeave_Event => linklabel消失

  2. As label vanished, the mouse cursor then enter the picturebox, so MouseHover_Event of the picturebox work => linklabel appears 标签消失后,鼠标光标进入图片框,因此图片框的MouseHover_Event => linklabel出现

---> eternal loop -> blinking --->永恒循环->闪烁

I 've dealt with it by this code to check whether the mouse cursor is in linklabel bound area, the linklabel stands still and only disappears when cursor actually get out of the picturebox: 我已经通过以下代码处理了它,以检查鼠标光标是否在linklabel的绑定区域中,linklabel静止不动,并且仅在光标实际离开图片框时才会消失:

    private void ptbType1_MouseLeave(object sender, EventArgs e)
    {
        PictureBox ptb = sender as PictureBox;
        LinkLabel lkl = ptb.Controls[0] as LinkLabel;
        if (!lkl.Bounds.Contains(ptb.PointToClient(Cursor.Position)))
        {
           lkl.Visible = false;
        }
    }

Done! 做完了! My 1st question on the site and i myself answer it, brilliant :)) 我在网站上的第一个问题,我自己回答,非常棒:))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM