can anyone help?
Printable View
can anyone help?
Ok there are a couple of ways you can do this, I need to find out which way you are wanting to go about this.
1. You can validate that only numbers have been entered into the text box upon a button, label, etc... action
orCode:if isnumeric(textbox1.text) = true then
'do something
else
msgbox "Please enter numbers only"
textbox1.focus
exit sub
end if
2. You can validate while the users is typing in the text box.
You can use the is_changed (i think) event of the textbox and find set it to accept certain keys only
something like:
I don't have VB in front on this computer so I don't know who accurate the syntax is, but I hope it is good enough for you to get the idea.Code:
if not keys.ascii = numeric1 then
msgbox "Please enter numbers only"
textbox1.focus
exit sub
end if
Ideal event for that is Keypress event . Here you go :
VB Code:
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress Dim KeyAscii As Integer KeyAscii = Asc(e.KeyChar) Select Case KeyAscii Case Asc("0") To Asc("9") 'acceptable keystrokes e.Handled = False Case Else e.Handled = True End Select End Sub
Hmmm, i worded it a bit wrong, i actually want to take all the text and leave just the numbers
I made my own textbox a while ago and it works perfectly for me... I don't feel like explaining it now though... see if you can figure it out:
and no need to explain I guess, that is a usercontrol :DVB Code:
Imports System.ComponentModel Public Class NumericTextBox Inherits Windows.Forms.TextBox Public Event InvalidKeyEntered(ByVal sender As NumericTextBox) Public Event InvalidTextEntered(ByVal sender As NumericTextBox) Public Event ValueOutOfRange(ByVal sender As NumericTextBox) Public Event ValueChanged(ByVal sender As NumericTextBox) Public Event KeyPressEvent(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) #Region " Properties " Private mAllowInvalidBeforeValidate As Boolean = False <Browsable(True), _ Description("If true, accepts pasting none-numeric text in the NumericTextBox, before " & _ "the Validate event is fired."), _ Category("Additional Setting"), _ DefaultValue(False)> _ Public Property AllowInvalidBeforeValidate() As Boolean Get Return mAllowInvalidBeforeValidate End Get Set(ByVal Value As Boolean) mAllowInvalidBeforeValidate = Value End Set End Property Private mAutoFormat As Boolean = True <Browsable(True), _ Description("If true, will format the entered value after the TextBox is validated. " & _ "Any extra zeros after floating point will be removed."), _ Category("Additional Setting"), _ DefaultValue(True)> _ Public Property AutoFormat() As Boolean Get Return mAutoFormat End Get Set(ByVal Value As Boolean) mAutoFormat = Value End Set End Property Private mAcceptDecimal As Boolean = False <Browsable(True), _ Description("If true, will accept decimal numbers."), _ Category("Additional Setting"), _ DefaultValue(False)> _ Public Property AcceptDecimal() As Boolean Get Return mAcceptDecimal End Get Set(ByVal Value As Boolean) mAcceptDecimal = Value End Set End Property Private mAcceptNegative As Boolean = False <Browsable(True), _ Description("If true, will accept negative numbers."), _ Category("Additional Setting"), _ DefaultValue(False)> _ Public Property AcceptNegative() As Boolean Get Return mAcceptNegative End Get Set(ByVal Value As Boolean) mAcceptNegative = Value End Set End Property Private mMinValue As Double = Double.MinValue <Browsable(True), _ Description("Minimum value that the NumericTextBox accepts."), _ Category("Additional Setting"), _ DefaultValue(Double.MinValue)> _ Public Property MinValue() As Double Get Return mMinValue End Get Set(ByVal Value As Double) mMinValue = Value End Set End Property Private mMaxValue As Double = Double.MaxValue <Browsable(True), _ Description("Maximum value that the NumericTextBox accepts."), _ Category("Additional Setting"), _ DefaultValue(Double.MaxValue)> _ Public Property MaxValue() As Double Get Return mMaxValue End Get Set(ByVal Value As Double) mMaxValue = Value End Set End Property Private mOnOutOfRangeSetToLim As Boolean = True <Browsable(True), _ Description("If the user enters a value larger than MaxValue, it will be set to" & _ "MaxValue. If a value smaller than MinValue is entered, it will be set" & _ " to MinValue. If False, restores the last valid value,"), _ Category("Additional Setting"), _ DefaultValue(True)> _ Public Property OnOutOfRangeSetToLim() As Boolean Get Return mOnOutOfRangeSetToLim End Get Set(ByVal Value As Boolean) mOnOutOfRangeSetToLim = Value End Set End Property #End Region Private mLastValidText As String Private Property lastValidText() As String Get If mLastValidText Is Nothing Then If MinValue > 0 Then mLastValidText = MinValue.ToString Else mLastValidText = "0" End If End If Return mLastValidText End Get Set(ByVal Value As String) Value = Value.Trim If Value = "" Then Value = lastValidText ElseIf CLng(mLastValidText) < MinValue Then mLastValidText = MinValue.ToString ElseIf CLng(mLastValidText) > MaxValue Then mLastValidText = MaxValue.ToString Else mLastValidText = Value End If End Set End Property Protected Overrides Sub OnTextChanged(ByVal e As System.EventArgs) If Not AllowInvalidBeforeValidate Then checkText() ' LastValidText = Me.Text End If MyBase.OnTextChanged(e) End Sub Protected Overrides Sub OnKeyPress(ByVal e As System.Windows.Forms.KeyPressEventArgs) Dim BACK_SPACE As Integer = 8 Dim ENTER As Integer = 13 Dim KeyASCII As Integer = Asc(e.KeyChar) 'Not a number If KeyASCII <> BACK_SPACE AndAlso KeyASCII <> ENTER AndAlso _ KeyASCII < 48 OrElse KeyASCII > 57 Then ' Accept decimal character, and the negative sign if set to accept them If Not (AcceptNegative AndAlso e.KeyChar = "-"c) AndAlso _ Not (AcceptDecimal AndAlso e.KeyChar = "."c) Then ' Cancel the entered key, not a valid key e.Handled = True RaiseEvent InvalidKeyEntered(Me) End If ElseIf KeyASCII = ENTER Then OnValidated(Nothing) e.Handled = True Me.SelectAll() End If 'RaiseEvent KeyPressEvent(Me, e) MyBase.OnKeyPress(e) End Sub ' Checks to see if the entered text is a number Private Sub checkText() Me.Text = Me.Text.Trim If Me.Text = "" Then Exit Sub Try Dim tmp As Double = CDbl(Me.Text) Catch Me.Text = lastValidText RaiseEvent InvalidTextEntered(Me) End Try End Sub ' Formats the entered number (has to be a number). For example, removes extra zeros Private Sub formatText() Try Dim tmp As Double = CDbl(Me.Text) Me.Text = tmp.ToString Catch MsgBox("DEBUG: WARNING! UNEXPECTED ERROR!", MsgBoxStyle.Critical, "ERROR!!!") End Try End Sub ' Changes to see if the entered number is in the valid range (has to be a number). ' Valid range is from MinValue to MaxValue Private Sub checkRange() Try Dim tmp As Double = CDbl(Me.Text) If tmp < MinValue Then If mOnOutOfRangeSetToLim Then Me.Text = MinValue.ToString Else Me.Text = lastValidText RaiseEvent ValueOutOfRange(Me) End If ElseIf tmp > MaxValue Then If mOnOutOfRangeSetToLim Then Me.Text = MaxValue.ToString Else Me.Text = lastValidText RaiseEvent ValueOutOfRange(Me) End If End If Catch MsgBox("DEBUG: WARNING! UNEXPECTED ERROR!", MsgBoxStyle.Critical, "ERROR!!!") End Try End Sub Protected Overrides Sub OnValidated(ByVal e As System.EventArgs) checkText() If Me.Text = "" Then Me.Text = lastValidText End If checkRange() If AutoFormat Then formatText() lastValidText = Me.Text RaiseEvent ValueChanged(Me) MyBase.OnValidated(e) End Sub Public Sub New() ' Since lastValidText is not set yet, the property will generate a ' default value. See the lastValidText property Me.Text = mLastValidText End Sub End Class
hope this helps a little , it will make a textbox only accept numerics.Code:Private Const GWL_STYLE As Integer = CInt(-16)
Private Const ES_NUMBER As Integer = CInt(&H2000)
Private txtstyle As Integer
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Integer, ByVal nIndex As Integer) As Integer
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Integer, ByVal nIndex As Integer, ByVal dwNewLong As Integer) As Integer
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
txtstyle = GetWindowLong(TextBox2.Handle.ToInt32, GWL_STYLE)
'/// set textbox to only accept numbers.
SetWindowLong(TextBox2.Handle.ToInt32, GWL_STYLE, txtstyle Or ES_NUMBER)
End Sub
Private Sub TextBox2_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox2.KeyPress
Select Case Asc(e.KeyChar)
Case 22
e.Handled = True '/// prevent pasting letters in to textbox.
Case Else
e.Handled = False
End Select
End Sub
Private Sub TextBox2_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged
If Not IsNumeric(TextBox2.Text) Then
MessageBox.Show("Sorry only numerics allowed!", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information)
TextBox2.Text = "" '/// make sure no letters are allowed in.
End If
End Sub
hmm what does the API do? sounds like you're filtering the entered keys in the KeyPress event :confused:
the api sets the textbox's window style to numbers only
here's a quote from msdn library :
the keypress / text changed handlers are to prevent the pasting of none numeric chars.Quote:
ES_NUMBER
Allows only digits to be entered into the edit control. Note that, even with this set, it is still possible to paste non-digits into the edit control.
To change this style after the control has been created, use SetWindowLong.
oooh ok so it doesnt let you enter non-numeric keys.... got it... my textbox is better:D it rules:DQuote:
Originally posted by dynamic_sysop
the api sets the textbox's window style to numbers only
here's a quote from msdn library :
the keypress / text changed handlers are to prevent the pasting of none numeric chars.
what if you have to enter a decimal or a negative number?
:O
thats one complicated text box...
what im doing is pasteing someting, and want to get only the nubmers in it
here:
VB Code:
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged TextBox1.Text = filterText(TextBox1.Text) End Sub Private Function filterText(ByVal txt As String) As String If txt Is Nothing Then Return Nothing Const numbers As String = "1234567890" Dim filtered As String = "" Dim i As Integer Dim aChar As String For i = 0 To txt.Length - 1 aChar = txt.Substring(i, 1) ' See if this character is a number If numbers.IndexOf(aChar) <> -1 Then filtered &= aChar End If Next Return filtered End Function