Results 1 to 12 of 12

Thread: Lostfocus check before menu change

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2002
    Location
    The Netherlands
    Posts
    96

    Lostfocus check before menu change

    Me again here...

    I have a small problem...
    I have some textboxes some are checked like the code below...

    VB Code:
    1. Private Sub Text1_LostFocus()
    2.     If Text1.DataChanged Then
    3.         MsgBox "Bla"
    4.     End If
    5. End Sub

    I also have a menu bar which has some actions which loads another form. Problem is I really need the MsgBox when the data is Changed as I need some Yes/No from it to make some calculation.

    I don't want Form2 to appear before checked for the Text Change...it can only appear after the text has been checked for a change. Got it? Thanks

  2. #2
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    I think what you need is to put the check into the validate event of the textbox.

    -= a peet post =-

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Aug 2002
    Location
    The Netherlands
    Posts
    96

    That's impossible...

    How would I ever to be able to set...my lostfocus is checking for a lot of things...

    But when I go to the next form by clicking on another menu item it won't check the lostfocus before it loads the new form..that's all

    Note.
    When I use a CommandButton...it checks the lostfocus first....I want to have this to happen when I click a menu-item aswell...

  4. #4
    Randalf the Red honeybee's Avatar
    Join Date
    Jun 2000
    Location
    off others' brains
    Posts
    4,345

    Well ...

    LostFocus may not be fired in quite a few cases, for e.g. pressing a hotkey combination to "click" a button on a form. Therefore if you want to have some code run everytime a textbox or another control loses focus, it's better to place it in the Validate event. What are your reasons for not using the Validate event?

    .
    I am not a complete idiot. Some parts are still missing.
    Check out the rtf-help tutorial
    General VB Faq Thread
    Change is the only constant thing. I have not changed my signature in a long while and now it has started to stink!
    Get more power for your floppy disks. ; View honeybee's Elite Club:
    Use meaningfull thread titles. And add "[Resolved]" in the thread title when you have got a satisfactory response.
    And if that response was mine, please think about giving me a rep. I like to collect them!

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Aug 2002
    Location
    The Netherlands
    Posts
    96

    Never heared of..

    Reason...I've never heared of it...can somebody explain it?

  6. #6
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974
    it's basically the same as lostfocus, but will also be called when menu's etc get the focus, in fact any time the box is left.

  7. #7
    Randalf the Red honeybee's Avatar
    Join Date
    Jun 2000
    Location
    off others' brains
    Posts
    4,345

    Well ...

    Originally posted by Chrissie
    Reason...I've never heared of it...can somebody explain it?
    Firstly, you can use it only if you have VB6.0

    Secondly, unlike the LostFocus event, the Validate event for a control lets you decide if focus can move out of the control or the focus should stay in the control. For e.g. you could write code in the Validate event of a textbox to see if the user had entered the correct data, and if the data is not correct, you could set the focus back to the textbox. This way the user wouldn't be able to leave the textbox unless he entered the correct data.

    An example:

    VB Code:
    1. Private Sub Text1_Validate(Cancel As Boolean)
    2.     If UCase(Text1.Text) = "CHRISSIE" Then
    3.         MsgBox "Correct!"
    4.         Cancel = False
    5.     Else
    6.         MsgBox "Wrong!!"
    7.         Cancel = True
    8.     End If
    9. End Sub

    The above code will not allow you to leave the textbox unless you enter "Chrissie" into it. If you were to write this code in the LostFocus event, you could only show a messagebox saying "Wrong!!" to the user in case of wrong input, but you could not force the user to go back and correct the data in the textbox, which you can do with the Validate event.

    There are a few other things to note about this event, but you can either learn them yourself, or first familiarize yourself with the Validate event and then ask for those things.

    .
    Last edited by honeybee; Oct 9th, 2002 at 04:32 AM.
    I am not a complete idiot. Some parts are still missing.
    Check out the rtf-help tutorial
    General VB Faq Thread
    Change is the only constant thing. I have not changed my signature in a long while and now it has started to stink!
    Get more power for your floppy disks. ; View honeybee's Elite Club:
    Use meaningfull thread titles. And add "[Resolved]" in the thread title when you have got a satisfactory response.
    And if that response was mine, please think about giving me a rep. I like to collect them!

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Aug 2002
    Location
    The Netherlands
    Posts
    96

    how to?

    How to call the Validate function? Not like this..that doesn't work for me..
    VB Code:
    1. Private Sub Text1_Validate()
    2.     If Text1.DataChanged Then
    3.         MsgBox "Bla"
    4.     End If
    5. End Sub

  9. #9
    Randalf the Red honeybee's Avatar
    Join Date
    Jun 2000
    Location
    off others' brains
    Posts
    4,345

    Well ...

    The Validate() is an event which will be called automatically, just like LostFocus, when the user tries to leave the textbox and move to another control, maybe click a button.

    In order to write code in this event, in design mode of your form, double-click a textbox. When the code window appears, select the Validate event from the list of events at the top (just like you would write code in the Click or Change events). Then write the code in the event.

    .
    I am not a complete idiot. Some parts are still missing.
    Check out the rtf-help tutorial
    General VB Faq Thread
    Change is the only constant thing. I have not changed my signature in a long while and now it has started to stink!
    Get more power for your floppy disks. ; View honeybee's Elite Club:
    Use meaningfull thread titles. And add "[Resolved]" in the thread title when you have got a satisfactory response.
    And if that response was mine, please think about giving me a rep. I like to collect them!

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Aug 2002
    Location
    The Netherlands
    Posts
    96

    Thanks...

    I forgot the Cancel as Boolean I suppose

  11. #11
    Randalf the Red honeybee's Avatar
    Join Date
    Jun 2000
    Location
    off others' brains
    Posts
    4,345

    Well ...

    Originally posted by Chrissie
    I forgot the Cancel as Boolean I suppose
    I missed that too, I wrote it as in Integer . Actually that's from writing code in too many such events, CancelUpdate and all that.... I sort of lost track which Cancel was a Boolean and which was an Integer

    .
    I am not a complete idiot. Some parts are still missing.
    Check out the rtf-help tutorial
    General VB Faq Thread
    Change is the only constant thing. I have not changed my signature in a long while and now it has started to stink!
    Get more power for your floppy disks. ; View honeybee's Elite Club:
    Use meaningfull thread titles. And add "[Resolved]" in the thread title when you have got a satisfactory response.
    And if that response was mine, please think about giving me a rep. I like to collect them!

  12. #12

    Thread Starter
    Lively Member
    Join Date
    Aug 2002
    Location
    The Netherlands
    Posts
    96

    Thanks

    Thanks a lot..it's working

    If anyone ever has the same problem...use ValidateControls in your menu item Click code...
    Use
    VB Code:
    1. Private Sub menuItem_Click(Index As Integer)
    2. ValidateControls
    3. 'code for the menu item...for example loading another form
    4. Form2.Show
    5. End sub
    6.  
    7. Private Sub txt1_Validate(Cancel as Boolean)
    8. 'Your check here
    9. End sub

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