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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Changing ENTER to TAB in TComboBox

Status
Not open for further replies.

mattcs

Technical User
Jul 21, 2002
32
AU
Is anyone able to tell me how to change the behaviour of the ENTER key to mirror the TAB key when a TComboBox is the active control.

I can't seem to get the KeyPress events to work for either the Control or the Form (even with key preview on).

Thanks
 
I haven't tried it but how about
if(Key == '\r') Key = '\t';
it might just work..... Totte
 
On the compnent's OnKeyPress event, you will need to to add the following code
Code:
if (Key == VK_RETURN)
{
    // The RETURN key was pressed
    Key = 0; // trashes the RETURN
    Perform(WM_NEXTDLGCTL,0,0); // performs a tab to next control
}
James P. Cottingham

When a man sits with a pretty girl for an hour, it seems like a minute. But let him sit on a hot stove for a minute and it's longer than any hour. That's relativity.
[tab][tab]Albert Einstein explaining his Theory of Relativity to a group of journalists.
 
Another way to do it is in the keyupevent use the following code.

if (key == VK_RETURN)
keybd_event(VK_TAB,0,0,0);
 
Thanks to the people who have given me some suggestions but you will find that these are unlikely to work.

The reason I say this is that I have tried capturing the ENTER key in; OnKeyPress, OnKeyUp and OnKeyDown events for the TComboBox. Putting a break point into these events has no effect.

From this I am thinking that the ENTER key doesn't generate a KeyPress event for a TComboBox.

I have tried the same thing also without success using the OnKeyPress for the Form (with key preveiw on) and then ascertaining which control has the focus.

The interesting point is that when I press the ENTER key from 'within' the TComboBox, code I have associated with a TBitButon (an OK button) is activated. Setting the modal result of the OK button to mrNone has no effect.

Is this because of some 'modal result' type action that the TComboBox takes ?

Having the ENTER key behave like a TAB key is only a minor design decision in my program. It was simply to 'force' the user into a certain course of action.

If anyone else has some ideas (or sample code) I would certainly be most grateful.

Thanks
 
> the ENTER key doesn't generate a KeyPress event for a TComboBox.

with the key preview property set to true it should generate a key press event to the form

> Putting a break point into these events has no effect

are you sure you did not press the release button on options\compiler tab ?
 
Funny, it worked for me on my forms.
James P. Cottingham

When a man sits with a pretty girl for an hour, it seems like a minute. But let him sit on a hot stove for a minute and it's longer than any hour. That's relativity.
[tab][tab]Albert Einstein explaining his Theory of Relativity to a group of journalists.
 
Is the BitButton's Default property set to true? If so when you press enter in a text box it will be as if you clicked whatever button has Default=true. Try setting Default to false and see if it works. [Thanks in advance|Hope I helped you]
Exodus300
World-Wide Alliance of Evil and Twisted People
[red]"Experimentation by Example is the best learning tool" - Exodus300[/red]
Code:
if (pop_tarts == 0)
{
   tantrum = 1;
}
[pc3]
 
Many thanks to exodus300 for the clue that provided a solution.

Setting the Default property of the TBitButton to false enabled me to trap the ENTER key (as VK_RETURN) in the OnKeyPress handler for the TComboBox.

Within the handler I then set the focus to the button which sets the Default property of the button back to true so the OnClick handler for the button operates. Obviously when the combobox has the focus again, I will need to reset the button's default property back to false.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top