应用程序 C# 中的屏幕键盘
On Screen Keypad in application C#
我正在尝试为我的触摸屏计算机应用程序创建 KEYPAD,但我无法使用 windows 屏幕键盘。在我的表单应用程序中,我有我的 KEYPAD GROUPBOX,我有多个文本框和一个数据网格视图。
我确实尝试通过此代码使用 SendKeys.Send()
代码:
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 中的活动单元格中,这是焦点。
您不能指望 SendKeys
工作:您的应用程序有 Focus
,但是您的输入控件 none 有 - 您刚刚按下的 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 单元格中获取正确的光标位置并不像使用文本框那么简单。它需要一个额外的事件和两个辅助变量。
我正在尝试为我的触摸屏计算机应用程序创建 KEYPAD,但我无法使用 windows 屏幕键盘。在我的表单应用程序中,我有我的 KEYPAD GROUPBOX,我有多个文本框和一个数据网格视图。
我确实尝试通过此代码使用 SendKeys.Send()
代码:
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 中的活动单元格中,这是焦点。
您不能指望 SendKeys
工作:您的应用程序有 Focus
,但是您的输入控件 none 有 - 您刚刚按下的 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 单元格中获取正确的光标位置并不像使用文本框那么简单。它需要一个额外的事件和两个辅助变量。