Results 1 to 7 of 7

Thread: Inputbox problem

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2006
    Posts
    3

    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

  2. #2
    Member
    Join Date
    Nov 2005
    Location
    Kent
    Posts
    40

    Re: Inputbox problem

    Hi,
    only allow numbers and back,enter..
    VB Code:
    1. Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
    2.         Dim c As Char
    3.         c = e.KeyChar
    4.         If Not (Char.IsDigit(c) Or Char.IsControl(c)) Then
    5.             e.Handled = True
    6.         End If
    7.     End Sub
    It's only easy if you know!
    VB.NET2003/1.1
    VB2005/2.0

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    35,242

    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

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,461

    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:
    1. Dim input As String
    2. Dim number As Integer
    3.  
    4. Do
    5.     input = InputBox("Please enter a whole number.")
    6.  
    7.     If Integer.TryParse(input, number) Then
    8.         Exit Do
    9.     Else
    10.         MessageBox.Show("Yo have entered an invalid value.")
    11.     End If
    12. Loop

  5. #5

    Thread Starter
    New Member
    Join Date
    Apr 2006
    Posts
    3

    Re: Inputbox problem

    Hey thanks for your help. I just couldn't get it to work. I'll use your idea jmc......

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    35,242

    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

  7. #7

    Thread Starter
    New Member
    Join Date
    Apr 2006
    Posts
    3

    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
  •  



Click Here to Expand Forum to Full Width