Results 1 to 15 of 15

Thread: Please help a Newbie!

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2000
    Posts
    2

    Question

    Hi everybody!

    I'm a VB-beginner and nee help.
    How can I define an Input-Field, where the User can only write numbers and noch letters or other Signs.

    Ps.: Sorry for my bad english.. I'm from Germany

  2. #2
    Fanatic Member wildcat_2000's Avatar
    Join Date
    Nov 2000
    Location
    Italy
    Posts
    727
    Are you talking about an input box or a textbox on a form? If you're referring to a textbox, just put the property of the textbox values to numerical (you can do so in the properties windows, when you select the textbox).

    When your car breaks down,
    close all windows and retry

    => please rate all users posts! <=

  3. #3
    Fanatic Member VBKNIGHT's Avatar
    Join Date
    Oct 2000
    Location
    Port25
    Posts
    619

    Wink :):):)


    or you can validate it using Isnumeric() function. :P

    If a post has helped you then Please Rate it!

  4. #4

    Thread Starter
    New Member
    Join Date
    Nov 2000
    Posts
    2

    Unhappy

    A Textbox.. where can I do that!?!?!?!?

  5. #5
    Fanatic Member wildcat_2000's Avatar
    Join Date
    Nov 2000
    Location
    Italy
    Posts
    727
    Well, when you build your form you can insert a textbox, by clicking on the left panel on textbox and then dragging it over your form. Is this what you are asking ?!?

    VBKinght yep you're right, IsNumerical() does work fine, you should however then set the KeyPreview = True on the form itself and use the function to filter the keyboard input, so that it just doesn't print other text ;0
    When your car breaks down,
    close all windows and retry

    => please rate all users posts! <=

  6. #6
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238

    Thumbs up ...

    Hey! guy, if you're talking about the textbox control, I've another 2 method as show below. The first is a bit more complicated as compare to the second.

    Code:
    'Method 1
    Option Explicit
    Private Declare Function GetWindowLong& Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long)
    Private Declare Function SetWindowLong& Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long)
    Private Const ES_NUMBER = &H2000&
    Private Const GWL_STYLE = (-16)
    Private Sub Text1_Change()
        Dim tmpValue&
        Dim fAlignment&
        Dim ret&
        fAlignment& = ES_NUMBER
        tmpValue& = GetWindowLong&(Text1.hwnd, GWL_STYLE)
        ret& = SetWindowLong&(Text1.hwnd, GWL_STYLE, tmpValue& Or fAlignment&)
        Text1.Refresh
    End Sub
    Code:
    'Method 2
    Private Sub Text1_KeyPress(KeyAscii As Integer)
    If (KeyAscii < 47 Or KeyAscii > 58) And KeyAscii <> 8 Then KeyAscii = 0
    End Sub

  7. #7
    Fanatic Member wildcat_2000's Avatar
    Join Date
    Nov 2000
    Location
    Italy
    Posts
    727
    Hi Chris,

    yes your 2nd option is the one I've suggested here below, still thanks for the code it's true - it does make things more clear to persons

    I still believe you have to set the From KeyPreview property to True for this to work properly.

    Kind regards,

    W.
    When your car breaks down,
    close all windows and retry

    => please rate all users posts! <=

  8. #8
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238

    KeyPreview = false

    Hi! Wildcat, I juz execute the code without set the KeyPreview properties to true and yet it still return the expected result too.

    Originally posted by wildcat_2000
    Hi Chris,

    yes your 2nd option is the one I've suggested here below, still thanks for the code it's true - it does make things more clear to persons

    I still believe you have to set the From KeyPreview property to True for this to work properly.

    Kind regards,

    W.

  9. #9
    Fanatic Member wildcat_2000's Avatar
    Join Date
    Nov 2000
    Location
    Italy
    Posts
    727
    Woops! Does it? I'll better shut up then hehe

    Cheers,

    W.
    When your car breaks down,
    close all windows and retry

    => please rate all users posts! <=

  10. #10
    New Member
    Join Date
    Nov 2000
    Posts
    14

    dunno whether this might help

    what about a mask input box? you can specify the location of your numbers and special characters using a mask input box.

  11. #11
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    <?>

    It's called a Masked Edit Control, referred to in Project/Components/Microsoft Masked Edit Control, and it it's own properties as MaskedBox1. You can set a mask, and the only thing to remember is that when you clear a maskeditbox you must first clear the mask then clear the text and then reset the mask. With that in mind it makes a very nice option.
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  12. #12
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238

    MaskEdit

    Will you think using MaskEdit control in this case is much more complecated?

  13. #13
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    <?>

    No..you just have to know the little quirk I mentioned above.

    MaskEdBox1.Mask = "##########" 'however long your field

    then all you can enter is numbers and backspace. No other code required.

    To clear the text.

    MaskEdBox1.Mask = ""
    MaskEdBox1.Text = ""
    MaskEdBox1.Mask = "##########" 'however long your field

    'if you wanted numbers seperated by / then
    MaskEdBox1.Mask = "###/###/####" 'as per phone number
    'then the / would remain in the box for guidance
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  14. #14
    Lively Member
    Join Date
    Nov 2000
    Location
    Guadalajara, Jalisco, Mex
    Posts
    105

    Answer

    You can add this code on the OnKeyPress event of any TextBox:

    If Not IsNumeric(Chr(KeyAscii)) And KeyAscii <> vbKeyBack _
    And Chr(KeyAscii) <> "." Then

    KeyAscii = 0

    End If

  15. #15
    Hyperactive Member marnitzg's Avatar
    Join Date
    Oct 2000
    Location
    South Africa
    Posts
    372

    Re: Answer

    fnajar that would disable keys like enter and tab. use this rather:
    Code:
    If Not IsNumeric(Chr(KeyAscii)) And KeyAscii > 31 Then
      KeyAscii = 0
    End If
    If you want to enable spaces change 31 to 32

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