Unable to use function

Pankaj tripathi 185 Reputation points
2023-11-28T12:12:11.6466667+00:00

Hi @KOZ6.0 ,
i was using the customtextbox code which you suggested and make some modifications in it as per my need . Now i am stuck at some point as i want to use a function

   protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {          
            if (keyData == Keys.Tab && Last)
            {
                return true;
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }


inside my code but found that i am unable to add it to the code . The intention is just to check if the user presses tab and if the Last property is set to true then the tab should do nothing . Please suggest how do i do it .

the code is quite long and i have inserted it in a txt file
micr.txt

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,869 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,610 questions
{count} votes

Accepted answer
  1. KOZ6.0 6,300 Reputation points
    2023-11-29T06:11:57.29+00:00

    To capture the tab key, catch the PreviewKeyDown event.

    public static void AttachEvents<T>(T txb) where T : TextBox, ICustomTextBox {
        txb.KeyDown += Txb_KeyDown;
        txb.GotFocus += Txb_GotFocus;
        txb.LostFocus += Txb_LostFocus;
        txb.ParentChanged += Txb_ParentChanged;
        txb.TextChanged += Txb_TextChanged;
        txb.Enter += Txb_enter;
        txb.KeyPress += Txb_KeyPress;
        txb.PreviewKeyDown += Txb_PreviewKeyDown; // ■ Add
        CustomNativeWindows.Add(txb, new CustomNativeWindow(txb));
    }
    
    public static void DetachEvents<T>(T txb) where T : TextBox, ICustomTextBox {
        txb.KeyDown -= Txb_KeyDown;
        txb.LostFocus -= Txb_LostFocus;
        txb.ParentChanged -= Txb_ParentChanged;
        txb.TextChanged -= Txb_TextChanged;
        txb.Enter -= Txb_enter;
        txb.GotFocus -= Txb_GotFocus;
        txb.KeyPress -= Txb_KeyPress;
        txb.PreviewKeyDown -= Txb_PreviewKeyDown; // ■ Add
        // remove from dictionary 
        if (OriginalBackColors.ContainsKey(txb)) {
            OriginalBackColors.Remove(txb);
        }
        if (CustomNativeWindows.TryGetValue(txb, out var customNativeWindow)) {
            customNativeWindow.Dispose();
            CustomNativeWindows.Remove(txb);
        }
    }
    // ■ Add Method
    private static void Txb_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e) {
        ICustomTextBox customTextBox = (ICustomTextBox)sender;
        if (e.KeyData == Keys.Tab && customTextBox.Last) {
            e.IsInputKey = true;
        }
    }
    

    If you set the IsInputKey property of the PreviewKeyDownEventArgs class to true, you will receive a WM_KEYDOWN message in WndProc. CustomNativeWindow class as follows

        case WM_KEYDOWN:
            Keys keyCode = (Keys)(int)(m.WParam.ToInt64() & 0xffff);
            if (keyCode == Keys.Tab) return; // ■ Add
            if (Control.ModifierKeys.HasFlag(Keys.Shift)) {
                switch (keyCode) {
                    case Keys.Up:
                    case Keys.Down:
                    case Keys.Right:
                    case Keys.Left:
                    case Keys.Home:
                    case Keys.End:
                        return;
                }
            }
            base.WndProc(ref m);
            break;
        ```
    
    
    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful