如何创建一个可以将密钥发送到控制而不会丢失焦点的按钮 - 虚拟键盘 [英] How to create a Button that can send keys to a conrol without losing focus - Virtual Keyboard

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

问题描述

如何创建一个可以将密钥发送到datagridview的按钮(所以我需要以某种方式将datagridview返回到它的状态,然后才会失去焦点)



我会解释问题



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





我可以点击按钮,它将输入相应的信件,

  dataGridView1.Focus(); 
SendKeys.Send(a); //或m或z,具体取决于按钮的文字。

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



我希望用户能够使用按钮将数据输入到datagrid中。



问题是,我想让用户能够输入我无法做到的文字maz。我只得到一个或一个或一个或一个个人。即使我可以把一个单元格重新编成可编辑的模式,我需要记住光标在哪里..所以如果有人把一个格式化成一个单元格,把光标放在d和f之间,点击m,我想要它说asdmf。



我不知道如何完成。



我有一个想法。这可能是如果有一种注册方式,鼠标在一个按钮上,点击被推送,但不会失去对datagridview的关注,那么这将是一种可以完成的方式。我不知道该怎么做,而且我对不同的方式感兴趣。

解决方案

不可选按钮,然后处理它的点击事件和发送键。这样,这些按钮可以像虚拟键盘键一样工作,并且焦点将保留在具有焦点的控件上,同时将键发送到重点控制。

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

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

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







有没有使用
继承来调用 SetStyle Control 上的方法?


是的。创建此扩展方法:

  public static class Extensions 
{
public static void SetStyle ,ControlStyles标志,bool值)
{
类型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);
}
}
}

然后以这种方式使用它,在您的表单加载事件:

  this.button1.SetStyle(ControlStyles可选,虚假); 

然后button1将无法继承。


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)

I'll explain the problem

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.

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.

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.

解决方案

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);
    }
}

Then handle click event and send key:

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


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

Yes 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);
        }
    }
}

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

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

Then button1 will be non-selectable without inheritance.

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

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