|
-
Oct 9th, 2002, 02:40 AM
#1
Thread Starter
Lively Member
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:
Private Sub Text1_LostFocus()
If Text1.DataChanged Then
MsgBox "Bla"
End If
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
-
Oct 9th, 2002, 02:43 AM
#2
-= B u g S l a y e r =-
I think what you need is to put the check into the validate event of the textbox.
-
Oct 9th, 2002, 02:51 AM
#3
Thread Starter
Lively Member
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...
-
Oct 9th, 2002, 03:41 AM
#4
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?
.
-
Oct 9th, 2002, 03:44 AM
#5
Thread Starter
Lively Member
Never heared of..
Reason...I've never heared of it...can somebody explain it?
-
Oct 9th, 2002, 03:56 AM
#6
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.
-
Oct 9th, 2002, 04:05 AM
#7
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:
Private Sub Text1_Validate(Cancel As Boolean)
If UCase(Text1.Text) = "CHRISSIE" Then
MsgBox "Correct!"
Cancel = False
Else
MsgBox "Wrong!!"
Cancel = True
End If
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.
-
Oct 9th, 2002, 04:06 AM
#8
Thread Starter
Lively Member
how to?
How to call the Validate function? Not like this..that doesn't work for me..
VB Code:
Private Sub Text1_Validate()
If Text1.DataChanged Then
MsgBox "Bla"
End If
End Sub
-
Oct 9th, 2002, 04:15 AM
#9
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.
.
-
Oct 9th, 2002, 04:19 AM
#10
Thread Starter
Lively Member
Thanks...
I forgot the Cancel as Boolean I suppose
-
Oct 9th, 2002, 04:31 AM
#11
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 
.
-
Oct 9th, 2002, 04:47 AM
#12
Thread Starter
Lively Member
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:
Private Sub menuItem_Click(Index As Integer)
ValidateControls
'code for the menu item...for example loading another form
Form2.Show
End sub
Private Sub txt1_Validate(Cancel as Boolean)
'Your check here
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|