C# Textbox Focus



在Windows表单C#应用程序中我有两个文本框控件。

我将Tabindex 1设置为第一个控制我将Tabindex 2设置为第二次控制

当我按第一个控件中的选项卡时,我需要进行一些检查操作。

我尝试使用此代码拦截标签键

control1__KeyPress(...)
{
   if (e.KeyChar == (char)Keys.Tab)
    {
     ....
    }
}

但事件没有被解雇。

我尝试将其做到

control1__KeyDown(...)
{
}

,但事件也没有被解雇。

我如何在第二个控件具有焦点之前拦截标签键?

谢谢

正如我在评论中所说的那样,您应该根据您的要求使用适当的事件处理程序。如果您的意图仅仅是为了验证用户输入,则有一个验证事件可以帮助您。如果要在控制宽松的焦点时要提醒您的意图,就会有休假事件。

如果您真的只想处理选项卡键,那么您应该像在此示例中一样覆盖ProcessCmdkey

public class Form1 : System.Windows.Forms.Form
{
    protected override bool ProcessCmdKey(ref Message msg, Keys keyData )
    {
        if( keyData == Keys.Tab)
        {
            // This method will be called for every control in your form
            // so probably you need to add some checking on the Handle
            // of the control that originates the call
            if(msg.Hwnd == yourTextBox.Handle)
            {
                 ExecuteYourTabProcessinLogicHere();
                 // Returning TRUE here halts the normal behavior of the 
                 // TAB key, you could change this based on the result 
                 // of your logic
                 return true; 
            }
        }
        return base.ProcessCmdKey(ref msg, keyData);
    }
}

但是,我应该警告您,对于您的用户来说,弄乱Tab键可能会感到非常不安,希望此密钥具有预定义的行为

最新更新