Thread: moving from one textBox to another one

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    183

    moving from one textBox to another one

    Hello,

    I have three textBoxes (textBox1, textBox2 and textBox3).
    Each textBox gets 2 digits.

    For example
    textBox1 -> 01
    textBox2 -> 32
    textBox3 -> 78

    I need to press the tab botton or use the mouse, to move from textBoxi to textBoxj.
    I have seen some windows forms in which the cursor automatically jumps to the next textBox after proper number of digits are entered.

    I mean, for example when "01" is entered in textBox1, the cursor automatically jumps to textBox2 and so on.

    How can I write such a code?


    Thank you
    Arian

  2. #2
    Registered User
    Join Date
    Mar 2009
    Location
    england
    Posts
    209
    Use the KeyDown or KeyPress event to validate the user input. If satisfied the textbox contains the required data, move onto the next textbox using the Focus() method. Eg:

    Code:
    int i;
    
    if (textbox1.Text.Length == 2 && int.TryParse(textbox1.Text, out i))
        textbox2.Focus();

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Building on what theobe said, instead of doing textbox2.Focus(), there's the Control.SelectNextControl() method to automatically set focus on the next control in the tab order.
    If you understand what you're doing, you're not learning anything.

  4. #4
    Registered User
    Join Date
    Nov 2003
    Posts
    183
    Thank you

    I checked "Control.SelectNextControl"

    but I have a problem with the first argument "ctl as control". what is that?


    what should I write in the code to call the function?
    Code:
    Control.SelectNextControl(???,true,true,true,true),

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Check out the examples on this page.

    You can pass it ActiveControl to pass it the control that currently has focus.
    If you understand what you're doing, you're not learning anything.

  6. #6
    Registered User
    Join Date
    Nov 2003
    Posts
    183
    sorry but it doesn't work

    if I dont press the tab botton, all the digits will be written in the first textBox

    here is my code:

    Code:
    private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
            {
                Control ctl;
    
                this.SetStyle(ControlStyles.Selectable, true);
                this.UpdateStyles();
    
                if (textBox1.Text.Length == 2)
                {
                    ctl = (Control)sender;
                    ctl.SelectNextControl(ActiveControl, true, true, true, true);
                }
            }
    What am I doing wrong?

  7. #7
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    ctl.SelectNextControl(ActiveControl, true, true, true, true);

    Remove the "ctl.", you should call it on the form, not the control.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  8. #8
    Registered User
    Join Date
    Nov 2003
    Posts
    183
    Thank you so much

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Textbox
    By maxorator in forum Windows Programming
    Replies: 20
    Last Post: 09-25-2005, 10:04 AM
  2. Textbox
    By Salibea in forum C++ Programming
    Replies: 6
    Last Post: 07-27-2005, 09:55 AM
  3. Get Int with TextBox
    By Squintz in forum C# Programming
    Replies: 1
    Last Post: 01-27-2003, 09:02 AM
  4. How can I add to TextBox??(MFC)
    By c-- in forum Windows Programming
    Replies: 4
    Last Post: 09-03-2002, 06:32 AM
  5. How would I add a textbox?
    By -KEN- in forum C# Programming
    Replies: 2
    Last Post: 11-13-2001, 02:02 PM