Results 1 to 5 of 5

Thread: use textbox Instead of using MaskedTextBox

  1. #1
    kazemfallahi1371 is offline VB.NET Forum Newbie
    .NET Framework
    .NET 4.0
    Join Date
    Apr 2012
    Posts
    2
    Reputation
    0

    Talking use textbox Instead of using MaskedTextBox

    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



  2. #2
    jmcilhinney's Avatar
    jmcilhinney is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Posts
    11,344
    Reputation
    1543
    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.

  3. #3
    Solitaire is offline VB.NET Forum Miyagee
    .NET Framework
    .NET 4.0
    Join Date
    Jun 2004
    Location
    New York
    Posts
    418
    Reputation
    162
    Code:
    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

  4. #4
    jmcilhinney's Avatar
    jmcilhinney is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Posts
    11,344
    Reputation
    1543
    Quote Originally Posted by Solitaire View Post
    Code:
    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.

  5. #5
    Herman is offline VB.NET Forum Miyagee
    .NET Framework
    .NET 4.0
    Join Date
    Oct 2011
    Location
    Montreal, QC, CA
    Posts
    448
    Reputation
    346
    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?

Bookmarks

Posting Permissions

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