我需要让我的编辑框对 Tab 键和回车键的工作方式相同。
我在应用程序中发现了很多问题。有什么方法可以将 Tab 键发送到表单/编辑框。
(请注意,这必须在 Compact Framework 中。)
解决方案:
这是我最终使用的:
// This class allows us to send a tab key when the the enter key is pressed for the mooseworks mask control.
public class MaskKeyControl : MaskedEdit
{
[DllImport("coredll.dll", EntryPoint = "keybd_event", SetLastError = true)]
internal static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);
public const Int32 VK_TAB = 0x09;
protected override void OnKeyDown(KeyEventArgs e)
{
if (e.KeyData == Keys.Enter)
{
keybd_event(VK_TAB, VK_TAB, 0, 0);
return;
}
base.OnKeyDown(e);
}
protected override void OnKeyPress(KeyPressEventArgs e)
{
if (e.KeyChar == '\r')
e.Handled = true;
base.OnKeyPress(e);
}
}
我将答案交给 Hans,因为他的代码让我朝着正确的解决方案前进。
最佳答案
你可以试试这个控件:
using System;
using System.Windows.Forms;
class MyTextBox : TextBox {
protected override void OnKeyDown(KeyEventArgs e) {
if (e.KeyData == Keys.Enter) {
(this.Parent as ContainerControl).SelectNextControl(this, true, true, true, true);
return;
}
base.OnKeyDown(e);
}
protected override void OnKeyPress(KeyPressEventArgs e) {
if (e.KeyChar == '\r') e.Handled = true;
base.OnKeyPress(e);
}
}
关于c# - 如何以编程方式在 netcf 中发送 TAB 击键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2829333/