[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.
Re: Backspace to move back in Textbox
Instead of using "SelectNextControl", do it the proper way.
vb.net Code:
'' Declare this at form level
Dim GroupTextBoxes() As TextBox
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'' add all the controls you want in the text group.
GroupTextBoxes = New TextBox() {txtDate, txtStore}
'' attach the KeyUp event handler
For Each tb In GroupTextBoxes
AddHandler tb.KeyUp, AddressOf GroupTextBoxes_KeyUp
Next
End Sub
Private Sub GroupTextBoxes_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
Dim TB As TextBox = CType(sender, TextBox)
Select Case e.KeyCode
Case Keys.Back
'' handle the backspace key
If TB.Text.Length = 0 Then
If TB IsNot GroupTextBoxes.First Then
Dim prevTB As TextBox = GroupTextBoxes(Array.IndexOf(GroupTextBoxes, TB) - 1)
prevTB.Select()
End If
End If
Case Else
'' other keys
If TB.Text.Length = TB.MaxLength AndAlso TB.SelectionStart = TB.MaxLength Then
If TB IsNot GroupTextBoxes.Last Then
Dim nextTB As TextBox = GroupTextBoxes(Array.IndexOf(GroupTextBoxes, TB) + 1)
nextTB.Select()
End If
End If
End Select
End Sub
Re: Backspace to move back in Textbox
Quote:
Originally Posted by
Pradeep1210
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.
Re: Backspace to move back in Textbox
Quote:
Originally Posted by
jmcilhinney
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:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'' attach the KeyUp event handler
For Each tb In Panel1.Controls
AddHandler CType(tb, TextBox).KeyUp, AddressOf GroupTextBoxes_KeyUp
Next
End Sub
Private Sub GroupTextBoxes_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
Dim TB As TextBox = CType(sender, TextBox)
Select Case e.KeyCode
Case Keys.Back
'' handle the backspace key
If TB.Text.Length = 0 Then
Panel1.SelectNextControl(TB, False, True, False, False)
End If
Case Else
'' other keys
If TB.Text.Length = TB.MaxLength AndAlso TB.SelectionStart = TB.MaxLength Then
Panel1.SelectNextControl(TB, True, True, False, False)
End If
End Select
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.
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.
1 Attachment(s)
Re: Backspace to move back in Textbox
Quote:
Originally Posted by
Taem
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.
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.