如何创建一个可以在不失去焦点的情况下向控件发送键的按钮 - 虚拟键盘 [英] How to create a Button that can send keys to a conrol without losing focus - Virtual Keyboard

查看:17
本文介绍了如何创建一个可以在不失去焦点的情况下向控件发送键的按钮 - 虚拟键盘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何制作一个可以将键发送到 datagridview 的按钮(所以我需要以某种方式将 datagridview 返回到失去焦点之前的状态)

How can I make a button that can sendkeys into a datagridview (so I need to somehow return datagridview to its state prior to it losing focus)

我会解释问题

我有一个带有 datagridview 和一些按钮的表单

I have a form with a datagridview and some buttons

我可以点击按钮,它会输入相应的字母,

I can click the button and it will enter the corresponding letter,

dataGridView1.Focus();
SendKeys.Send("a");  //or m or z, depending on the text of the button.

该按钮用于将数据输入到 datagridview.我可以使它只在调用 datagridview 的 enter 方法时启用.单击按钮时,datagridview 会失去焦点,因此在发送密钥之前,我必须再次给它焦点.因此,上面的两行.

The button is meant to be for entering data into datagridview. I could make it so it's enabled only when datagridview's enter method is called. When the button is clicked then the datagridview loses focus so I have to give it the focus again, prior to sending the key. Hence the two lines above.

我希望用户能够使用按钮将数据输入到数据网格中.

I want the user to be able to use the buttons to enter data into datagrid.

问题是,假设我希望用户能够输入文本maz",但我不能这样做.我只得到一个人 a 或 m 或 z.即使我可以将一个单元格放回可编辑模式,我也需要记住光标所在的位置..所以如果有人在单元格中输入 asdf 并将光标放在 d 和 f 之间,然后单击 m,我想要它说 asdmf.

The problem is, let's say I want the user to be able to enter the text "maz" I can't do it. I only get an individual a or m or z. And even if I could put a cell back into editable mode, i'd need to remember where the cursor was.. So if somebody typed asdf into a cell and put the cursor between d and f, and clicked m, i'd want it to say asdmf.

我不知道如何做到这一点.

I don't know how to accomplish that.

我有一个想法..也许如果有一种方法可以注册鼠标在按钮上并按下点击,但不会失去对数据网格视图的关注,那么这将是一种可以完成的方法.不过我不知道该怎么做,而且我对不同的方式感兴趣.

I have an idea.. that maybe if there was a way of registering that the mouse was over a button and the click was pushed, but without losing focus on datagridview, then that would be one way that it could be done. I don't know how to do that though, and i'm interested in different ways.

推荐答案

Non-Selectable Button to Send Key like Virtual Keyboard

制作一个不可选择的按钮,然后处理它的点击事件并发送键就足够了.通过这种方式,这些按钮可以像虚拟键盘按键一样工作,并且在将键发送到获得焦点的控件时,焦点将保持在具有焦点的控件上.

It's enough to make a non-selectable button and then handle it's click event and send key. This way these buttons can work like virtual keyboard keys and the focus will remain on the control which has focus while it sends the key to the focused control.

public class ExButton:Button
{
    public ExButton()
    {
        SetStyle(ControlStyles.Selectable, false);
    }
}

然后处理点击事件并发送key:

Then handle click event and send key:

private void exButton1_Click(object sender, EventArgs e)
{
    SendKeys.SendWait("A");
}

<小时>

控件的 SetStyle 扩展方法

是否有任何方法可以在不使用的情况下在 Control 上调用 SetStyle继承?

Is there any way of calling SetStyle on a Control without using inheritance?

是的,使用 反思,有.创建此扩展方法:

Yes, using Reflection, there is. Create this extension method:

public static class Extensions
{
    public static void SetStyle(this Control control, ControlStyles flags, bool value)
    {
        Type type = control.GetType();
        BindingFlags bindingFlags = BindingFlags.NonPublic | BindingFlags.Instance;
        MethodInfo method = type.GetMethod("SetStyle", bindingFlags);
        if (method != null)
        {
            object[] param = { flags, value };
            method.Invoke(control, param);
        }
    }
}

然后这样使用,在你的表单Load事件中:

Then use it this way, in your form Load event:

this.button1.SetStyle(ControlStyles.Selectable, false);

那么 button1 将是不可选择的,没有继承.

Then button1 will be non-selectable without inheritance.

这篇关于如何创建一个可以在不失去焦点的情况下向控件发送键的按钮 - 虚拟键盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆