.NET Framework Bookmark and Share   
 index > Common Language Runtime > Tab key movement within textbox in C#
 

Tab key movement within textbox in C#

Hi,

I am the beginner of the C# programming. I am doing a project now and customer has a special requirement for the moving rule of Tab key.

There are 8 textbox in a windows form, named for example textbox1, textbox2, � textbox 8. The customer want to use the Tab key first move to textbox 1 then textbox 2, �until textbox8, after that he want to jump back to textbox 3 by pressing Tab key and then do loop within textbox3, textbox4 and textbox8 with Tab key.

I only know how to do loop within all textbox1, � textbox8, and I have no idea how to manage jump back to textbox3 and only do loop within textbox3, textbox4 and textbox8. Does anyone has this experience or any suggestions?

Thanks for your help!

/mhbj

mhbj
Hi,
When you reach the specific textbox.change the focus into textbox 1.i hope it will help you.
  private void textBox11_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode.Equals(Keys.Tab))
            {
                textBox1.Focus();
            }
        }

Best Regards, C.Gnanadurai ----------------------- Please mark the post as answer if it is helpfull to you
  • Marked As Answer bymhbj Wednesday, September 16, 2009 9:04 PM
  •  
Gnanadurai
set the TabIndex (from the property window of the textbox control) of textBox1-8 from 1 to 8.
Then use the method given by Ganadurai to bring the focus back to textBox3 from textBox8.

        private void textBox8_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode.Equals(Keys.Tab))
            {
                textBox3.Focus();
            }
        }


Hope this helps.
Paras
  • Marked As Answer bymhbj Wednesday, September 16, 2009 9:04 PM
  •  
paras kumar
Hi,
When you reach the specific textbox.change the focus into textbox 1.i hope it will help you.
  private void textBox11_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode.Equals(Keys.Tab))
            {
                textBox1.Focus();
            }
        }

Best Regards, C.Gnanadurai ----------------------- Please mark the post as answer if it is helpfull to you
  • Marked As Answer bymhbj Wednesday, September 16, 2009 9:04 PM
  •  
Gnanadurai
set the TabIndex (from the property window of the textbox control) of textBox1-8 from 1 to 8.
Then use the method given by Ganadurai to bring the focus back to textBox3 from textBox8.

        private void textBox8_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode.Equals(Keys.Tab))
            {
                textBox3.Focus();
            }
        }


Hope this helps.
Paras
  • Marked As Answer bymhbj Wednesday, September 16, 2009 9:04 PM
  •  
paras kumar
Thanks Gnanadurai and Paras. I tried your suggestions but it doesn't work :-(.
It still jump back from textbox8 to textbox1 by Tab key.
By the way, the textbox8 is actually a button not a textbox. And I sent these 8 items TabIndex 0-7.
Does these make it difference? (I think it should not...)

/Mei
mhbj
        void button1_LostFocus(object sender, EventArgs e)
        {
            this.textBox3.Focus();
        }

Paras
paras kumar
for each control that do not catch TAB key, override IsInputKey method :
Add following code.
        void button8_KeyUp(object sender, KeyEventArgs e)
        {
            this.textBox3.Focus();
        }

        void button8_KeyDown(object sender, KeyEventArgs e)
        {
            this.textBox3.Focus();
        }

        protected override bool IsInputKey(Keys keyData)
        {
            if (keyData == Keys.Tab) return true;
            return base.IsInputKey(keyData);
        }
Check this out :
http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/5a578bc3-2f28-47f5-8058-80206735d4a8
Paras
paras kumar
Hi,

KeyUp works, but it is not what I need. I want to stay in button8 until user press down tab key, then jump to texbox3.
Using keyup, it will jump to textbox3 immediately andwill notstay on button8 at all.

Adding IsInputkey or not make no difference.

Any more suggestion? Thanks!

mhbj
I did not get you...
whenever user presses tab (when the focus is on button) the focus should go on textbox3 right??
you can use key up for that.

why I have written event handlerfor both keyup and key down because sometimes IsInputKey doesnot work with key down...
Check this out :
http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/5a578bc3-2f28-47f5-8058-80206735d4a8

if you want to stay on button8 untill user press down tab key then you should not use keydown event, you should use keyup event only.

Hope this helps.

Paras
paras kumar
Hi paras,

I read some related links from web and finally found the problem. The button key is a little different with texbox. it needs PreviewKeyDown to help KeyDown event. I add the following code in my program and it works. Thanks for your help.


private
void button8_KeyDown(object sender, KeyEventArgs e)
{
e.Handled = ProcessKeyDown(e.KeyCode);
}

private void button8_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
ProcessKeyDown(e.KeyCode);
}

private bool ProcessKeyDown(Keys keyCode)
{
if (keyCode == Keys.Tab)
{
textBox3.Focus();
return true;
}
return false;
}

mhbj

You can use google to search for other answers

Custom Search

More Threads

• ExecutionEngineException, A very serious and bizarre bug!
• Is it correct C# reference and value analogs ?
• AspNet_wp.exe role in ServicedComponent Exceution
• Each object is going to be added in Finalize queue before it is Garbage Collected?
• Strong-Name Keys and Loading Assemblies
• Announcing PostSharp 1.0 Beta 1
• Getting a module's preferred base address
• Startup time of .NET application (Any CPU) slow on IA64 when compared with 32-bit
• .net 2 security issue - configuration in a client server environment to a shared network drive
• PermissionSet builder for AppDomain creation