views:

23

answers:

1

I have a few textboxes. I'd like to point the user each time to the next textbox, on pressing enter. Textboxes have Tabindex set up correctly.

I got something like:

 private void textBox_Description_KeyPress(object sender, KeyPressEventArgs e)
    {
        if (e.KeyChar == (char)Keys.Enter)
        {
            e.Handled = true;
            setFocusOnNextElement(sender);
        }
    } 

How should setFocusOnNextElement look like? If i want to make it general. I could parse each control, and find what's next, but I have a feeling that this can be done nicer.

+4  A: 

I would not advise constructing the function just as you have it, as it would require that the parameter be an object.

private static void SetFocusOnNextElement(Control control)
{
    Control target = Control.GetNextControl(control, true);

    if (target != null) target.Focus();
}

Then invoke it like this:

SetFocusOnNextElement((Control)sender);
Adam Robinson