|
-
Apr 30th, 2006, 05:38 PM
#1
Thread Starter
New Member
Inputbox problem
I have been looking all over the place and I just can't seem to find the answer. The answer is yes I am a nub. When you call for an inputbox function
(ex. inputbox("Enter a number","Ladeda")
is there a way that you can limit the user input to only accept numbers and reject letters.
I would appriciate any help
~thanks
-
Apr 30th, 2006, 06:12 PM
#2
Member
Re: Inputbox problem
Hi,
only allow numbers and back,enter..
VB Code:
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
Dim c As Char
c = e.KeyChar
If Not (Char.IsDigit(c) Or Char.IsControl(c)) Then
e.Handled = True
End If
End Sub
It's only easy if you know!
VB.NET2003/1.1
VB2005/2.0
-
Apr 30th, 2006, 06:17 PM
#3
Re: Inputbox problem
What mrcrash posted will work, but not on an InputBox(). However, that is just one more reason to never use InputBox (actually, I have used it a few times myself, but only when I was being lazy). Basically, the InputBox() function allows you a simple way to get text data, but it is VERY limitted because there is very little customization available. Making your own form to get the information you want in the way you want it is a vastly more flexible solution.
Make a form with a textbox and an OK and Cancel button. Use mrcrash's code, or any of the many similar snippets posted in this forum, and you can have something that looks and feels like an InputBox, but does just what you need it to do.
My usual boring signature: Nothing
 
-
Apr 30th, 2006, 06:18 PM
#4
Re: Inputbox problem
Someone else asked this same question within hours of you. Are you two working on the same project or something? Anyway the answer is "No". You need to either create your own form, which I would recommend, or else do something like:
VB Code:
Dim input As String
Dim number As Integer
Do
input = InputBox("Please enter a whole number.")
If Integer.TryParse(input, number) Then
Exit Do
Else
MessageBox.Show("Yo have entered an invalid value.")
End If
Loop
-
Apr 30th, 2006, 06:50 PM
#5
Thread Starter
New Member
Re: Inputbox problem
Hey thanks for your help. I just couldn't get it to work. I'll use your idea jmc......
-
Apr 30th, 2006, 10:30 PM
#6
Re: Inputbox problem
Oooooog! Do you mean his code? All three of us prefered not using an InputBox. What JM posted will work (of course), but it is not very kind to the user. They don't know anything is wrong until it fails. If you used a custom form, you could give them quicker feedback.
However, if you really want to stick with the InputBox, then consider what shoudl be shown in the Inputbox. What JM posted will have the InputBox show up empty every time, which may well be what you want to have happen. However, You could also do it something like this:
input = InputBox("Please enter a whole number.","Enter Number", input)
This will cause whatever is in input to show up in the InputBox() when it is shown.
My usual boring signature: Nothing
 
-
May 1st, 2006, 09:39 AM
#7
Thread Starter
New Member
Re: Inputbox problem
Ya unfortuantly for my I had to use inputbox function on a project I was working on I would have used another way such as an textbox or another form. but i really didn't like the idea of not being able to limit the user input. But I had to use it. I also did need it in a loop which was kindof nice.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|