all groups > vb.net controls > october 2005 >
You're in the

vb.net controls

group:

Handling the Tab key in a form


Handling the Tab key in a form Jason Hunt
10/25/2005 10:55:47 AM
vb.net controls:
Hello,

I'm displaying a form which has a TextBox for the user to type a search
string, and then a ListView containing all the entries that can be searched.
When the form is first displayed, all the entries are pulled from the
database and inserted into the ListView. As the user types a string in to
the TextBox, the ListView Item's are searched and the closest match is
automatically selected.

I am using the TextBox_KeyDown event to check for when the user presses Tab.
When Tab is pressed, the SortColumn of the ListView is changed. I quickly
discovered that I must disable the TabStop property on all of the objects in
order to capture the Tab key.

The problem is that when the form is first displayed, I want to focus the
cursor on the TextBox so that the user can just start typing right away.
The New constructor has TextBox.Focus() as it's last line, but it doesn't
work. If I set TextBox.TabStop=True, then it works and the TextBox is
focused right away, but then I can't capture the Tab key.

I need to either come up with a way to capture the Tab key while having
TabStop=True on some of the form objects, or else find a way to force the
TextBox to automatically be focused when the form is first displayed.

Any suggestions on how I can accomplish this?

On another note, how can I differentiate between Tab and Shift-Tab when
using the KeyDown event?

--
Jason Hunt
Advanced Computer Systems

Re: Handling the Tab key in a form Herfried K. Wagner [MVP]
10/27/2005 12:00:00 AM
"Jason Hunt" <jhunt@advcs.ca> schrieb:
[quoted text, click to view]

Check out the form's 'Process*Key' methods. These methods can be overridden
and you can use the overrides to catch the tab key.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>
Re: Handling the Tab key in a form j.sjouw NO[at]SPAM optimit.nl
10/27/2005 3:32:44 AM
Hi Jason,

You can set the focus to the textbox client-side by using javascript.
You need to create a javascipt function something like


function setTextFocus()
{
var txt = document.getElementById(TextBoxName);
txt.focus();
}

You can call this function in the body like:

<body onload="setTextFocus();">

To differentiate between Tab and Shift+tab you can use the modifiers
property of the keydown event, see:
http://www.devguru.com/technologies/javascript/10918.asp

Hope this helps.

Kind regards,

Justin Sjouw
Caesar Groep

Will you be my new colleague? Visit www.caesar.nl

[quoted text, click to view]
Re: Handling the Tab key in a form Jason Hunt
10/27/2005 9:27:32 AM
Sorry, I didn't specifty that I'm developing a "Windows Form" not a "Web
Form". My bad.

--
Jason Hunt
Advanced Computer Systems

AddThis Social Bookmark Button