Trapping when leaving a container if the data has changed

I have been reading the discussions about whether or not Containers should have got/lost focus events. They so in fact have them but they dont seem to work. I have tried lots of different settings but I cnt get the LostFocus to fire for my containers.

This is the challenge.
I have a container with the bulk of my edit fields, an associated class to hold all the data and have various display/data related methods (DisplayRefresh, Get Data, SaveData etc).

I set the Container.Changed boolean when a control in the container changes. That in turn raises the Container.ContentsChanged event in which I set a container public property “IsDirty”.

So I know from ‘outside’ the container if the data has changed.

But my problem is, how do I ‘trap’ when the user leaves the container? I know if the data needs saving from the ‘IsDirty’ property, and I can save it away no problem.

But there are many different ways the user could ‘leave’ the container and there does not seem to be an event that the container will raise to say it is being left.

Instead it seems I have to put the same checking code in every possible route that the user could leave the container (Search for an other record, go to another record, close the app, got to a different section of the app etc etc etc).

Now if the container.lostfocus worked…

Any ideas pls???


What if they never leave the container but hit “save” or otherwise trigger a save to occur ?

Hi Norman

Yes that all works fine. I have a toolbar with a save button. If it is pressed it checks the ‘IsDirty’ flag and fires off the save process if needed.

Problem is it relies on the user to remember to press the Save button before heading off somewhere else. I wanted to do some kind of warning that a Save is needed when they try to move away from the container.


One of the controls likely has focus (although this is NOT always true depending on what OS you’re on)

Use their lost focus event since the container doesnt actually have focus

What I’m doing is, if anything has changed, I’m setting the save button as default to remember the user to save the data