none
MaskedTextBox and TextBoxes with SelectNextControl RRS feed

  • Question

  • Hi

    I have a form with a MaskedTextBox (txtMaskCode) and some TextBoxes

    I use the SelectNextControl to move between the textboxes and MaskedTextBox

    But when the cursor is into the MaskedTextBox and press the ENTER, don't move to the next textbox. Also the programm stop to run in debug mode

    From all the textboxes and the MaskedTextBox, the property is   TabStop=False

    Why happened?

    Private Sub txtMaskCode_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtMaskCode.KeyPress, txtEponymia.KeyPress
    
            Dim KeyAscii As Short = Asc(e.KeyChar)
            
            Select Case KeyAscii                       
                Case 13                                               
                    e.Handled = True                
                    SelectNextControl(ActiveControl, True, True, True, True)                
                    KeyAscii = 0
                Case 27                      
                    e.Handled = True
                    ExitToOtherControls()
                    KeyAscii = 0            
                Case Else
                    e.Handled = True
            End Select
        End Sub

    Saturday, August 3, 2019 7:03 PM

All replies

  • Hi

    Please explain what you want to happen. The code you show is stopping all input other than Asc(13) and Asc(27) and since you don't show the other code it is impossible to help.


    Regards Les, Livingston, Scotland

    Saturday, August 3, 2019 8:10 PM
  • Hello,

    This is the perfect opportunity to use the Visual Studio debugger or Console.WriteLine to see what's happening. I used the following and works perfectly (not the change on keyAscii variable as Asc is not a Short, it's an Integer.

    Public Class Form1
        Private Sub txtMaskCode_KeyPress(sender As Object, e As KeyPressEventArgs) _
            Handles txtMaskCode.KeyPress, txtEponymia.KeyPress
    
            Dim keyAscii As Integer = Asc(e.KeyChar)
    
            Select Case keyAscii
                Case 13
    
                    e.Handled = True
    
                    SelectNextControl(ActiveControl,
                                      True,
                                      True,
                                      True,
                                      True)
                Case 27
                    e.Handled = True
                    ExitToOtherControls()
                Case Else
                    e.Handled = True
            End Select
        End Sub
    
        Private Sub ExitToOtherControls()
            Console.WriteLine("ExitToOtherControls")
        End Sub
    End Class
    


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange


    Sunday, August 4, 2019 12:15 AM
    Moderator
  • My problem is in the MaskedTextBox

    When i press the ENTER, don't move the cursor to the next control -textbox

    The cursor remains in the maskedtextbox

    The source code is not complete, I have cut it to show you the point I am using to change the control

    In another part of the program the code works perfectly when I only have textboxes

    The MaskedTextBox has a mask
    Sunday, August 4, 2019 10:17 AM
  • Hi,

    If you don't want to lose focus,add code:

    Public Class Form1
    
        Private Sub txtMaskCode_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtMaskCode.KeyPress, txtEponymia.KeyPress
            Dim KeyAscii As Short = Asc(e.KeyChar)
    
            Select Case KeyAscii
                Case 13
                    e.Handled = True
                    SelectNextControl(ActiveControl, True, True, True, True)
                    KeyAscii = 0
                    txtMaskCode.Focus()
                Case 27
                    e.Handled = True
                    ExitToOtherControls()
                    KeyAscii = 0
                Case Else
                    e.Handled = True
            End Select
        End Sub
        Private Sub ExitToOtherControls()
            MsgBox("ExitToOtherControls")
        End Sub
    
    End Class
    

    Best Regards,

    Alex


    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.

    Monday, August 5, 2019 3:21 AM