CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Hybrid View

  1. #1
    Join Date
    May 2012
    Posts
    1

    Exclamation limit amout of user input

    how i want to set text box with user only can insert value from 0 until 50 and if user insert more than 50 message box will appear using vb6

  2. #2
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: limit amout of user input

    Look up Masked Edit Control. It can accept valid ranges.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    Re: limit amout of user input

    Or you can put in some code in either the Change() event or the Validate() event.
    The change event fires for each character the user enters, the validate event will fire before the TextBox looses focus. If using the validate you would go
    Code:
    Private Sub Text1_Validate(Cancel As Boolean)
      If Val(Text1.Text) > MaxVal Then
         MsgBox "Value exceeds maximum!"
         Cancel = True
      End If
    End Sub
    Setting Cancel to true will make the focus stay in Text1 to allow for editing the value.
    The Validate() fires when the user attempts to go to another TextBox or Button, to continue his input. That means the user cannot leave the TextBox until the value is approved.

    You could put a similar code (without the Cancel, though) int the Change() event, but this has some disadvantages.
    Code:
    Private Sub Text1_Change()
      If Val(Text1.Text) > MaxVal Then
         MsgBox "Value exceeds maximum!"
      End If
    End Sub
    This fires the messagebox as soon as the entered value exceeds MaxVal, but after clicking away the messagebox the user can gladly continue going on to another input field, leaving the wrong value where it is.

  4. #4
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: limit amout of user input

    You can also set the maxlength property =2 to stop the box from allowing more than 2 characters.
    Always use [code][/code] tags when posting code.

  5. #5
    Join Date
    Aug 2003
    Location
    Sydney, Australia
    Posts
    1,900

    Re: limit amout of user input

    Put the test in the Text Lost Focus event rather than the Text Change event to avoid the test taking place each time you key a character

  6. #6
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    Re: limit amout of user input

    For that purpose VB objects provide the Validate() event (as I stated before), which happens BEFORE the LostFocus().
    The advantage is, if your validate routine finds the user input unsatisfying, it can set the Cancel parameter and AVOID the control to loose focus.

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

Featured