简体   繁体   English

如何在不聚焦的情况下在按键上进行按钮更改?

[英]How do I make a button change on keypress without focusing it?

I'm trying to make a program in which when you hit a key it will show a button in a keyboard like row on screen so I'm trying to make the buttons change color while pressed and go back to normal when released without focusing the button, is there a way in which I can make this?我正在尝试制作一个程序,当你按下一个键时,它会在键盘上显示一个按钮,如屏幕上的行,所以我试图让按钮在按下时改变颜色,go 在释放时恢复正常而不聚焦按钮,有没有办法可以做到这一点?

I've been looking at a lot of tutorials and code snippets but none of them seems to work in my case.我一直在看很多教程和代码片段,但它们似乎都不适用于我的情况。

the reason why I don't want to focus the buttons is because I have a text box in which written text is shown and changing the focus would just be a bother because you would have to click in it every time you want to write a character.我不想集中按钮的原因是因为我有一个文本框,其中显示了书面文本,更改焦点会很麻烦,因为每次你想写一个字符时都必须点击它.

Force the focus back to the TextBox .强制焦点回到TextBox

Example:例子:

button1.Click += (sender, args) => 
{
    // where "this" is a Form, if you are inside another control,
    // try "ParentForm" (works for Panel)
    this.ActiveControl = textBox1;
};

About the colors, I usually do with Label , but you can change the following to Button关于 colors,我通常用Label做,但您可以将以下内容更改为Button

internal class LabelButton : Label
{
    public Color MouseOverColor {get; set;} = Color.Red;
    public Color MouseDownColor {get; set;} = Color.Green;
    public Color NormalColor {get; set;} = Color.Blue;

    public LabelButton() {
        MouseDown  += (sender, args) => BackColor = MouseDownColor;
        MouseUp    += (sender, args) => BackColor = NormalColor;
        MouseEnter += (sender, args) => BackColor = MouseOverColor;
        MouseLeave += (sender, args) => BackColor = NormalColor;
    }
}

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

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