User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the VB.NET section within the Software Development category of DaniWeb, a massive community of 429,818 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,432 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our VB.NET advertiser: Programming Forums
Views: 3751 | Replies: 7
Reply
Join Date: Feb 2008
Posts: 3
Reputation: Wilderness Bob is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Wilderness Bob Wilderness Bob is offline Offline
Newbie Poster

Limit Text box input

  #1  
Feb 28th, 2008
I need to be able to limit the text entered into a text box, I need to make it so that only whole numbers can be entered and not decimal places. I have already limited it to just Numbers but need help with this. I think checking a box to see if a fullstop is in it would be a good way but i dont know the command for searching a string.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jan 2008
Posts: 43
Reputation: Alekhan is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 4
Alekhan Alekhan is offline Offline
Light Poster

Re: Limit Text box input

  #2  
Feb 28th, 2008
to limit text box set its property "MaxLength" to number you want to enter like to 3 or 6.
to check decimal put condition
somthing like this...
if textbox1.text=Asc(.) then
msgbox("Please dont enter Decimal Point, Thanks")
endif
Reply With Quote  
Join Date: Jan 2008
Posts: 43
Reputation: Alekhan is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 4
Alekhan Alekhan is offline Offline
Light Poster

Re: Limit Text box input

  #3  
Feb 28th, 2008
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'Dim dt As Integer = Asc(Me.TextBox1.Text)
If TextBox1.Text = Chr(46) Then
MsgBox("You pressed dot")
End If
End Sub


this is one way you can check whether the dot is pressed or not
Reply With Quote  
Join Date: Nov 2007
Location: � Jogja �
Posts: 2,585
Reputation: Jx_Man is a glorious beacon of light Jx_Man is a glorious beacon of light Jx_Man is a glorious beacon of light Jx_Man is a glorious beacon of light Jx_Man is a glorious beacon of light Jx_Man is a glorious beacon of light 
Rep Power: 11
Solved Threads: 235
Jx_Man's Avatar
Jx_Man Jx_Man is offline Offline
Posting Maven

Re: Limit Text box input

  #4  
Feb 29th, 2008
try this following code, this code only allowed number entered on textbox :
  1. Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
  2. If (Microsoft.VisualBasic.Asc(e.KeyChar) < 48) _
  3. Or (Microsoft.VisualBasic.Asc(e.KeyChar) > 57) Then
  4. e.Handled = True
  5. End If
  6. If (Microsoft.VisualBasic.Asc(e.KeyChar) = 8) Then
  7. e.Handled = False
  8. End If
  9. end sub
Never tried = Never Know
So, Please do something before post your thread.
* PM Asking will be ignored *
Reply With Quote  
Join Date: Jan 2008
Posts: 24
Reputation: suganzeni is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
suganzeni suganzeni is offline Offline
Newbie Poster

Re: Limit Text box input

  #5  
Mar 4th, 2008
Hi
Try the following code So that you can enter only the numbers not even any special characters.

try it in the textbox keypress event

Private Sub textbox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles tsum.KeyPress
If Asc(e.KeyChar) = 8 Then
Exit Sub
End If
If Asc(e.KeyChar) = 44 Then
e.Handled = True
End If

If Asc(e.KeyChar) < 48 Then
e.Handled = True
ElseIf Asc(e.KeyChar) > 57 Then
e.Handled = True
End If
End Sub
Reply With Quote  
Join Date: Jan 2008
Posts: 65
Reputation: SolTec is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 4
SolTec SolTec is offline Offline
Junior Poster in Training

Re: Limit Text box input

  #6  
Mar 5th, 2008
Originally Posted by Wilderness Bob View Post
I need to be able to limit the text entered into a text box, I need to make it so that only whole numbers can be entered and not decimal places. I have already limited it to just Numbers but need help with this. I think checking a box to see if a fullstop is in it would be a good way but i dont know the command for searching a string.


You could also use the IsNumeric to insure all numbers and then check for the (.) as the other answer stated.

The MaxLength function will limit the number of characters.

You could also use code to check on keydown event if the character being enter is a number or period. If the period is captured, trigger a messagebox event and remove the period and reset the focus at the end of the numeric string.
Reply With Quote  
Join Date: Nov 2007
Posts: 2
Reputation: samichou6 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
samichou6 samichou6 is offline Offline
Newbie Poster

Re: Limit Text box input

  #7  
Mar 5th, 2008
just try this if will help you find if you type dot in the textbox
If TextBox1.Text.Contains(".") Then
MsgBox("true")
Else
MsgBox("true")
End If
Reply With Quote  
Join Date: Jan 2008
Posts: 65
Reputation: SolTec is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 4
SolTec SolTec is offline Offline
Junior Poster in Training

Re: Limit Text box input

  #8  
Mar 5th, 2008
Originally Posted by suganzeni View Post
Hi
Try the following code So that you can enter only the numbers not even any special characters.

try it in the textbox keypress event

Private Sub textbox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles tsum.KeyPress
If Asc(e.KeyChar) = 8 Then
Exit Sub
End If
If Asc(e.KeyChar) = 44 Then
e.Handled = True
End If

If Asc(e.KeyChar) < 48 Then
e.Handled = True
ElseIf Asc(e.KeyChar) > 57 Then
e.Handled = True
End If
End Sub


If IsNumeric(TextBox1.Text) And TextBox1.Text.Contains(".") Then
MsgBox("True for .")
Else
MsgBox("False for .")
End If
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb VB.NET Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the VB.NET Forum

All times are GMT -4. The time now is 5:29 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC