In my Windows app, I have a listbox showing various records on a page panel. When a record is selected, the corresponding detailed data for that record is shown using various textfields in a container control. I allow the user to change the data within the container control and have provided a save button to update a database. This works fine if the user clicks the save button after changes are made. It doesn’t work if the user clicks elsewhere (like a new row or goes to a different page panel). I thought I could use the lost focus event of the container control to test to see if the data was saved, but that event just doesn’t work.
Is there any other way I can check to see if the container control has lost the focus so I can test to be sure the user’s changes where saved?
One of the controls in the container probably has focus and so its lost focus event might be raised
You could add an event to the container “LostFocus” and then just raise that from the lost focus events in the controls in the container
I’m scratching my head about why GotFocus/LostFocus was implemented for a ContainerControl. It was obviously a deliberate act, as the docs for AcceptFocus explicitly mention ContainerControl. When and how does it make sense to think of the Container getting focus? The controls in the container can get focus, but the container itself? I don’t understand how that makes any sense.
Norm pretty much described what is going on in my app. The problem with his solution is that I have several controls in the container control. If the user moves from one control to another in the container, the control loses focus, but the container hasn’t really lost the focus. It only loses the focus if another control outside the container receives the focus, except this is not reported (by design).
So, any ideas then on how to know when a control outside a container control receives the focus?
You’d still have the same problem. The canvas would never “get focus”, just the controls in the container. To make this work right, you have to trap every possible way the user can change focus - mousedown and keydown of every control - to determine when focus is switching from inside the container to outside and then 1) validate the contents of the container, and 2) either alert the user (and disallow the focus change?) or update the record.
I hope I am understanding properly your problem and I am not proposing a too stupid idea here… : what about having a flag/boolean property that will be set to true when any of the controls on the container control loses focus and they will set it to false when they get focus. Also, every time one of the controls loses focus set a timer to fire almost inmediately and check for the value of the flag. If the flag is true one control lost focus and no other control of the container control got it.
Container controls have a “focus” property (same as a window)
And that is the currently focused control - whether its in the CC or not
So you could check that “focus” is one of the controls in the CC and if so then it has focus and if not it doesn’t
Something like
create a new desktop project
add container control
add two event definitions GotFocus & LostFocus
add a text field & text area to the container control
in the lostfocus event of those two controls put
if focus = textArea1 or _
focus = textField1 then
return
end if
raiseEvent LostFocus
in the got focus events of those two controls put
if self.focus = textArea1 or _
self.focus = textField1 then
raiseEvent GotFocus
end if
put an instance of the cc on the default window
put some other controls on the window outside the cc that can get focus (more text fields & text areas)
implement the GotFocus & LostFocus events of the cc
That should cover it
Note that for Windows you’;ll also have to check buttons & other controls that can also get & lose focus
I tried what you mentioned but I can’t get it to work. I may have implemented the GotFocus and Lost Focus event/event defintion incorrectly. Also, the Focus property is returning Nil. Could it be that the control has lost focus but the next control has not yet received it, therefore the Focus property is Nil?
Here is my sample project. I only implemented the LostFocus as that’s all I need for now. Perhaps someone can give their thoughts on why it is not working.