C# .NET - "Enter Key" to navigate through the input controls.

Asked By prasad civa on 30-May-08 07:41 PM

Hi,

I have a data entry form and our client wants the Enter key works like a Tab control (only to user input controls). I have seem many articles on how to do this (Send Keys, GetNextControl, SelectNextControl) but none of the approaches works for me. I want to have a public method that can be called in form's KeyPress event. Let me explain the issue I have:

I have a form with standard controls (textboxes, comboboxes, maskedTextboxes etc) and a user control which has standard controls again. If I tab then it goes through all the controls perfectly. However, if I hit enter key (code using "ActiveControl" property and "GetNextControl" method) it goes through the controls on the form but not through the controls in the user control.

private void Form1_KeyPress(object sender, KeyPressEventArgs e)

{

   SetFocusOnNextControl(this, ActiveControl, e);

}

private void SetFocusOnNextControl(Control container, Control activeControl, KeyPressEventArgs e)

{

try

{

if (e.KeyChar == (char)13)

{

Control Ctrl;

Ctrl = container.GetNextControl(activeControl, true);

if (Ctrl != null)

{

if (Ctrl is TextBox || Ctrl is ComboBox || Ctrl is MaskedTextBox || Ctrl is CheckBox || Ctrl is RadioButton || Ctrl is ListBox)

{

if (Ctrl.Visible && Ctrl.Enabled && Ctrl.TabStop)

{

Ctrl.Focus();

e.Handled = true;

return;

}

else

{

SetFocusOnNextControl(container, Ctrl, e);

}

}

else

{

SetFocusOnNextControl(container, Ctrl, e);

}

}

else

{

SetFocusOnNextControl(container, null, e);

}

}

}

catch (Exception ex)

{

MessageBox.Show(ex.Message);

}

}

I did tried tracking if the control returned by the "this.GetNextControl" method is a user control then use "usercontrolname.GetNextControl". It does return the correct next control in the usercontrol but the "ActiveControl" property on the form does not remember the actual control in the UserControl and if you hit enter key then it skips all the controls on the UserControl.   It is always remembering just the UserControlName.

Please let me know if any one encountered this situation and have a solution.

By the way I am using WinForms in VS2005 using C#.

Thanks

Civa.


Try this - Santhosh N replied to prasad civa on 31-May-08 01:05 AM

The user control on whole is treated as a control in the form. In order to get the control details of the user control, you need to expose the control properties...

I suppose you can try the following 2 approaches...
1) Implements the same logic inside user control...
2) Expose each control properties by writing properties in user control and call these properties at the main form level...

Set Focus to Next Control on "Enter Key" Press - Shailendrasinh Parmar replied to prasad civa on 31-May-08 01:09 AM

See the following articles ::

http://www.delphifaq.net/how-to-focus-the-next-control-after-pressing-enter/

http://www.access-programmers.co.uk/forums/showthread.php?p=674950
alice johnson replied to prasad civa on 01-Jun-08 06:22 AM

Refer this site  prasad.

http://www.bgxcomponents.com/flashSetFocusOnEnter.htm


Swapnil Salunke replied to prasad civa on 02-Jun-08 12:24 AM

Hello,

This should work

Private Sub Form_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
        ERV_Global.Focus_To_Next_Control(sender, e)
    End Sub
 
 
  Public Shared Sub Focus_To_Next_Control(ByVal sender As Object, ByVal strKeyCode As System.Windows.Forms.KeyPressEventArgs)
 
        If strKeyCode.KeyChar = Microsoft.VisualBasic.ChrW(13)  Then
            Me.GetNextControl(Me.ActiveControl, False).Focus()
        End If
 
    End Sub
And Follow this if problem persist
http://www.experts-exchange.com/Programming/Languages/.NET/Visual_Basic.NET/Q_23126607.html
http://forums.msdn.microsoft.com/en-US/netfxjscript/thread/0ae531f3-dc4a-4c2c-b06f-1bdecb0e0f07/
http://www.access-programmers.co.uk/forums/showthread.php?p=674950
Happy Coding Takecare 

  

"Enter Key" to navigate through the input controls. - prasad civa replied to alice johnson on 02-Jun-08 12:28 AM

Hi Johnson,

Thanks for your replay. The example mentioned requires quite a bit of coding on all the forms. I want to have one public method (may be another helper method) to do all these processing by using "GetNextControl" method. All I want to do is just call the public method on every form.

Thanks

"Enter Key" to navigate through the input controls. - prasad civa replied to Shailendrasinh Parmar on 02-Jun-08 12:35 AM

Hi Sanjay,

Thanks for your replay. The examples does not give any clue for what I was planning to implement.


How Enter key will work like tab key - shoaib rupom replied to prasad civa on 14-Oct-09 11:49 PM


please follow the link here is an example 

http://shoaibrupom.blogspot.com/2009/08/c.html