views:

99

answers:

1

I need to have my edit box work the same for both tab and the enter key.

I have found lots of problems doing this in the application. Is there some way that I can send a tab key to the form/edit box.

(Note that this has to be in the Compact Framework.)


Solution:
Here is what I ended up using:

// This class allows us to send a tab key when the the enter key is pressed for the mooseworks mask control.   
public class MaskKeyControl : MaskedEdit
{
    [DllImport("coredll.dll", EntryPoint = "keybd_event", SetLastError = true)]
    internal static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

    public const Int32 VK_TAB = 0x09;

    protected override void OnKeyDown(KeyEventArgs e)
    {
        if (e.KeyData == Keys.Enter)
        {
            keybd_event(VK_TAB, VK_TAB, 0, 0);
            return;
        }
        base.OnKeyDown(e);
    }

    protected override void OnKeyPress(KeyPressEventArgs e)
    {
        if (e.KeyChar == '\r') 
            e.Handled = true;
        base.OnKeyPress(e);
    }
}

I am giving the answer to Hans because his code got me moving toward the right solution.

+2  A: 

You could try this control:

using System;
using System.Windows.Forms;

class MyTextBox : TextBox {
    protected override void OnKeyDown(KeyEventArgs e) {
        if (e.KeyData == Keys.Enter) {
            (this.Parent as ContainerControl).SelectNextControl(this, true, true, true, true);
            return;
        }
        base.OnKeyDown(e);
    }
    protected override void OnKeyPress(KeyPressEventArgs e) {
        if (e.KeyChar == '\r') e.Handled = true;
        base.OnKeyPress(e);
    }
}
Hans Passant
Alas that does not work. Somehow the tab works, but the select next control does not.I think it is because only the menus are enabled until tab is pressed and the control looses focus.Very frustrating....
Vaccano
Not sure what you mean, but you can't tab to another control if none are enabled or visible.
Hans Passant
Thanks for getting me started Hans. See the solution that worked in the question.
Vaccano