简体   繁体   English

按Enter键以单击LinkLabel

[英]Press enter to click a LinkLabel

If I have a linklabel on a .net winform, is there anyway I can, when the link label is focused , get a press of the enter key to cause said LinkLabel to click? 如果我在.net winform上有一个linklabel,无论如何我可以, 当链接标签被聚焦时 ,按下enter键以使所述LinkLabel点击?

Unfortunately it doesn't seem to expose a KeyDown event. 不幸的是,它似乎没有公开KeyDown事件。


EDIT 编辑

The simplest solution is to use PreviewKeyDown , if anyone happens to Google here. 最简单的解决方案是使用PreviewKeyDown ,如果Google在这里遇到任何人。

You could create your own link label class which extends the LinkLabel and overrides the OnKeyUp or OnKeyDown event to capture the ENTER keypress. 您可以创建自己的链接标签类,该类扩展LinkLabel并覆盖OnKeyUp或OnKeyDown事件以捕获ENTER按键。

That would save you reproducing the code for every link label you add to your form. 这样可以节省您为表单添加的每个链接标签重现的代码。

eg 例如

public class LinkLabelEx : LinkLabel
{

    protected override void OnKeyUp(KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Enter)
        {
            e.SuppressKeyPress = true;
            e.Handled = true;
            OnLinkClicked(new LinkLabelLinkClickedEventArgs(new Link(0, this.Text.Length)));
        }
        else
        {
            base.OnKeyUp(e);
        }
    }

}

Use a function for the logic placed behind your LinkLabel click event. 使用函数作为LinkLabel单击事件后面的逻辑。 Then you can use the keyDown event to call the same function. 然后,您可以使用keyDown事件来调用相同的函数。

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

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