Lost Focus

  • Thread starter Thread starter Patrick De Ridder
  • Start date Start date
P

Patrick De Ridder

How do I connect to an event being thrown when a field has
lost focus? Could you please give a code example?
 
Patrick> How do I connect to an event being thrown when a field has
Patrick> lost focus? Could you please give a code example?

WinForm or WebForm?
 
For a windows application:

[Assuming that you wish to receive LostFocus notification
for a textbox called textBox1]

Create a method as follows:

private void textBox1_LostFocus(object sender,
System.EventArgs e)

Add the following to InitializeComponent():

this.textBox1.LostFocus += new System.EventHandler
(this.textBox1_LostFocus);

HTH,

mk
 
Hi mk,

Your solution to signify the loss of focus of a text box works fine !
Many thanks.
 
100 said:
Hi Patric,

A bunch of events are fired when the control loses the focus.
The right order is:
Leave
Validating
Validated
LostFocus

According to the event order above. You will receive LostFocus only if the
focus is actually lost and Leave as a inforamtion when request for changing
the focus has arrived. Validating event handler can cancel the request and
it is posible to receive Leave, but not to receive LostFocus.

Anyway there is a bug in the framework and this order is valid only when the
focus has been chaged using the TAB key to circle among the controls. If you
use the mouse for changing the focus (which is most often used I think) the
event order is:
LostFocus
Leave
Validating
Validated

You can see that you will receive LostFocus first and then the others
events. Validating event handler can cancel the operation and the focus
won't be changed even though you have received LostFocus.
So, the LostFocus is not reliable.
I haven't seen good workaround for this bug. Some people suggest catching
Windows messages, other using some special flags inside the form class. But
I think there is no universal cure for this. Until MS don't fix this the
best workaround depends on the application logic.

Hi 100,

This is very interesting. However, what you
write is not totally within the scope of my
understanding at this point in time. Are
there any references for me to look at
on what you are discussing?

I have applied what mk advised me to do,
and that works fine.So I am very pleased.

Greetings,
 
Hi Patric,
I have applied what mk advised me to do,
and that works fine.So I am very pleased.
This is the right way to do it. I just warned you about a well known bug in
the framework.
As long you don't do any validation of the data typed in text boxes It will
be fine. I just mantioned it just in case you are going to use validating
event. If you use LostFocus and Validating event together you will
definitely run into this.

B\rgds
100
 
I will read up on validation, since that appears to be your main concern,
and I will have been warned by you not to combine it with the focus stuff.
Actually I do quite a bit of data validation (my code) in the prig, alerting
the user in the case of incorrect prig use with message boxes. Should I
understand from you that there are better alternatives to this approach?
Sorry prig = prog(gram)
(Typo)
 
Back
Top