Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

What event is fired by the tab key press 1

Status
Not open for further replies.

Katy44

Technical User
Dec 12, 2003
723
GB
Can someone tell me how to capture a tab key press on a combo box? It doesn't seem to fire the KeyDown / KeyPress events?
Or am I missing something obvious?

Thanks
 
What exactly are you trying to achieve?

if you want to change the order of the TabStops, change the number sequence TabIndex value in each control's properies list.

If you want to be able to handle the TabKey yourself or use it as a character in a textbox... change the AcceptsTab property value to True, then keyPress and/or KeyDown/Up should catch it...

If it is for something not listed above, please provide more details.

Have Fun, Be Young... Code BASIC
-Josh

cubee101.gif


PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
I need to do something when a combobox is exited via a key press (I already have the enter key working as the tab key). I have a KeyDown event:

Code:
this.Controls[i].KeyDown += new KeyEventHandler(this.comboBox_KeyDown);

and then my method just outputs the KeyCode.

Code:
protected void comboBox_KeyDown(object sender, KeyEventArgs e)
		{
			MessageBox.Show(e.KeyCode.ToString());
...

if this is the enter key, the box flashes up "13".

If it is the tab key, the event is never called, as far as I can see.

Thanks for your help
 
btw... After you set AcceptsTab to true...
Code:
        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            textBox2.Text = e.KeyCode.ToString();
            if ((e.KeyCode.ToString() == "Tab"))
            {
                textBox2.Text = "Tab Pressed";
                textBox3.Text = "Shift: " + e.Shift;
            }
        }

*Note: I am using C# 2005 Express (Beta 1)

I don't know how to do this with the combo box though...

If you use Ctrl+Tab, KeyDown will catch it...

Have Fun, Be Young... Code BASIC
-Josh

cubee101.gif


PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
Yes, I can't see that property for a combobox. I wonder why?
 
I've been looking into this proprty, and it is intended to actually put a tab " " inside a textbox, which I didn't realise.
I want to use the tab (and the enter) to naviagte between controls.
 
Here you go...

I think I figured it out...
Code:
        private void comboBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode.ToString() == "Return")
            {
                GetNextControl((Control)sender, true).Select();
            }
        }

Is this what you were looking for?

Have Fun, Be Young... Code BASIC
-Josh

cubee101.gif


PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
And here is a better way to handle this...

Code:
        private void comboBox1_KeyDown(object sender, KeyEventArgs e)
        {
            [b]softTab(sender, e);[/b]
        }

        [b]private void softTab(object sender, KeyEventArgs e)
        {
            if (e.KeyCode.ToString() == "Return")
            {
                if (e.Shift)
                {
                    GetNextControl((Control)sender, false).Select();
                }
                else
                {
                    GetNextControl((Control)sender, true).Select();
                }
            }
        }[/b]

This way you can use Shift+Enter to traverse backwards and any control can reuse the code...

Have Fun, Be Young... Code BASIC
-Josh

cubee101.gif


PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
Thanks for your help, but my problem is that the tab key doesn't ever fire a KeyDown event when it is used to tab through a comboBox - unless I am missing something! I have tried

Code:
 private void comboBox1_KeyDown(object sender, KeyEventArgs e)
        {
            MessageBox.Show("Hello");
        }

and this is never called when tabbing through the comboboxes.
 
Ok...

I'm confused as to what exactly you want...

you said:
I want to use the tab (and the enter) to naviagte between controls.

The code I posted above...
Code:
        private void comboBox1_KeyDown(object sender, KeyEventArgs e)
        {
            softTab(sender, e);
        }

        private void softTab(object sender, KeyEventArgs e)
        {
            if (e.KeyCode.ToString() == "Return")
            {
                if (e.Shift)
                {
                    GetNextControl((Control)sender, false).Select();
                }
                else
                {
                    GetNextControl((Control)sender, true).Select();
                }
            }
        }

Makes the Enter key act the same way the Tab key does...

Why do you need to know what event the tab key fires?

It does not fire an event because it is preprocessed...
See this link:
...Which is handled when the control recieves a WM_KEYDOWN message.

The KeyDown event is fired after the message is handled, if an unhandled message still exists...

for more info on this...
do a google search for
c# WM_KeyDown
or
c# WM_KeyDown combobox

Good Luck,
Josh

Have Fun, Be Young... Code BASIC
-Josh

cubee101.gif


PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
Thanks for the article, it looks like it may help, when I can get my head round it!
I have already set up the Enter key to work in the same way as the tab, but I cause the combobox to do certain things before it is exited. I have caught this when they are exiting via Enter, but also need to do it when they are exiting via tab.

(Before you ask - the Leave event doesn't seem to do what I want, although I get the feeling I am making this unnecessarily complicated :))
 
That looks like exactly what I need, thank you very much for your help
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top