Results 1 to 7 of 7

Thread: [RESOLVED] Backspace to move back in Textbox

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2013
    Posts
    123

    Resolved [RESOLVED] Backspace to move back in Textbox

    My code currently checks if text length is greater than "4" and automatically tabs to the next textbox. For added convenience to the user, I'd like it to also move to the previous textbox when the user hits backspace on an empty textbox. And finally, I'd like it so when a user presses Enter on a textbox, the field automatically saves. Currently, I have this code below:

    Code:
    Private Sub CheckEntry(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtDate.TextChanged, txtStore.TextChanged
    
            Dim TB As TextBox = CType(sender, TextBox)
            If TB.Text.Length = TB.MaxLength Then
    
                If TB.SelectionStart = 4 Then
                    Me.SelectNextControl(TB, True, True, False, False)
                End If
            End If
    
        End Sub
    I know how to make it tab back from SelectNextControl, but I don't know how to make it do that automatically when backspace is pressed on an empty textbox? I tried adding this to my code, but got an error:

    Code:
       '///////PROBLEM CODE HERE @"TB.KEYCODE =" ERROR: KEYCODE IS NOT A MEMBER OF 'SYSTEM.WINDOWS.FORMS.TEXTBOX'
            If TB.SelectionStart = 0 And
                TB.KeyCode = Keys.Back Then
                Me.SelectNextControl(TB, False, True, False, False)
            End If
    I also tried changing the selection start to -1 and getting rid of the KeyCode bit, but of course that didn't work either. I'm sure the answer is pretty simple, but I'm just not finding it.

  2. #2
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Backspace to move back in Textbox

    Instead of using "SelectNextControl", do it the proper way.

    vb.net Code:
    1. '' Declare this at form level
    2.     Dim GroupTextBoxes() As TextBox
    3.  
    4.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    5.         '' add all the controls you want in the text group.
    6.         GroupTextBoxes = New TextBox() {txtDate, txtStore}
    7.  
    8.         '' attach the KeyUp event handler
    9.         For Each tb In GroupTextBoxes
    10.             AddHandler tb.KeyUp, AddressOf GroupTextBoxes_KeyUp
    11.         Next
    12.     End Sub
    13.  
    14.     Private Sub GroupTextBoxes_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
    15.         Dim TB As TextBox = CType(sender, TextBox)
    16.         Select Case e.KeyCode
    17.             Case Keys.Back
    18.                 '' handle the backspace key
    19.                 If TB.Text.Length = 0 Then
    20.                     If TB IsNot GroupTextBoxes.First Then
    21.                         Dim prevTB As TextBox = GroupTextBoxes(Array.IndexOf(GroupTextBoxes, TB) - 1)
    22.                         prevTB.Select()
    23.                     End If
    24.                 End If
    25.             Case Else
    26.                 '' other keys
    27.                 If TB.Text.Length = TB.MaxLength AndAlso TB.SelectionStart = TB.MaxLength Then
    28.                     If TB IsNot GroupTextBoxes.Last Then
    29.                         Dim nextTB As TextBox = GroupTextBoxes(Array.IndexOf(GroupTextBoxes, TB) + 1)
    30.                         nextTB.Select()
    31.                     End If
    32.                 End If
    33.         End Select
    34.     End Sub
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Backspace to move back in Textbox

    Quote Originally Posted by Pradeep1210 View Post
    Instead of using "SelectNextControl", do it the proper way.
    SelectNextControl is the proper way. It follows the Tab order and wraps at the ends so works like Tab and Shift+Tab.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Backspace to move back in Textbox

    Quote Originally Posted by jmcilhinney View Post
    SelectNextControl is the proper way. It follows the Tab order and wraps at the ends so works like Tab and Shift+Tab.
    Hmmm.. looks like I was much biased against the use of SelectNextControl. Didn't read the documentation well. It becomes much easier with the SelectNextControl, since we don't have to identify the current control in the collection any more.

    Put all the textboxes you want to group in a container. Say a Panel (named Panel1).
    Then this code would work:
    vb.net Code:
    1. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.     '' attach the KeyUp event handler
    3.     For Each tb In Panel1.Controls
    4.         AddHandler CType(tb, TextBox).KeyUp, AddressOf GroupTextBoxes_KeyUp
    5.     Next
    6. End Sub
    7.  
    8. Private Sub GroupTextBoxes_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
    9.     Dim TB As TextBox = CType(sender, TextBox)
    10.     Select Case e.KeyCode
    11.         Case Keys.Back
    12.             '' handle the backspace key
    13.             If TB.Text.Length = 0 Then
    14.                 Panel1.SelectNextControl(TB, False, True, False, False)
    15.             End If
    16.         Case Else
    17.             '' other keys
    18.             If TB.Text.Length = TB.MaxLength AndAlso TB.SelectionStart = TB.MaxLength Then
    19.                 Panel1.SelectNextControl(TB, True, True, False, False)
    20.             End If
    21.     End Select
    22. End Sub

    Or, you can remove the Form1_Load code all together and attach the event handler directly with the "Handles" clause specifying each TextBox separately.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Feb 2013
    Posts
    123

    Re: Backspace to move back in Textbox

    Thanks for the response Pradeep1210, but I could not get your code working with the panel. Nonetheless, I just tried some code I know and got it working, but I had to enable it in the Keyup event handler of each textbox as:

    Code:
    Private Sub txtLastFive_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtLastFive.KeyUp, txtDate.KeyUp
            If e.KeyCode = Keys.Enter Then
                btnLastFive_Click(sender, e)
            End If
    
            Dim TB As TextBox = CType(sender, TextBox)
            If TB.SelectionStart = 0 And e.KeyCode = Keys.Back Then
                Me.SelectNextControl(TB, False, True, False, False)
            End If
        End Sub
    This is now resolved. EDIT: The enter key bit was just some extra so I wanted and now everything works as I want!
    DOUBLE-EDIT: I put "not", but I meant to put "now. My bad.
    Last edited by Taem; May 19th, 2013 at 06:22 PM.

  6. #6
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Backspace to move back in Textbox

    Quote Originally Posted by Taem View Post
    Thanks for the response Pradeep1210, but I could not get your code working with the panel. Nonetheless, I just tried some code I know and got it working, but I had to enable it in the Keyup event handler of each textbox as:

    This is not resolved. EDIT: The enter key bit was just some extra so I wanted and now everything works as I want!
    I had tested the code before posting here. Probably you are doing something wrong. Are you sure that your textboxes are inside the panel?

    See the attached project. I have put textboxes outside of the panel also to show that it will work only for that panel only. So it should not automatically start moving the focus to next control of other textboxes also.
    Attached Files Attached Files
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Feb 2013
    Posts
    123

    Re: [RESOLVED] Backspace to move back in Textbox

    Your application works, yes. I think the problem with my program is I have another Form1_Load that positions the program on the window where I want it at program startup, and when I add your code, the page no longer goes to the correct position, so I tried making a separate Form_load after the page positioning, but for whatever reason, it did not work. I'll tinker with it some more and see what I can figure out, but in any case, I figured out how to do what I want it to do, so I think I have nothing more to add to this post. I thank you for your help and providing an alternative method for me to try.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width