views:

58

answers:

4

i want to know how to move the cursor from current textbox to previous textbox. i am creating textbox dynamically by enter keypress event one by one

e.g.

textbox1

textbox2

textbox3

suppose total three textbox now created and am on third one textbox means my cursor on textbox3 and i wants to move the cursor to textbox2 or focus to textbox2 for modify.

how do i do? please suggest proper code for the same.

+1  A: 

If the tab orders are set correctly you can use GetNextControl method to move find previous control.

Giorgi
sir,how can fix tab orders on run time dynamically created textbox please suggest.
mahesh
on enter keypress event
mahesh
+2  A: 

Basing on Giorgi's solution:

foreach(var tbox in new[]
     {
         tbox0, tbox1, tbox2
     })
{
    tbox.KeyPress += (sender,e) => keypressed(sender,e);
}

private void keypressed(object sender, KeyPressEventArgs e)
{
    if (e.KeyChar == (char)Keys.Back)
        ((Control)sender).GetNextControl((Control)sender, false).Select(); // .Focus()
}
abatishchev
sir,it's dynamically created textbox on enter keypress event next time it will be more than three box than.
mahesh
@mahesh: `var textBox = new TexBox() { TabIndex = ++i; };`
abatishchev
thx for help it will help me lots
mahesh
+2  A: 

Be careful that it won't cause problems in usability, like using BACKSPACE to erase the last letter, will also jump to the previous control, that will frustrate any user!

Only jump controls if the current control text is empty, and the user pressed backspace:

    private void textBox1_KeyUp(object sender, KeyEventArgs e)
    {
        if ((e.KeyCode == Keys.Back) & (((Control)sender).Text.Length == 0))
        {
            this.SelectNextControl((Control)sender, false, true, true, true);
        }
    }
Wez
+1  A: 

Backspace is an important editing key for the user, don't mess with it. Press Tab to move forward, Shift+Tab to move backward. Your user is likely to already know this shortcut.

Hans Passant
very true sir,am also familiar of it but sir can u tell me if user enter the entries on large scale and suddenly he realize that some of text entries are wrong. then as per ur suggestion he press shift+tab but the text on previous textbox is remain the same and he have to modify it manually by deleting characters on it. so sir, guide me on it. thx for ur co-operation.
mahesh
The user will simply click on the bad text, press backspace or delete as necessary and type the replacement. You don't have to help. Trying to help will just make it more difficult for the user.
Hans Passant
thx for reply it will helpful for me.
mahesh