C# 应用程序C中的屏幕键盘#
我正在尝试为触摸屏计算机的应用程序创建键盘,我不能使用windows屏幕键盘。在我的表单应用程序中,我有我的键盘组框,我有多个文本框和一个datagridview 我确实试着用这段代码使用SendKeys.Send() 代码:C# 应用程序C中的屏幕键盘#,c#,winforms,datagridview,C#,Winforms,Datagridview,我正在尝试为触摸屏计算机的应用程序创建键盘,我不能使用windows屏幕键盘。在我的表单应用程序中,我有我的键盘组框,我有多个文本框和一个datagridview 我确实试着用这段代码使用SendKeys.Send() 代码: public partial class Form1 : Form { const int W_ACTIVATE = 0x08000000; public Form1() { InitializeCompon
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中的文本框和活动单元格中键入键,这是聚焦的。你不能期望
发送键
起作用:你的应用程序有聚焦
,但是你的输入控件没有-你刚才按下的按钮有
以下是一种方法:
// 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);
}
}
//当前输入控件
控制焦点DCTL=null;
//用于跟踪dgv单元格中光标的变量
int curSelStart=0;
TextBox curTB=null;
//这是所有输入控件的常见输入事件
//我们选择了上次进入/关注的门店
私有无效控件输入(对象发送方,事件参数e)
{
focusedCtl=发送方作为控制;
}
//这是键盘中所有按钮的常见单击事件
私有无效padButton_单击(对象发送者,事件参数e)
{
//对发件人的引用
按钮padKey=发送器为按钮;
//获取要处理的文本的一种方法是标记
字符串输入=padKey.Text;
//我们做了很多检查,失败时发出嘟嘟声并离开
if(focusedtl==null){Console.Beep();return;}
//尝试确定哪个是接收控件
DataGridView dgv=focusedCtl作为DataGridView;
TextBox tb=焦点dctl作为TextBox;
如果(dgv!=null)
{
DataGridViewCell单元格=dgv.CurrentCell;
if(cell==null){Console.Beep();return;}
其他的
{
如果(curTB==null)curseStart=-1;
else curSelStart=curTB.SelectionStart;
如果(输入=“C”)单元格。值=单元格。值==null?
“”:修剪(cell.Value.ToString());
其他的
{
字符串cs=cell.Value==null?”:cell.Value.ToString();
if(curseStart>=0&&curseStart
在DGV单元格中获得正确的光标位置并不像使用文本框那样简单。它需要一个额外的事件和两个辅助变量。您只需设置文本框的Text属性,无需使用sendkey模拟击键。好的,我如何找到哪个控件处于活动状态[TextBox]、Datagridview[cell,row]、????您是否尝试过:SendKeys.Send(“0”,True);好的,你可以遍历每个控件(以及每个控件的每个子控件,等等)并检查“IsFocused”属性,或者你可以通过谷歌查看是否有“Form.FocusedControl”属性或者你可以从中获得它的东西。谢谢你的帮助,伙计,,,,我只是添加FocusedCtrl=sender作为控件;对于DataGridView1u CellEnter,每件事都很完美,当我们输入超过3位的最后一位数字时,会出现一个小错误,最后一位数字变为前一位。。。