textbox

  • Thread starter Thread starter Steve
  • Start date Start date
S

Steve

I am new to vb.net. I have a textbox. I want the user to be able to only
enter numbers between 2 and 12 into the textbox. Is there a way to do this?
Thanks for any help.
 
Do you only want 2 digits? With CTRL + V (paste) will cause you a few more
problems

Why don't you use a spin button instead? That way you can specify min/max
values that the user can choose - problem solved
 
Steve,

You mean something as this typed in this message and not checked

\\\\
Private Sub TextBox1_Validating(ByVal sender _
As Object, ByVal e As System.ComponentModel.CancelEventArgs) _
Handles TextBox1.Validating
If Not Check12(TextBox1.Text) then messagebox.show ("The value is not
between 2 and 12")
End Sub
Private Function Check12(value) as boolean
If value > 0 Then
If IsNumeric(value) Then
If Cint(value) < 2 OrElse Cint(value) > 12 Then
return true
End if
end if
end if
End sub
///

I hope this helps a little bit?

Cor
 
Steve said:
I am new to vb.net. I have a textbox. I want the user to be able to only
enter numbers between 2 and 12 into the textbox.

\\\
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)
If i < 1 OrElse i > 12 Then
ErrorText = "Value must fall in the interval 1, ..., 12."
End If
Catch
ErrorText = "Value must be an integer."
End Try
Me.ErrorProvider1.SetError( _
SourceControl, _
ErrorText _
)
End Sub
///
 
If you do this
'the public sub define is not correct, when you double click on textbox and
go to code add the event onchange

Public sub TextBox_Change(byval sender, byval e)
try
try
if((microsoft.visualbasic.right(cint(e.text),1) <= 2)) and
((microsoft.visualbasic.right(cint(e.text),1) >= 9) then
'if you want to make sure that the user does not have 'a
number larger then 12
if((cint(e.text)< 12) and (cint(e.text)>=2))then
'number betweeen 2 and 12
else
'number not between 2 and 12
me.textbox.text = microsoft.visualbasic.left
(me.textbox.text,(LEN(me.textbox.text)-1))
end if
else

me.textbox.text = microsoft.visualbasic.left
(me.textbox.text,(LEN(me.textbox.text)-1))
end if
end if


catch
'number was not entered
me.textbox.text = microsoft.visualbasic.left
(me.textbox.text,(LEN(me.textbox.text)-1))
end try



catch ex as

end try

end sub
 
HerFried

If the user wants to allow only numbers between 2 and 12 why are you
checking for if i< 1? It should be If i<2
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top