Silverlight / WPF - Enter/return key - Asked By David on 03-Jul-11 01:22 PM

 Hi All

 I am using a form to input data but the cursor will only move to the next cell on the Tab key after data is put in the cell
 I want this to move on the Enter/return key or the Tab key any Ideas please
pete rainbow replied to David on 03-Jul-11 02:12 PM
how about using this

on the form
protected override bool ProcessDialogKey(Keys keyData)
    {
        Keys key = (keyData & Keys.KeyCode);

        if (key == Keys.Enter)
        {
            keyData = Keys.Tab;
        }
        return base.ProcessDialogKey(keyData);
    }

or you could subclass any controls that you want with behaviour and override

protected override bool ProcessDialogKey(Keys keyData)
{
   switch (keyData)
   {
    case Keys.Enter:
    case Keys.Space:
     return base.ProcessDialogKey(Keys.Tab);
   }
   return base.ProcessDialogKey(keyData);
}
Ashish Kapoor replied to David on 03-Jul-11 02:52 PM
Make use of the property "tabIndex".
If the tabIndex of a textBox or any other control is 'i' (where i is an integer value) then change the tabIndex property of the control which u want to be the next on pressing the tab key to 'i+1'
David replied to pete rainbow on 04-Jul-11 04:48 AM
 Thanks for the quick reply Pete

 I am not sure where to enter the code on forms and what does the { mean
 I tried to copy and paste but just get errors
 I have been doing this programme for ages trying to copy one I have running in Access

 This is the top of code page for this form
 

Imports System.Data

Imports System.Configuration

Imports System.Data.OleDb

Imports System.Windows.Forms.Keys
__________________________________________

Class Window3  etc

 David

pete rainbow replied to David on 04-Jul-11 06:13 AM
sorry was in c# my fav language :-)

anyway will be something like ...

on the form
Protected Overrides Function ProcessDialogKey(ByVal keyData As System.Windows.Forms.Keys) As Boolean

   If (keyData = Keys.Enter ) Then
   keydata = Keys.Tab
      End If Return MyBase.ProcessDialogKey(keyData)

End Function
David replied to pete rainbow on 04-Jul-11 04:15 PM
 Hi Pete sorry to ask again but where on the form should I be looking to enter the code.

 Should it be on the XAML form or on the form code page I cant get anything to work

Thanks David
pete rainbow replied to David on 04-Jul-11 08:11 PM
sorry not focused been a busy day or two

anyway in the xaml.vb code behind the main window add this

        Protected Overrides Sub OnPreviewKeyDown(e As System.Windows.Input.KeyEventArgs)

      If (e.Key = Key.Enter) Then
        Dim ue As FrameworkElement
        ue = e.OriginalSource
        e.Handled = True
        ue.MoveFocus(New TraversalRequest(FocusNavigationDirection.Next))

      End If

    End Sub
David replied to pete rainbow on 05-Jul-11 12:13 PM
 Hi Pete

 Thanks for reply I have added the code and when I run the prog the form opens as a black page no input boxex etc
 Any ideas?

 Thanks David
pete rainbow replied to David on 05-Jul-11 12:18 PM
have you added the text boxes to the page?

don't think this code would affect it, but to be safe put an error handler around it, exception handler and see if anything going wrong?

if you comment out does it work ok?
David replied to pete rainbow on 05-Jul-11 01:45 PM


 Thanks Pete it now works perfect on Tab or Enter

 Now on to my next problem which I believe I have to post again

 Thanks David