none
Can Tab order Key Functionality Using Enter Key in VB.Net ? RRS feed

  • Question

  • I want moving Indicator between textboxs and ComboBoxs and DateTimePickers use key Enter Instead of the key Tab , My example :

    Monday, July 10, 2017 8:18 AM

Answers

  • Hi srajimuneer,

    If you want to use Key Enter instead of the Key Tab to move Indicator to the next Control, you can Handle the KeyDown event for each control, like this:

    Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown
            If e.KeyData = Keys.Return Then
                TextBox2.Focus()  ' The Next textbox to select
            End If
        End Sub
    
        Private Sub TextBox2_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox2.KeyDown
            If e.KeyData = Keys.Return Then
                DateTimePicker1.Focus()  
            End If
        End Sub
    
        Private Sub DateTimePicker1_KeyDown(sender As Object, e As KeyEventArgs) Handles DateTimePicker1.KeyDown
            If e.KeyData = Keys.Return Then
                ComboBox1.Focus()
            End If
        End Sub

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by srajmuneer Thursday, July 13, 2017 5:24 AM
    Tuesday, July 11, 2017 2:36 AM
    Moderator

All replies

  • You mean likewise in 1970 was done with an MS Dos screen. 

    Be aware that if you make your program commercially it will be returned because this is seen as a bug.


    Success
    Cor

    Monday, July 10, 2017 8:43 AM
  • I want moving Indicator between textboxs and ComboBoxs and DateTimePickers use key Enter Instead of the key Tab

    Pleas see: https://developer.microsoft.com/en-us/windows/apps/design

    Monday, July 10, 2017 10:22 AM
  • Microsoft has in the past supported that kind of thing. They have in the past published articles explaining how to do it.

    You should put this question in the forum corresponding to the UI that you are using. The solution might be different depending on whether you are using Windows Forms, WPF or UWP.

    First I will tell you what not to do. Don't process keyboard messages at the form level to do this. Many years ago I was answering questions in the CodeGuru forums. Many members suggested processing keyboard messages at the window level so I wrote Processing Keyboard Messages to explain how to process keyboard messages in an object-oriented manner.

    Some controls have special properties for the enter key, for example text box controls have properties determining what to do for the Enter key. Some might have events that are specific to the Enter key. You should look at the classes for the controls, such as the TextBox and DateTimePicker classes.

    You need to understand about tab order and how to set it.

    If there is any possibility that the program might be used by a blind person then it is more important that you use documented solutions.

    The details of all this depends on whether you are using Windows Forms, WPF or UWP.



    Sam Hobbs
    SimpleSamples.Info


    Monday, July 10, 2017 6:47 PM
  • Hi srajimuneer,

    If you want to use Key Enter instead of the Key Tab to move Indicator to the next Control, you can Handle the KeyDown event for each control, like this:

    Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown
            If e.KeyData = Keys.Return Then
                TextBox2.Focus()  ' The Next textbox to select
            End If
        End Sub
    
        Private Sub TextBox2_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox2.KeyDown
            If e.KeyData = Keys.Return Then
                DateTimePicker1.Focus()  
            End If
        End Sub
    
        Private Sub DateTimePicker1_KeyDown(sender As Object, e As KeyEventArgs) Handles DateTimePicker1.KeyDown
            If e.KeyData = Keys.Return Then
                ComboBox1.Focus()
            End If
        End Sub

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by srajmuneer Thursday, July 13, 2017 5:24 AM
    Tuesday, July 11, 2017 2:36 AM
    Moderator
  • Except it is better to use the method that will move to whatever control is in the tab order. I don't know if it the same for Forms, WPF  and UWP. I know the Windows SDK has the function so I assume .Net has wrapped it into a method. That way the tab order is used instead of hard-coding the sequence.

    In the Windows SDK we can sub-class controls and then we can put all the relevant processing in one place for each control. With MFC it is easy to sub-class controls and add functionality to controls. With .Net we must use a UserControl to do the equivalent. So a UserControl could be created for each control class, such as a TextBoxEX for TextBox. A UserControl for this would be small and easy to write and use.



    Sam Hobbs
    SimpleSamples.Info

    Tuesday, July 11, 2017 4:50 AM
  • With MFC it is easy to sub-class controls and add functionality to controls. With .Net we must use a UserControl to do the equivalent.

    There is no need to use crate a user control just to subclass a built-in .Net control.   A user control is useful where multiple controls are combined into one control.  Otherwise, simple subclassing is much easier.  However the user interface is such an integral part of a .Net control that overriding the default behaviour sometimes creates unexpected behaviour.

    Tuesday, July 11, 2017 5:01 AM
  • I have not seen any sub-classing capability in .Net. I have only seen user controls suggested in situations where sub-classing might be appropriate.


    Sam Hobbs
    SimpleSamples.Info

    Tuesday, July 11, 2017 5:24 AM
  • There is no need to use crate a user control just to subclass a built-in .Net control.   A user control is useful where multiple controls are combined into one control.  Otherwise, simple subclassing is much easier.

    You make it sound as if user controls are difficult.

    However the user interface is such an integral part of a .Net control that overriding the default behaviour sometimes creates unexpected behaviour.

    Huh? I don't understand.



    Sam Hobbs
    SimpleSamples.Info

    Tuesday, July 11, 2017 5:31 AM
  • The <Enter> key is usually used to submit the form, but if you are still interested in specific usage for reasonable purposes, then consider the next approach too.

    • You probably already have an OK and Cancel buttons. Add one more button called Button3, for example. Set the property TabStop: False.
    • Select the form and set the property AcceptButton: Button3.
    • Add a Click event handler for Button3:

        Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click

            SelectNextControl(ActiveControl, True, True, True, True)

        End Sub

    • In the Load event handler of the form, make the button invisible:

        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

            Button3.Left = -Button3.Width

        End Sub


    • Edited by Viorel_MVP Tuesday, July 11, 2017 6:21 AM
    Tuesday, July 11, 2017 6:19 AM
  • You make it sound as if user controls are difficult.

    It depends on what functionality you want to implement.  But subclassing an existing control is much easier than creating a user control, if you only want to work with the functionality of one control.  See (for example:
    https://docs.microsoft.com/en-us/dotnet/framework/winforms/controls/how-to-inherit-from-existing-windows-forms-controls

    Tuesday, July 11, 2017 7:18 AM
  • Unfortunately Microsoft does not encourage and use custom controls (subclassing controls) as much as they should. Microsoft usually uses and suggests user controls. I certainly prefer to derive controls instead of wrapping them in user controls and I was frustrated when I could not get them to work. I think I now understand something important that I did not previously understand.

    For this, custom controls and user controls are about the same. Neither one is especially easier or has significant advantages. The main disadvantage of custom controls is that for beginners there is much less about them in the Microsoft documentation and samples.

    For a custom (subclassed) TextBox control we change the base class to TextBox then add the following:

    protected override void OnKeyDown(KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Enter)
        {
            this.Parent.SelectNextControl(this, true, true, true, true);
            e.Handled = true;
        }
    }

    If we were to use a user control instead of a custom control (for a TextBox) then we would add a TextBox to the user control then add a PreviewKeyDown event and put the following in the event handler:

    // Multiline should always be false for this
    if (textBox1.Multiline == true)
        return;
    if (e.KeyCode == Keys.Enter)
        this.Parent.SelectNextControl(this, true, true, true, true);

    For the user control we would need to adjust the TextBox size based on the user control size using something such as:

    // subtract 4, but that is only a guess at the amount
    textBox1.Width = ClientSize.Width-4;



    Sam Hobbs
    SimpleSamples.Info



    Tuesday, July 11, 2017 8:54 PM
  • For a custom (subclassed) TextBox control we change the base class to TextBox then add the following:

    Please don't post C# code in the VB forum - it is very confusing for beginners.

    Tuesday, July 11, 2017 10:51 PM
  • For a custom (subclassed) TextBox control we change the base class to TextBox then add the following:

    Please don't post C# code in the VB forum - it is very confusing for beginners.


    Please don't be sarcastic when someone makes a simple mistake. I am sorry, but it was a mistake. It was not intentional; you imply it was.


    Sam Hobbs
    SimpleSamples.Info

    Wednesday, July 12, 2017 6:19 AM


  • Please don't be sarcastic when someone makes a simple mistake. I am sorry, but it was a mistake. It was not intentional; you imply it was.


    Sam Hobbs
    SimpleSamples.Info

    Sarcastic? The only sarcasm in this thread is from you. 

    ".Net seems to be designed by VB people more than by C++ people. "

    Yea lucky enough, often C++ people think they program for computers, while VB people program for end users. We see this in your reply which is only "It can be done".

    The same as someone who once positioned the steering wheel at the backside of the car just because that was done in old days as well often.

    Most of us know how it can be done, but it does not tell much about the ability to program for end users.  


    Success
    Cor



    Wednesday, July 12, 2017 6:26 AM
  • Sarcastic? The only sarcasm in this thread is from you. 

    I was not being sarcastic of you but you were being sarcastic of me.

    This sub-discussion is getting out of hand. If I don't reply then please don't misinterpret that as any agreement.



    Sam Hobbs
    SimpleSamples.Info

    Wednesday, July 12, 2017 7:26 AM