Question use textbox Instead of using MaskedTextBox

kazemfallahi1371

New member
Joined
Apr 27, 2012
Messages
2
Programming Experience
Beginner
Hello
I wrote a program that takes as input the programyou want to be controlled
1. How can I get the code, only numbers
2. You name just a string
3. Your destination phone number with dashes
I Live Help
I do not want to use the MaskedTextBox


 
1. Numeric Text Box
2. That's fairly obvious, given that the Text of a TextBox is a String.
3. License Key / Serial Code TextBox That is not exactly what you want but it should give you an idea of how to proceed. You might want to combine it with #1.
 
VB.NET:
Private Sub txtNumber_KeyPress(~~~) Handles txtNumber.KeyPress
        If Not Char.IsDigit(e.KeyChar) Then e.Handled = True    'numbers only
        If e.KeyChar = Chr(8) Then e.Handled = False 	'allow Backspace
        If e.KeyChar = "." And txtNumber.Text.IndexOf(".") = -1 Then e.Handled = False 	'allow single decimal point 
        If e.KeyChar = Chr(13) then txtLetter.Focus()	'Enter key moves to next textbox
    End Sub

    Private Sub txtLetter_KeyPress(~~~) Handles txtLetter.KeyPress
        If Not Char.IsLetter(e.KeyChar) Then e.Handled = True    'letters only
        If e.KeyChar = Chr(8) Then e.Handled = False 'allow Backspace
    End Sub
 
VB.NET:
Private Sub txtNumber_KeyPress(~~~) Handles txtNumber.KeyPress
        If Not Char.IsDigit(e.KeyChar) Then e.Handled = True    'numbers only
        If e.KeyChar = Chr(8) Then e.Handled = False 	'allow Backspace
        If e.KeyChar = "." And txtNumber.Text.IndexOf(".") = -1 Then e.Handled = False 	'allow single decimal point 
        If e.KeyChar = Chr(13) then txtLetter.Focus()	'Enter key moves to next textbox
    End Sub

    Private Sub txtLetter_KeyPress(~~~) Handles txtLetter.KeyPress
        If Not Char.IsLetter(e.KeyChar) Then e.Handled = True    'letters only
        If e.KeyChar = Chr(8) Then e.Handled = False 'allow Backspace
    End Sub

The most obvious flaw with handling KeyPress alone is that it won't handle pasted text, so you would have to prevent pasting to ensure that invalid text wasn't entered.
 
Instead of the KeyPressed event, handle the TextChanged event, which does handle pasting. It's slower to process the whole text box but it should not matter for small amounts of data. Of course using the proper component is much better... Why do you not want to use the MaskedTextBox?
 
Back
Top