单击 winforms (C#) 中的按钮后,如何将“SendKeys”发送到最后一个活动的输入文本框?
How to `SendKeys` to the last active input TextBox after clicking a button in winforms (C#)?
在 winforms (C#) 中单击按钮后如何 SendKeys
到最后一个活动输入 TextBox
?我是 C# 的新手,我正在尝试创建一个带有屏幕键盘的 winforms 应用程序,并且我有多个文本框。
我已经搜索并尝试了找到的指南,这是目前有效的示例。
当我将光标聚焦在我的 winforms 应用程序之外(记事本等)时,下面的代码可以正常工作,但是当我在 winforms 应用程序中单击我创建的 TextBox
,然后单击一个按钮 SendKeys
到 TextBox
,光标被移除,焦点在我点击的按钮上,这使得 TextBox
没有焦点。
const uint WS_EX_NOACTIVATE = 0x08000000;
const uint WS_EX_TOPMOST = 0x00000008;
protected override CreateParams CreateParams
{
get
{
CreateParams param = base.CreateParams;
param.ExStyle |= (int)(WS_EX_NOACTIVATE | WS_EX_TOPMOST);
return param;
}
}
private void btnA_Click(object sender, EventArgs e)
{
SendKeys.Send("A");
}
当我点击一个按钮SendKeys
到TextBox
时,我如何return将光标聚焦到最后一个活动的TextBox
?
致电
_recentTextbox.Select();
在发送密钥之前。存在另一种工作方式类似的方法 ( Focus()
),但它主要用于创建自定义控件的人
如果您有很多文本框并且您需要知道哪个文本框最近失去了焦点到您的按钮,请将相同的离开(或 LostFocus)事件处理程序附加到所有文本框:
private void Leave(object sender, EventArgs e){
_recentTextbox = (TextBox)sender; //_recentTextbox is a class wide TextBox variable
}
private void btnA_Click(object sender, EventArgs e)
{
if(_recentTextbox == null)
return;
_recentTextbox.Select();
SendKeys.Send("A");
_recentTextbox = null; //will be set again when a textbox loses focus
}
// 可选择转到其他字段:
SomeOtherField.Focus();
// 在插入点/选定文本上添加字符串:
SendKeys.Send("This is a test...");
Send() 方法会立即处理您传递的字符串;要等待程序的响应(如 auto-populated 数据库记录匹配输入),请改用 SendWait()。
在 winforms (C#) 中单击按钮后如何 SendKeys
到最后一个活动输入 TextBox
?我是 C# 的新手,我正在尝试创建一个带有屏幕键盘的 winforms 应用程序,并且我有多个文本框。
我已经搜索并尝试了找到的指南,这是目前有效的示例。
当我将光标聚焦在我的 winforms 应用程序之外(记事本等)时,下面的代码可以正常工作,但是当我在 winforms 应用程序中单击我创建的 TextBox
,然后单击一个按钮 SendKeys
到 TextBox
,光标被移除,焦点在我点击的按钮上,这使得 TextBox
没有焦点。
const uint WS_EX_NOACTIVATE = 0x08000000;
const uint WS_EX_TOPMOST = 0x00000008;
protected override CreateParams CreateParams
{
get
{
CreateParams param = base.CreateParams;
param.ExStyle |= (int)(WS_EX_NOACTIVATE | WS_EX_TOPMOST);
return param;
}
}
private void btnA_Click(object sender, EventArgs e)
{
SendKeys.Send("A");
}
当我点击一个按钮SendKeys
到TextBox
时,我如何return将光标聚焦到最后一个活动的TextBox
?
致电
_recentTextbox.Select();
在发送密钥之前。存在另一种工作方式类似的方法 ( Focus()
),但它主要用于创建自定义控件的人
如果您有很多文本框并且您需要知道哪个文本框最近失去了焦点到您的按钮,请将相同的离开(或 LostFocus)事件处理程序附加到所有文本框:
private void Leave(object sender, EventArgs e){
_recentTextbox = (TextBox)sender; //_recentTextbox is a class wide TextBox variable
}
private void btnA_Click(object sender, EventArgs e)
{
if(_recentTextbox == null)
return;
_recentTextbox.Select();
SendKeys.Send("A");
_recentTextbox = null; //will be set again when a textbox loses focus
}
// 可选择转到其他字段:
SomeOtherField.Focus();
// 在插入点/选定文本上添加字符串:
SendKeys.Send("This is a test...");
Send() 方法会立即处理您传递的字符串;要等待程序的响应(如 auto-populated 数据库记录匹配输入),请改用 SendWait()。