if textbox have numbers then ...

If you need help with a project or need to know how to do something specific in VB.NET then please ask your questions in here.
Forum rules
Please LOCK your topics once you have found the solution to your question so we know you no longer require help with your query.
5 posts Page 1 of 1
Contributors
User avatar
troop
Dedicated Member
Dedicated Member
Posts: 75
Joined: Sat Sep 28, 2013 11:57 am

if textbox have numbers then ...
troop
hi im making a program now im stuck
i wanna make a simple code to check if
textbox1.text has numbers and have
no letters then it will msgbox else it will
do nothing

i will make a fake code to make it easier

if textbox1.text have numbers and dont have letters or characters then
msgbox("yolo")

thnx :?
User avatar
noypikami
VIP - Donator
VIP - Donator
Posts: 151
Joined: Sat Dec 22, 2012 1:49 am

try this code:::
Code: Select all
 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim value As Integer

        Dim text As String = TextBox1.Text
        If Integer.TryParse(text, value) Then
            MsgBox(value.ToString)
        Else
            MsgBox("the value is not an integer")
        End If
    End Sub
User avatar
troop
Dedicated Member
Dedicated Member
Posts: 75
Joined: Sat Sep 28, 2013 11:57 am

Re: if textbox have numbers then ...
troop
noypikami wrote:
try this code:::
Code: Select all
 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim value As Integer

        Dim text As String = TextBox1.Text
        If Integer.TryParse(text, value) Then
            MsgBox(value.ToString)
        Else
            MsgBox("the value is not an integer")
        End If
    End Sub
thanks lol i didnt expect anyone to answer me this early, im going to school now when i come back i will tell you if it works
loove; loove; cooll;
User avatar
Shim
VIP - Donator
VIP - Donator
Posts: 882
Joined: Wed Dec 14, 2011 5:02 am

Re: if textbox have numbers then ...
Shim
Hi,

Try this

Function :
Code: Select all
 Private Function IsInputNumeric(input As String) As Boolean
        If String.IsNullOrWhiteSpace(input) Then Return False
        If IsNumeric(input) Then Return True
        Dim parts() As String = input.Split("/"c)
        If parts.Length <> 2 Then Return False
        Return IsNumeric(parts(0)) AndAlso IsNumeric(parts(1))
    End Function
Usage :
Code: Select all
If IsInputNumeric(TextBox1.Text) Then
            'handle numeric input
        Else
            'handle not a number
        End If
Source : http://stackoverflow.com/a/15423718/2443148
Find my programs on Softpedia
User avatar
comathi
Coding God
Coding God
Posts: 1242
Joined: Fri Mar 26, 2010 1:59 pm

Re: if textbox have numbers then ...
comathi
#Shim, don't forget to mention this code will also allow slashes ("/") ;)
5 posts Page 1 of 1
Return to “Coding Help & Support”