Text Box Leave event and Button click Event
Hi all,
Can i Know how to catch between these 2 event, Leave and Click?
I have one checking in a textbox Leave event. I will go check whether this ID is already exist in DB or not. If exist, I will prompt MSG box to user.
When I click on Close button while focus is on textbox, this textbox Leave Event will trigger first before Button Click event.
So, the form remains open without trigger Button Click event.
Can I know how to prevent this textbox leave event or how can i catch/control this situation?
Thanks.
Re: Text Box Leave event and Button click Event
You should be handling the Validating event rather than the Leave event. That will allow you to prevent focus leaving if required by setting e.Cancel to True. It also means that you can set your Button's CausesValidation property to False and clicking it will not cause any Validating event to be raised on the previously focused control, which is exactly what you want.
Re: Text Box Leave event and Button click Event
Quote:
Originally Posted by
jmcilhinney
You should be handling the Validating event rather than the Leave event. That will allow you to prevent focus leaving if required by setting e.Cancel to True. It also means that you can set your Button's CausesValidation property to False and clicking it will not cause any Validating event to be raised on the previously focused control, which is exactly what you want.
Thanks jmcilhinney. :) :afrog:
Now I changed my checking method to textbox Validaing event, it works well :)
I tried to find this event and I still don't understand when it will fire and what is the meaning of this event.
Although I solved my case, I still want to know why and how it solved. Hope you don't mind my second request :) :D
And can you share me a link where I can find the flow of each event?
Here is what i found on msdn. its a list not the flow sequence.
http://msdn.microsoft.com/en-us/libr...ts(VS.80).aspx
Thanks again.
Re: Text Box Leave event and Button click Event
Hi,
It seemed solved but it still has problem. :(
Although I moved my checking from Leave event to Validating event of a textbox, it still go and check when I click Button.
I set my button's Cause Validating to False.
I do the checking in TextBox Validating event.
If I keyin some data in textBox and didn't go anywhere just directly click on Close button, it still go to Validating event which cause the message box to pop up if same ID is found in Database. And click event is not working too.
Any way to prevent this happen????
Thanks.
Re: Text Box Leave event and Button click Event
When the user tries to move focus from one control to another, first the system checks whether the control receiving focus causes validation. If not then the Leave event is raised. If it does then the Validating event is raised. If the control fails validation, e.g. a numeric field contains a non-numeric value, then e.Cancel is set to True so that the control will not lose focus and no further events are raised. If the control passes validation then the Validated event is raised, then Leave event is raised. You can also force the Validating and Validated events to be raised by calling ValidateChildren on the form. This allows you to validate controls that have never been visited and have therefore never raised a Leave event.
You should ALWAYS perform validation on the Validating event. Whether you do other things on the Validated or the Leave event depends on whether you actually want to perform the action each time the control is validated or each time it loses focus. Often they occur together but not always.
Re: Text Box Leave event and Button click Event
hi jmcilhinney!
thanks for your explanation and help.
I still confused :confused: (don't mind cuz i'm not that clever:bigyello: )
here is what I understand from your answers.
If I set my button's CauseValidating = False, it should not raise my textBox Validating event. (Is it correct???)
If the above is true, I think my textBox validating event should not raise when I click Button. :(
Now my problem is Although I set my Button CauseValidating to False, whenever I click on button, it will raise the Textbox's Validating event if focus is on TextBox before I click button.
May be what I want to say is not clear or not complete.
What I want to achieve is I don't want this textBox validating to raise when I click on any button. Because checking of duplicate ID is in this Validating event, so I can't set my textBox' CauseValidating to False.
Can I achieve like this? or this validating will always raise?
Thanks.
(Previously, I check this duplicate check at 'Save' button click event, but my customers complain that they want to know whether it is duplicate or not after they input the ID. Then I started to think where should I put these checking.
First, I want to put in Keyup event but this will call to DB connection every time user press a key. So, I think again. And then I got an idea of when user leave focus of this ID textbox. So, I put on Leave Event. But what cause problem is when user Key in ID and without do anything and click on Clear or Close button, this Leave event will raise. So, it cause the problem of form closing.
And now I changed to Validating Event, but it still cause the same problem like Leave event. I'm out of idea... :( )
1 Attachment(s)
Re: Text Box Leave event and Button click Event
Quote:
Originally Posted by
scsfdev
Hi,
It seemed solved but it still has problem. :(
Although I moved my checking from Leave event to Validating event of a textbox, it still go and check when I click Button.
I set my button's Cause Validating to False.
I do the checking in TextBox Validating event.
If I keyin some data in textBox and didn't go anywhere just directly click on Close button, it still go to Validating event which cause the message box to pop up if same ID is found in Database. And click event is not working too.
Any way to prevent this happen????
Thanks.
You must be doing something wrong but it's not possible to say what from your description. I assure you, if your Button's CausesValidation property is set to False then clicking it will not raise a Validating event on the TextBox. I've attached a project that demonstrates. Try playing with it. The TextBox validates for a numeric value. Try entering different values and then leaving the TextBox in different ways to see the behaviour.
Re: Text Box Leave event and Button click Event
Quote:
Originally Posted by
jmcilhinney
You must be doing something wrong but it's not possible to say what from your description. I assure you, if your Button's CausesValidation property is set to False then clicking it will not raise a Validating event on the TextBox. I've attached a project that demonstrates. Try playing with it. The TextBox validates for a numeric value. Try entering different values and then leaving the TextBox in different ways to see the behaviour.
Hi jmcilhinney,
Thanks for your help. I really appreciate it.
And also thanks for the sample program.
I tested it and it works well.
I also did the same way like your sample. (exactly the same, except my form didn't set Cancel button.) I wonder how you know that my form is dialogue form :)
May be because I have 2 other events for this textbox like Enter event (which will highlight all text in this textbox.) and KeyPress Event (use to check whether Enter key is press so I can move my cursor to next control.) I think these two will not effect Validating.
Anyway, I will clear all my coding and re code again for this form :(
I will update it later after I finish re-code it.
Thanks again for your help and explanation.
(This is my first time that I know I can use such features. :))
Re: Text Box Leave event and Button click Event
Quote:
Originally Posted by
jmcilhinney
You must be doing something wrong but it's not possible to say what from your description. I assure you, if your Button's CausesValidation property is set to False then clicking it will not raise a Validating event on the TextBox.
That probably wasn't worded very well. What I meant was that, if you have set CausesValidation to False and setting focus to that control still causes the previous control to be validated then there must be some other factor at play.
1 Attachment(s)
Re: Text Box Leave event and Button click Event
Hi all,
I know this thread is old, but as the explanations from jmcilhinney are very complete and right, I used his advices after suffering from using Leave event.
Even using Validating event, I still had problems with Causesvalidation property ; I solved and give you my solution but first I wrote this post because I found some strange side effects on these events.
In my app, I have many textboxes user can update, among some I have to check if value changed. After filling a box the user either:
- uses Tab key to reach next textbox,
- click on Register button to save entries
- click on Exit button without saving modifications,
and a supplementary situation is after clicking the Register button, in the Validating event I check if the special textbox value is changed, thus two possibilities:
- if text is changed, I start a simulation task that displays the consequences of the modifications, then the user should click a second time Register button to save entries
- if text is not changed, I just save entries.
After reading explanations of jmcilhinney, I thought Validation event fires only when textbox content is changed, but this is not true!
So I added a flag in Textchanged event and in the Validating event I test this flag.
Next difficulty was to detect if the user left the textbox by using Tab key or the Regsiter button (in this second case, I had to set another flag to exit the Register click event to cancel saving data), or the Exit button.
I used registerbutton.focused to detect button click. But that's not easy because I have many other buttons. I didn't find a property of the textboxes, like event.target in Javascript, to know the reason or destination after leaving the textbox.
Now about side effects, I noticed these two facts:
- if I use a breakpoint in the simulation task that occurs during the Validating event, when clicking on Register button, the click event of this button doesn't fire. It fires only if there is no break before ; so it seems the click event is programmed to raise after a fixed delay after Validating event?
- I modified the validationdemo of jmcilhinney (see attached file) by adding 2 listboxes, one register all events and I was suprised to notice that after clicking the Cancel button, we have Cancel click event and no textbox validating event, as specified, But, if then we click on another control like Listbox1, the textbox validating event occurs! In the modified demo I attached, I set causesvalidation=false for Listbox2 and causesvalidation=true for Listbox1. So the extra textbox validating event happens by clicking Listbox1 and not when clicking Listbox2 (this is right).
I hope somebody will confrm these facts..
Thanks