Jump to content
Xtreme .Net Talk

Recommended Posts

Posted
I am writing a custom control, and I was wondering how I could capture the Tab key. I am sure it is a fairly common problem, but I could not seem to find it on google.
"For every complex problem, there is a solution that is simple, neat, and wrong." - H. L. Mencken
Posted (edited)

Windows. Also, it seems the arrow keys aren't being captured either.

 

Edit: Moved to correct forum

Edited by PlausiblyDamp
"For every complex problem, there is a solution that is simple, neat, and wrong." - H. L. Mencken
Posted
I am using e.KeyData in OnKeyDown. It does not capture the tab key(it moves to the next tabstop instead).
"For every complex problem, there is a solution that is simple, neat, and wrong." - H. L. Mencken
Posted
would I have to do something so dramatic?? There is no way I can just set an option somewhere?
"For every complex problem, there is a solution that is simple, neat, and wrong." - H. L. Mencken
Posted
would I have to do something so dramatic?? There is no way I can just set an option somewhere?

 

I just suggested this because you expressed major interest in keypresses. This being the case i assumed you might be doing something that would warrant this. As for the Tab, i'm unsure how to trap it.

-Sean
Posted

I don't think KeyPress has a Cancel argument? Ok, you can use 'e.Handled = True'. There's still a problem in that the {Tab} key seems to be handled before the Keypress ever arrives at the control and therefore moves focus to the next control without any Control_KeyPress() events firing at all.

 

I think you need to either:

 

(1) Set the .TabStop properties for the Controls in question = False, or

 

(2) Set the Form.KeyPreview property = True, allowing you to trap/process the {Tab} keys there.

 

I think that idea #1 is easier to implement though.

Posting Guidelines

 

Avatar by Lebb

Posted

One of the properties of both TextBox & RichTextBox is AcceptsTab, and I am looking to implement such functionality in my own custom control, which means I don't want the user to have to change the tabstop of every one of the other controls on the same form.

 

Here i just an example of what I am looking for:

_
   public class Example : Control
   {
       public Example()
       {

       }

       protected override void OnKeyDown(KeyEventArgs e)
       {
           if (e.KeyData == Keys.Tab)
           {
               // Won't ever reach this point.
               // How can I fix that?
               MessageBox.Show("Tab caught!");
           }
           else if (e.KeyData == Keys.A)
           {
               MessageBox.Show("The letter a is caught!");
           }
       }
   }

"For every complex problem, there is a solution that is simple, neat, and wrong." - H. L. Mencken
Posted

Certain keys, such as Tab get special processing. Because of this, they never reach OnKeyDown. To disable the special processing, you can override ProcessDialogKey and return False for the keys in question:

    Protected Overrides Function ProcessDialogKey(ByVal keyData As System.Windows.Forms.Keys) As Boolean
       If keyData = Keys.Tab Then
           Return False
       Else
           Return MyBase.ProcessDialogKey(keyData)
       End If
   End Function

 

After you do that, they'll reach OnKeyDown just like any other key.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...