Click here to Skip to main content
15,900,477 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi, i use the TAB ORDER to move focus from one control to another.
But it functions only pressing the TAB KEY.
To move focus using the ENTER KEY i use the KEY PRESS method in all the objects of the Form.
There is a way to use the ENTER KEY with TAB ORDER without the KEY PRESS method???
Thanks & regard…
Shambhoo kumar
Posted

Hi Shambhoo,

I tried using Javascript, and here is the code for you

XML
<asp:TextBox ID="TextBox1" runat="server" onkeydown="return MoveNext('TextBox2',event.keyCode);"></asp:TextBox>
        <asp:TextBox ID="TextBox2" runat="server" onkeydown="return MoveNext('TextBox4',event.keyCode);"></asp:TextBox>
        <asp:TextBox ID="TextBox4" runat="server" onkeydown="return MoveNext('TextBox1',event.keyCode);"></asp:TextBox>
        <script type="text/javascript">
            function MoveNext(next_ctrl,_key) {
                if (_key == 13) {
                    var _next = document.getElementById(next_ctrl);
                    _next.focus();
                    return false;
                }
             }
        </script>


For windows application:

C#
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
       {
           if (e.KeyChar == 13)
           {
               textBox2.Focus();
           }
       }

I hope this would help you a bit,

Regards,
RK
 
Share this answer
 
v3
Comments
Shambhoo kumar 21-May-13 2:51am    
thanks sir . but my question is for windows application..
♥…ЯҠ…♥ 21-May-13 2:59am    
Updated my solution for windows application too...
Shambhoo kumar 21-May-13 3:21am    
actually i m using same code on my project. but in student registration from there are lots of control (aprox-100). and i dont want to write this code on everycontrol_ keypress event . so if u have any better idea so plz help me. and i also dont want to click all controls keypress event..
You should not do that.
The reason is simple: it is against the accepted guidelines for Windows applications, where TAB and BACK TAB move between fields (and the arrow keys in some cases) but ENTER will normally terminate and accept input - activate the SAVE, GO or OK button. If you subvert this mechanism, then your users are likely to become confused and get things wrong.

Why do you think this would be a good idea?
 
Share this answer
 
use Sendkeys.send method
Tips: add all textboxes' & controls' Keypress event you want to work as tab on enter key press.. see underline portion
VB
private sub textBox1_KeyPress(object sender, KeyPressEventArgs e) Handles textbox1.keypress, textbox2.keypress...

           if (e.KeyChar == 13)
           {
                SendKeys.Send("{TAB}")
           }
       }

Happy Coding!
:)
 
Share this answer
 
Comments
Shambhoo kumar 21-May-13 3:22am    
thanks Aarti

actually i m also using same code on my project. but in student registration from there are lots of control (aprox-100). and i dont want to write this code on everycontrol_ keypress event . so if u have any better idea so plz help me. and i also dont want to click all controls keypress event..
Aarti Meswania 21-May-13 3:28am    
add events dynamically for all textboxes in pageload event...
for each ctrl as control in ph.controls
if typeof (ctrl) is textbox then
AddHandler ctrl.keypress,AddressOf Textbox1_keypress
end if
next
Shambhoo kumar 21-May-13 4:03am    
Thanks Aarti
let me check.
Aarti Meswania 21-May-13 4:04am    
okay

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900