Results 1 to 6 of 6

Thread: Canceling lose focus on Textbox

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Nov 2002
    Location
    Newcastle, Australia
    Posts
    20

    Canceling lose focus on Textbox

    Is there anyway from the TextBox's LostFocus function to cancel it so the user is stuck in focus.

    The reason I ask is that I want to validate the string in the textbox is a certain length. Currently when a user puts in a string longer than the allowed length a Messagebox displays and then the textbox is filled with a default value.

    I'd like the user to be returned to the editing mode in the textbox with their original entered string so they can delete/edit it straight away.

    Thanks

  2. #2
    Lively Member
    Join Date
    Apr 2005
    Posts
    68

    Re: Canceling lose focus on Textbox

    VB Code:
    1. Private Sub Text1_LostFocus()
    2.     Text1.SetFocus
    3. End Sub

  3. #3
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Canceling lose focus on Textbox

    Quote Originally Posted by microalps
    VB Code:
    1. Private Sub Text1_LostFocus()
    2.     Text1.SetFocus
    3. End Sub

    That wouldn't let you leave the textbox under any condition.
    You can use the maxlength property of the textbox, and it will just beep and not let any more text in.

    VB Code:
    1. Private sub text1_Validate()
    2.   if len(text1.text) <> 6 then
    3.     text1.setfocus
    4.   endif
    5. end sub
    Last edited by dglienna; May 3rd, 2005 at 11:53 PM.

  4. #4
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: Canceling lose focus on Textbox

    If you use the Validate event (which is the right thing to do), make sure the other controls on the form all have their CausesValidation property set to True. Going to a control that doesn't have it set to True will bypass the Validate event.

  5. #5
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: Canceling lose focus on Textbox

    My variation....
    VB Code:
    1. Private Sub TextBox_Validate(Cancel As Boolean)
    2.     If Len(TextBox) <> 6 Then
    3.         Cancel = True
    4.     End If
    5. End Sub
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  6. #6
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: Canceling lose focus on Textbox

    I just noticed that you said...
    Quote Originally Posted by gommo
    ...Currently when a user puts in a string longer than the allowed length a Messagebox displays and then the textbox is filled with a default value....
    If less than the max length is OK then all you need to do is to set the MaxLength property of the textbox. You might want to do that anyhow so you don't need to worry about validating the high end.

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