Click here to Skip to main content
15,890,825 members
Home / Discussions / C#
   

C#

 
QuestionTransferring focus from a textbox to a button Pin
gamer112724-Jan-10 2:49
gamer112724-Jan-10 2:49 
AnswerRe: Transferring focus from a textbox to a button Pin
Som Shekhar24-Jan-10 3:06
Som Shekhar24-Jan-10 3:06 
GeneralRe: Transferring focus from a textbox to a button Pin
Rod Kemp24-Jan-10 3:16
Rod Kemp24-Jan-10 3:16 
GeneralRe: Transferring focus from a textbox to a button Pin
Som Shekhar24-Jan-10 3:18
Som Shekhar24-Jan-10 3:18 
GeneralRe: Transferring focus from a textbox to a button [Modified] Pin
Rod Kemp24-Jan-10 3:34
Rod Kemp24-Jan-10 3:34 
AnswerRe: Transferring focus from a textbox to a button Pin
Jimmanuel24-Jan-10 3:16
Jimmanuel24-Jan-10 3:16 
GeneralRe: Transferring focus from a textbox to a button Pin
gamer112724-Jan-10 3:53
gamer112724-Jan-10 3:53 
GeneralRe: Transferring focus from a textbox to a button Pin
Jimmanuel24-Jan-10 4:23
Jimmanuel24-Jan-10 4:23 
But they don't all have the same behaviour, they have similar behaviour. It's not a bad idea, but you have to abstract away all of the things from the handler that are specific for each control. You can actually do this pretty elegantly with one handler by using the Controls Tag property and the sender param of the handler.

The idea is that the Enter key press transfers focus to the next Control, right? That's the similar behaviour the handler should implement but it needs to know which Control to transfer focus to. That's easy with the Tag. In the constructor of your form go through the similar behaving Controls and save a reference to the next Control that gets focus in the Tag:
txtBoxLogIn.Tag = txtBoxPassword; // give focus to the password box
txtBoxPassword.Tag = btnProceedLogIn; // give focus to the button
btnProceedLogIn.Tag = null; // set this to null to signify the end of the chain

Then your handler simplifies to this:
private void focusForwardingControl_KeyDown(object sender, KeyEventArgs e)
{
    Control control = sender as Control;
    if (control == null)
    {
        // if this event isn't from a Control object then return
        return;
    }

    if (e.KeyCode == Keys.Enter)
    {
        // get the next Control to have focus
        Control nextControl = control.Tag as Control;
        if (nextControl != null)
        {
            // if there's a valid Control stored then give it focus
            nextControl.Focus();
        }
    }
}

Then both text boxes can use to the same event handler to forward focus to the next Control in the chain.

Note that I'm using the KeyDown handler instead of KeyPress, I like the Event Args better.

Also, all code here was written in the CP post editor so it hasn't been tested or compiled Smile | :)

Badger | [badger,badger,badger,badger...]

GeneralRe: Transferring focus from a textbox to a button Pin
gamer112724-Jan-10 4:34
gamer112724-Jan-10 4:34 
GeneralRe: Transferring focus from a textbox to a button Pin
gamer112724-Jan-10 4:14
gamer112724-Jan-10 4:14 
AnswerRe: Transferring focus from a textbox to a button Pin
gamer112724-Jan-10 4:18
gamer112724-Jan-10 4:18 
GeneralRe: Transferring focus from a textbox to a button Pin
Jimmanuel24-Jan-10 4:38
Jimmanuel24-Jan-10 4:38 
GeneralRe: Transferring focus from a textbox to a button Pin
gamer112724-Jan-10 5:18
gamer112724-Jan-10 5:18 
GeneralRe: Transferring focus from a textbox to a button Pin
Jimmanuel24-Jan-10 5:32
Jimmanuel24-Jan-10 5:32 
GeneralRe: Transferring focus from a textbox to a button Pin
gamer112724-Jan-10 5:53
gamer112724-Jan-10 5:53 
GeneralRe: Transferring focus from a textbox to a button Pin
Jimmanuel24-Jan-10 5:58
Jimmanuel24-Jan-10 5:58 
GeneralRe: Transferring focus from a textbox to a button Pin
ragnaroknrol25-Jan-10 2:36
ragnaroknrol25-Jan-10 2:36 
QuestionMy Options Window Pin
Jassim Rahma24-Jan-10 1:50
Jassim Rahma24-Jan-10 1:50 
AnswerRe: My Options Window Pin
Som Shekhar24-Jan-10 2:22
Som Shekhar24-Jan-10 2:22 
QuestionPassing and displaying variables in forms Pin
Wogboiii23-Jan-10 23:36
Wogboiii23-Jan-10 23:36 
AnswerRe: Passing and displaying variables in forms Pin
Som Shekhar23-Jan-10 23:51
Som Shekhar23-Jan-10 23:51 
GeneralRe: Passing and displaying variables in forms Pin
Wogboiii24-Jan-10 0:01
Wogboiii24-Jan-10 0:01 
GeneralRe: Passing and displaying variables in forms Pin
Som Shekhar24-Jan-10 0:05
Som Shekhar24-Jan-10 0:05 
GeneralRe: Passing and displaying variables in forms Pin
Wogboiii24-Jan-10 0:09
Wogboiii24-Jan-10 0:09 
GeneralRe: Passing and displaying variables in forms Pin
Som Shekhar24-Jan-10 0:14
Som Shekhar24-Jan-10 0:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.