Jump to content
Xtreme .Net Talk

Recommended Posts

Posted
I was wondering if there was a way to have a textbox only allow numbers and not letters or any other characters. For example if someone were to try and type a "t" in that textbox then nothing would enter into the box. if anyone knows a way of setting a property like this on a textbox please post.
Posted

Regular Expressions...

 

One way would be to use Regular Expressions. So, you would create a boolean NumericOnly method where you would check the contents of the TextBox against a Regex that might look something like this:

 

@"^[0-9]$"

 

public bool NumericOnly(string userText)
{
 string expr = @"^[0-9]$";
 Regex exprNumeric = new Regex(expr);

 return exprNumeric.IsMatch(userText);
}

"Never ascribe to malice that which can adequately be explained by incompetence." -- Napolean Bonaparte
  • *Experts*
Posted

Another way is to look at the keyboard entry ASCII values in the TextBoxes KeyPress Sub.

   'Positive Whole Numbers and Backspace Only
   Private Sub PositiveWholeNumbersOnly_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles _
   aPositiveNumberOnlyTextBox.KeyPress, anotherPositiveNumberOnlyTextBox.KeyPress

       Dim txtBox As TextBox = DirectCast(sender, TextBox)

       If AscW(e.KeyChar) >= 47 And AscW(e.KeyChar) < 58 Or AscW(e.KeyChar) = 8 Then
           If e.KeyChar = "0" And txtBox.Text = Nothing Or e.KeyChar = "0" And txtBox.SelectedText = txtBox.Text Then
               e.Handled = True
           Else : e.Handled = False
           End If
       Else : e.Handled = True
       End If
   End Sub

   'Positive Numbers, Dot and Backspace Only
   Private Sub PositiveNumbersDotOnly_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles _
   aPositiveDecimalNumberOnlyTextBox.KeyPress

       Dim txtBox As TextBox = DirectCast(sender, TextBox)

       If AscW(e.KeyChar) = 8 Then 'backspace
           e.Handled = False
           Exit Sub
       End If
       If Not AscW(e.KeyChar) < 46 And Not AscW(e.KeyChar) >= 58 Or AscW(e.KeyChar) = 8 Then
           If txtBox.Text.IndexOf(".") >= 0 Then
               If txtBox.SelectedText.Length >= 1 Then
                   e.Handled = False
               ElseIf e.KeyChar = "." Then
                   e.Handled = True
               End If
           End If
       Else : e.Handled = True
       End If
   End Sub

With a little more defination, you can set any numeric preference possible...like entries greater than 0 and less than 1.00, etc. You can also set the decimal symbol (in this case a ".") to the users System.Globalization.NumberFormatInfo, .CurrentInfo.NumberDecimalSeparator.

Member, in good standing, of the elite fraternity of mentally challenged programmers.

 

Dolphins Software

  • Leaders
Posted

you could always set the style of the box to ES_NUMBER , but you must handle the WM_PASTE message via a simple subclass , eg:

 


Private Const GWL_STYLE As Int32 = -16
Private Const ES_NUMBER As Int32 = &H2000I
Private Declare Function GetWindowLong Lib "user32.dll" Alias "GetWindowLongA" (ByVal hwnd As IntPtr, ByVal nIndex As Int32) As Int32
Private Declare Function SetWindowLong Lib "user32.dll" Alias "SetWindowLongA" (ByVal hwnd As IntPtr, ByVal nIndex As Int32, ByVal dwNewLong As Int32) As Int32

Private numbertext As NumericText

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

	'/// to handle the case of someone trying to paste none - numeric text ...
	numbertext = New NumericText(TextBox1.Handle)

	'/// set the style of the textbox to numeric only ( but unfortunatly it still accepts pasting of text , hence the subclassing )
	Dim style As Int32 = GetWindowLong(TextBox1.Handle, GWL_STYLE)

	If style > 0 Then
		style += ES_NUMBER
		SetWindowLong(TextBox1.Handle, GWL_STYLE, style)
	End If
End Sub

End Class

Public Class NumericText
Inherits NativeWindow
Private Const WM_PASTE As Int32 = &H302

Public Sub New(ByVal hwnd As IntPtr)
	MyBase.AssignHandle(hwnd)
End Sub

Protected Overrides Sub WndProc(ByRef m As Message)
	Select Case m.Msg
		Case WM_PASTE
			'/// prevent the pasting of none-numerics
		Case Else
			MyBase.WndProc(m)
	End Select

End Sub
End Class

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...