Click here to Skip to main content
15,888,454 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
In my project on from i want to navigate between controls like textbox,combobox etc using 'Enter' Key Instead of 'Tab' key.
Have Any Setting or property is avialable for it?
Posted

I entirely agree with JSOP on this. What you want to do is non-standard.

However, if you absolutely must do it (and please think carefully before you decide to) follow these steps.

1. Handle either the OnKeyDown or OnKeyPress event for the controls that you want this to happen on. Look up the events on MSDN.
2. Test for the enter key (from memory the MSDN pages show you how to do this).
3. pseudo code in whichever handler you decide to use
if key is enter key
    SelectNextControl(sender, true, false, false, true); <==== real code
end if


Once again look up SelectNextControl on MSDN for the uses of the parameters.

Good luck! :-)
 
Share this answer
 
Comments
TaipeiJim 13-May-10 23:36pm    
Perhaps I should add that I was writing a simple but heavily used data entry application and the user definitely wants the 'enter' key to take him/her to the next field. (At the last item in the tab sequence, the user arrives at a button and the arrival 'presses' the button, which submits the input record and prepares for the next record. It's all VERY special-purpose, but highly tuned to make data entry quick and easy.)
Yatin Bhagat 14-May-10 1:05am    
by keypress event i can navigate between feilds easily, but i have to handle keypress of each control on the form and for this i have to write some line of code Repeatedly. So to avoid this i am looking for if is there any settings or property to set 'ENTER' key for navigation like TAB key
Tony Richards 14-May-10 6:52am    
Did you know that you can attach the same code to many event handlers? Or, why not just wrap it up in a function to reduce repeated code to a function call.
In my situation I wanted the enter key to function exactly like the tab key so I invented this event:

C#
private void WriteEnglish_KeyPress(object sender, KeyPressEventArgs e)
       {
           if (e.KeyChar == "\r"))
           {
               SendKeys.Send("\t");
           }
       }


Then I depend on the TabOrder to control where focus goes next. It works great for me.
 
Share this answer
 
v2
Comments
Yatin Bhagat 14-May-10 1:00am    
hey thanks its working.
That's a non-standard interface. The whole point of Windows is to provide a common interface for all applications.

The only way you could accomplish this is by eating the tab event, and handling the enter key event by manually determining which control to move to by examining the tab order of all controls and moving to the next control in the tab order that is set as a tab stop.

Again, this is extremely odd behavior for a Windows app, and you should expect users to report it as a bug, or at the very least, annoying as hell.
 
Share this answer
 
v2
Comments
Johnny J. 13-May-10 14:25pm    
Sorry John, but I have to disagree with you there. I've seens several situations where hitting the enter key produces unexpected behaviour, that might warrant such a change.

Besides: The OP didn't ask IF he should do it, but HOW he should do it. I would assume there is a reason for it...
#realJSOP 13-May-10 14:37pm    
When you press the enter key, one of two things should happen: a) the default button should post a click event, or b) you're in a multi-line edit field that accepts the enter key to start a new line. If you want to move from field to field, that's the job of the tab key. Anything else is non-standard.
Johnny J. 13-May-10 14:44pm    
I basically agree with you, but as mentioned, I have seen situations where a non-standard behaviour is wanted and required. So instead of deciding on behalf of the OP that he should not do it, I think the helpful thing to do is to tell him how to and leave it up to himself if he wants to do it or not...

Besides: With your nick, I shouldn't think you'd mind a little non-standard coding... :-)
#realJSOP 14-May-10 5:28am    
I'm really not interested in debating this.
Johnny J. 14-May-10 5:29am    
Of course not...
if you are working on windows form then you can use ProcessCmdKey and if you are using web form then you have to write custom function in javascript.
here is code for window form
C#
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
   {
       if ((msg.WParam.ToInt32() == 13))
       {
           SendKeys.Send("{Tab}");
           return true;
       }
       return base.ProcessCmdKey(ref msg, keyData);
   }

hope this will help you.
 
Share this answer
 
Hi Yatin,

In response to your comment on my previous answer, I am not aware of any property that will do this.

The only way that I know is to iterate over the controls collection of the Form and if the control is the type that requires this functionality, add your handler to its KeyPress or KeyDown event. Something like this:

C#
void frm1_Load(....)
{
    IterateThroughChildren(this);
}

void IterateThroughChildren(Control parent)
{
  foreach (Control c in parent.Controls)
  {
    if (c is TextBox || c is ComboBox) <============ See below
    {
        c.KeyDown += MyKeyDownHandler;
    }
    if (c.Controls.Count > 0)
    {
      IterateThroughChildren(c);
    }
  }
}

void MyKeyDownHandler(....)
{
    if (e.KeyCode == Keys.Enter)
    {
        ((Control)sender).SelectNextControl(....);
    }
}


The test for TextBox or ComboBox is there to avoid the event handler being applied to things like Panels
devise your own test to suit your circumstances.

One other thing to consider is what to do with Controls that might expect the enter key (Multi-line TextBoxes or ListBoxes for example). These will have to be handled differently, maybe test for Ctrl-Enter for those.

Good luck! :)
 
Share this answer
 
Hello,
paste this code in your keyupevent of textbox and hit enter on pressing enter combobox will have the focus :
private void textBox1_KeyUp(object sender, KeyEventArgs e)
        {
//condition is that if enter key is pressed then 
            if (e.KeyCode == Keys.Enter)
            {
//combobox will have the focus
                comboBox1.Focus();
            }
        }


and this code u have to paste it in ur entire forms controls key up event

Do rate my answer once you find it useful

Thanks & Regards
Radix :)
 
Share this answer
 
v3
Comments
Sandeep Mewara 14-May-10 10:19am    
Why replying to already accepted answer question? Whats the point?
radix3 14-May-10 10:37am    
Ok i wont

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