应用程序C#中的屏幕键盘

时间:2015-03-02 03:15:44

标签: c# winforms datagridview

我正在为触摸屏电脑的应用程序创建KEYPAD,我无法使用Windows屏幕键盘。在我的表单应用程序中我有我的KEYPAD GROUPBOX,我有多个文本框和一个datagridview。

我确实试图通过此代码

使用SendKeys.Send()

enter image description here

代码:

    public partial class Form1 : Form
  {
    const int W_ACTIVATE = 0x08000000;

    public Form1()
    {      
      InitializeComponent();      
    }

    protected override CreateParams CreateParams
    {
      get
      {
        CreateParams param = base.CreateParams;
        param.ExStyle |= W_ACTIVATE;
        return param;
      }
    }

    private void button1_Click(object sender, EventArgs e)
    {
     Button btn = (Button)sender;
        string btnTag = btn.Tag.ToString();
        switch (btnTag)
        {
            case "0":
                SendKeys.Send("(0)");

                break;
            case "1":
                SendKeys.Send("(1)");
                break;
            case "2":
                SendKeys.Send("(2)");
                break;
            case "3":
                SendKeys.Send("(3)");
                break;
            case "4":
                SendKeys.Send("(4)");
                break;
            case "5":
                SendKeys.Send("(5)");
                break;
            case "6":
                SendKeys.Send("(6)");
                break;
            case "7":
                SendKeys.Send("(7)");
                break;
            case "8":
                SendKeys.Send("(8)");
                break;
            case "9":
                SendKeys.Send("(9)");
                break;

            default:
                // Do Something
                break;
        }
    }
  }

但它不起作用,我需要帮助,我想知道如何将键输入到Datagridview中的文本框和活动单元格中。

1 个答案:

答案 0 :(得分:2)

您不能期望SendKeys能够正常工作:您的应用已经Focus,但您的输入控件都没有 - 您刚刚按下的Button

这是一种方法:

// the current input control
Control focusedCtl = null;

// variables to keep track of the cursor in the dgv cells
int curSelStart = 0;
TextBox curTB = null;


// this is the common Enter event for all our input controls
// we store which was entered/focused last
private void control_Enter(object sender, EventArgs e)
{
    focusedCtl = sender as Control;
}

// this is the common Click event for all buttons in your keypad
private void padButton_Click(object sender, EventArgs e)
{
    // reference to the sender
    Button padKey = sender as Button;

    // one way to get the text to process, Tag would be another
    string input = padKey.Text;

    // we do a lot of checks and beep&leave on fail
    if (focusedCtl == null) { Console.Beep(); return ;}

    // try to decide which is the receiving control
    DataGridView dgv = focusedCtl as DataGridView;
    TextBox tb = focusedCtl as TextBox;

    if (dgv != null)
    {
        DataGridViewCell cell = dgv.CurrentCell;
        if (cell == null) { Console.Beep(); return; }
        else
        {
            if (curTB == null) curSelStart = -1; 
            else curSelStart = curTB.SelectionStart;

            if (input == "C") cell.Value = cell.Value == null ? 
                              "" : TrimLast( cell.Value.ToString());
            else
            {
                string cs = cell.Value == null ? "" : cell.Value.ToString();

                if (curSelStart >= 0 && curSelStart < cs.Length) 
                     cell.Value = cs.Insert(curSelStart, input);
                else cell.Value = cs + input;
            }
        }
    }
    else  if (tb != null)
    {
        if (input == "C") tb.Text = TrimLast(tb.Text);
        else
        {
            int sstart = tb.SelectionStart;
            tb.Text = tb.Text.Insert(sstart, input);
            tb.SelectionStart += input.Length;
        }
    }

    else Console.Beep();
}

string TrimLast(string s) { return s.Substring(0, Math.Max(0, s.Length - 1)); }

// we need a reference to the Textbox that is used for editing
private void dataGridView1_CellEnter(object sender, DataGridViewCellEventArgs e)
{
    if (dataGridView1.CurrentCell.EditType == typeof(DataGridViewTextBoxEditingControl))
    {
        dataGridView1.BeginEdit(false);
        curTB = (TextBox)dataGridView1.EditingControl;
        if (curTB == null) return; 
        else curSelStart = curTB.SelectionStart;
        dataGridView1.BeginEdit(true);
    }
}

在DGV单元格中获取正确的光标位置并不像使用TextBox那样简单。它需要一个额外的事件和两个辅助变量。