473,399 Members | 4,192 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

text box validation

Lal
Hi,

Any method to validate on the text box; the entering data is numeric or
text
I want to type only numeric data on the text box


Regards
K R Lal
Nov 21 '05 #1
6 2991
Run following function in _TextChanged event.
Event:
------
Private Sub TextBox1_KeyPress(ByVal sender As System.Object, ByVal e As
System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress

TextBoxNumericOnly(Me.TextBox1, e)

End Sub
Function:
---------
Private Sub TextBoxNumericOnly(ByRef objTextBox As
System.Windows.Forms.TextBox, ByRef e As
System.Windows.Forms.KeyPressEventArgs, Optional ByVal blnAllowNegative As
Boolean = False, Optional ByVal blnAllowDecimal As Boolean = False)

Try
With objTextBox
e.Handled = True
Select Case Convert.ToInt32(e.KeyChar)
Case 48 To 57, 8, 13 ' 0 to 9, backspace, enter, delete.
e.Handled = False
Case 45 '-'.
REM You may only add 1 '-' char and only if it it
the first.
If blnAllowNegative Then
If (.Text.Length - .SelectedText.Length) = 0
Then
If .Text.LastIndexOf("-"c) = -1 Then
e.Handled = False
End If
End If
End If
Case 46 '.'.
REM You may only add 1 '.' char.
If blnAllowDecimal Then
If .Text.LastIndexOf("."c) = -1 Then
e.Handled = False
End If
End If
End Select
End With
Catch Ex As Exception
REM Handle exception.
End Try

End Sub
Nov 21 '05 #2
Qwert,

A nice very extended function. I assume it is working. However some remarks
to make it better. First to do it completly right you have to do something
for as people are pasting in a value. That can be in an event as validating.

And than beneath in the code a little change to make it more general.
Private Sub TextBox1_KeyPress(ByVal sender As System.Object, ByVal e As
System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress

TextBoxNumericOnly(Me.TextBox1, e)

TextBoxNumericOnly(DirectCast(sender, TextBox),e)

I hope this gives some ideas

Cor
Nov 21 '05 #3
"Lal" <la**@yahoo.com> schrieb:
Any method to validate on the text box; the entering data is numeric or
text
I want to type only numeric data on the text box


Place an ErrorProvider component on the form and add this code:

\\\
Imports System.ComponentModel
..
..
..
Private Sub TextBox1_Validating( _
ByVal sender As Object, _
ByVal e As CancelEventArgs _
) Handles TextBox1.Validating
Dim SourceControl As TextBox = DirectCast(sender, TextBox)
Dim ErrorText As String
Try
Dim i As Integer = Integer.Parse(SourceControl.Text)
Catch
ErrorText = "Value must be an integer."
Finally
Me.ErrorProvider1.SetError( _
SourceControl, _
ErrorText _
)
End Try
End Sub
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>
Nov 21 '05 #4
Lal
Thanks Qwert

it is working fine
"Qwert" <no**@nosp.com> wrote in message
news:zp********************@casema.nl...
Run following function in _TextChanged event.
Event:
------
Private Sub TextBox1_KeyPress(ByVal sender As System.Object, ByVal e As
System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress

TextBoxNumericOnly(Me.TextBox1, e)

End Sub
Function:
---------
Private Sub TextBoxNumericOnly(ByRef objTextBox As
System.Windows.Forms.TextBox, ByRef e As
System.Windows.Forms.KeyPressEventArgs, Optional ByVal blnAllowNegative As
Boolean = False, Optional ByVal blnAllowDecimal As Boolean = False)

Try
With objTextBox
e.Handled = True
Select Case Convert.ToInt32(e.KeyChar)
Case 48 To 57, 8, 13 ' 0 to 9, backspace, enter, delete. e.Handled = False
Case 45 '-'.
REM You may only add 1 '-' char and only if it it
the first.
If blnAllowNegative Then
If (.Text.Length - .SelectedText.Length) = 0
Then
If .Text.LastIndexOf("-"c) = -1 Then
e.Handled = False
End If
End If
End If
Case 46 '.'.
REM You may only add 1 '.' char.
If blnAllowDecimal Then
If .Text.LastIndexOf("."c) = -1 Then
e.Handled = False
End If
End If
End Select
End With
Catch Ex As Exception
REM Handle exception.
End Try

End Sub

Nov 21 '05 #5
J L
This is asked a lot. Check out this article and the associated code

http://msdn.microsoft.com/library/de...et04082003.asp

You can use his validator directly or (as I did) modify it to meet
your behavioral requirements. It also incorporates the error provider
interface. Once you have this tool in your toolbox, text validation is
solved for quite simply!

John

On Fri, 18 Mar 2005 11:59:51 +0530, "Lal" <la**@yahoo.com> wrote:
Hi,

Any method to validate on the text box; the entering data is numeric or
text
I want to type only numeric data on the text box


Regards
K R Lal


Nov 21 '05 #6
John,

However Quert has a solution I did not see until now.
He has as complet possible done the problem.

Cor
Nov 21 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Otto Krüse | last post by:
Hi everyone, I'm building a GUI in which I want, amongst other things, for people to fill in there postal code. The postal codes of my country (Holland) are in this format: 1234 AB So for the...
1
by: cga1982 | last post by:
I have a text box (for a password) with a validation rule that checks this password against another text box on the form and either lets you continue or tells you that the password is incorrect and...
4
by: Fraggle | last post by:
Hi, I want to validate a text box, The user can leave it blank, or fill it in. If they fill it in then it must be a date within a certain range. How can I achieve this? Thank you very much
6
by: The Eeediot | last post by:
Hello, Folks... I'm almost becoming a regular to this newsgroup. I am trying to display the contents of an MS-SQL Text field to a TextBox in ASPdotNET. The text in this field contains all...
1
by: Stephen Adam | last post by:
Hi there, I have written a custom validation control which checks to see of an input field is not empty and contains only numeric data. I was using a regular expression validation control but...
1
by: Kris | last post by:
Hi, I have a DataGrid where in each row has couple of text boxes and an update button. Each row is dynamically generated as the number of rows are not known ahead of time. When the user clicks the...
1
by: Joel Barsotti | last post by:
Is there anything builtin to ASP.net that allows you to tie a text box to a button so when you press enter in the text box it emulates clicking a near by button. I've coded up some client side...
9
by: sellcraig | last post by:
Microsoft access 2 tables table "data main" contains a field called "code" table "ddw1" is created from a make table query of "data main" Goal- the data in "code" field in needs to...
3
by: den | last post by:
inner a text element if I want to allow the insertion of only: alphabet's letters a,b,c,.... and A,B,C,... number and this - and this _ and not want space blank and others characters what is...
2
by: devnew | last post by:
hi i am new to tkinter and would like some help with canvas i am doing validation of contents of a folder and need to show the ok/error messages on a canvas resultdisplay =Canvas(...)...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.