OnKillfocus event eats OnClick event

Greetings!

An application I am trying to debug has several edit boxes and a Save
button.  One of the edit boxes displays a name retrieved from a
database.  That edit box has an OnKillfocus event that checks to see
if the user has changed the box's contents.  If so, then the other
edit boxes are initialized from the database.  If not, the user is
asked if he wants to create a new object.

If the user wants to create a new object with the same properties as
the current object, he would enter a new name and then click Save.
The OnKillfocus event of the edit box fires, correctly asking the user
if he wants to create a new object.  The user clicks "Yes".  Nothing
else happens.  The Save button's OnClick event is never fired.  I
tried removing the OnKillfocus event, and saw that the OnClick event
was fired as expected.

So, how do I get the OnClick event to fire?

The only workaround I can think of is to make sure the Save button
isn't included in the tab order and then fire its Click event
programmatically when it gets focus.  The user would click on the Save
button, the OnKillfocus event would fire, and then the OnSetfocus
event would fire.  I think.  But I seem to remember reading someplace
that that sequence actually happens in the other order, with
OnSetfocus firing first and then OnKillfocus.  If so, that idea won't
work either.  There's got to be a better way.

Thanks very much!

RobR

0
5/4/2007 12:42:02 PM
vc.mfc 33608 articles. 0 followers. Follow

9 Replies
1143 Views

Similar Articles

[PageSpeed] 57

Hi Cedric,

I've done this sort of thing many times without this kind of problem. 
You're right that you should get two messages.  Perhaps you could post some 
code snippets so we could see what you're trying to do exactly.  There may 
be some subtle problem.

Thanks,

Tom

<CedricCicada@gmail.com> wrote in message 
news:1178282522.179116.69530@y5g2000hsa.googlegroups.com...
> Greetings!
>
> An application I am trying to debug has several edit boxes and a Save
> button.  One of the edit boxes displays a name retrieved from a
> database.  That edit box has an OnKillfocus event that checks to see
> if the user has changed the box's contents.  If so, then the other
> edit boxes are initialized from the database.  If not, the user is
> asked if he wants to create a new object.
>
> If the user wants to create a new object with the same properties as
> the current object, he would enter a new name and then click Save.
> The OnKillfocus event of the edit box fires, correctly asking the user
> if he wants to create a new object.  The user clicks "Yes".  Nothing
> else happens.  The Save button's OnClick event is never fired.  I
> tried removing the OnKillfocus event, and saw that the OnClick event
> was fired as expected.
>
> So, how do I get the OnClick event to fire?
>
> The only workaround I can think of is to make sure the Save button
> isn't included in the tab order and then fire its Click event
> programmatically when it gets focus.  The user would click on the Save
> button, the OnKillfocus event would fire, and then the OnSetfocus
> event would fire.  I think.  But I seem to remember reading someplace
> that that sequence actually happens in the other order, with
> OnSetfocus firing first and then OnKillfocus.  If so, that idea won't
> work either.  There's got to be a better way.
>
> Thanks very much!
>
> RobR
> 

0
tom.nospam (3240)
5/4/2007 1:16:45 PM
The problem is the message box that is being popped up inside the
KillFocus event handler.  The help file for ON_KILL_FOCUS explicitly
says that a kill focus handler should not display or change a window.
When I removed the message box, I got both event handlers as
expected.

One solution, mentioned on another group, is to create my own message
(WM_APP_something).  Then, KillFocus can just send that message to the
current dialog object.  Inside that handler, I can do whatever I
want.  I'll see if that works.

RobR (aka CedricCicada)

0
5/4/2007 6:45:13 PM
As I already wrote you in codeguru forum you should POST not SEND message!

Victor

 <CedricCicada@gmail.com> wrote in message 
news:1178304313.698200.327170@e65g2000hsc.googlegroups.com...
> The problem is the message box that is being popped up inside the
> KillFocus event handler.  The help file for ON_KILL_FOCUS explicitly
> says that a kill focus handler should not display or change a window.
> When I removed the message box, I got both event handlers as
> expected.
>
> One solution, mentioned on another group, is to create my own message
> (WM_APP_something).  Then, KillFocus can just send that message to the
> current dialog object.  Inside that handler, I can do whatever I
> want.  I'll see if that works.
>
> RobR (aka CedricCicada)
> 


0
5/4/2007 8:00:41 PM
See below...
On 4 May 2007 05:42:02 -0700, CedricCicada@gmail.com wrote:

>Greetings!
>
>An application I am trying to debug has several edit boxes and a Save
>button.  One of the edit boxes displays a name retrieved from a
>database.  That edit box has an OnKillfocus event that checks to see
>if the user has changed the box's contents.  If so, then the other
>edit boxes are initialized from the database.  If not, the user is
>asked if he wants to create a new object.
*****
Right away I see problems.  If the OnKillFocus handler does ANYTHING that can change the
focus (such as popping up a dialog box) you are in trouble.  What the OnKillFocus handler
should do is if it detects anything needs to be changed, PostMessage a message that such a
change is required.  The handler for that message will do any updating or prompting
required.
****
>
>If the user wants to create a new object with the same properties as
>the current object, he would enter a new name and then click Save.
>The OnKillfocus event of the edit box fires, correctly asking the user
>if he wants to create a new object.  The user clicks "Yes".  Nothing
>else happens.  The Save button's OnClick event is never fired.  I
>tried removing the OnKillfocus event, and saw that the OnClick event
>was fired as expected.
****
No surprises here.  OnKillFocus handlers must not do anything that can change the focus.
And you've done that.  See above suggestion
****
>
>So, how do I get the OnClick event to fire?
>
>The only workaround I can think of is to make sure the Save button
>isn't included in the tab order and then fire its Click event
>programmatically when it gets focus.  The user would click on the Save
>button, the OnKillfocus event would fire, and then the OnSetfocus
>event would fire.  I think.  But I seem to remember reading someplace
>that that sequence actually happens in the other order, with
>OnSetfocus firing first and then OnKillfocus.  If so, that idea won't
>work either.  There's got to be a better way.
****
Yes, this is the wrong solution.  See the above suggestion.
****
>
>Thanks very much!
>
>RobR
Joseph M. Newcomer [MVP]
email: newcomer@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
0
newcomer (15973)
5/5/2007 4:24:59 PM
The POST_MESSAGE solution is not working for me, either.  The same
symptoms exist.  If the record does not exist in the database, I am
sending a POST_MESSAGE event to the current window.  Inside the new
message's handler, I pop up a message box to ask for confirmation of
the new record.  The save button's OnClick handler never is called.
If I comment out the message box but leave everything else intact, the
OnClick handler is called.  Here's the code:

void CWireInventoryGeneral::OnKillfocusCoilNumber()
{

	UpdateData(TRUE);
	if ( m_Coil <= _T(" ") ) return;

	if ( m_Document->GetCoil (m_Coil) ) {
		GetCoilData ();
	} else {
		// CWnd* pWindow = (CWnd*)this;
		PostMessage(WM_ADDNEW_APPROVAL_NEEDED, 0, 0);

	}

	UpdateData(FALSE);  // NOTE:  I tried commenting this line out, since
it changes the display, but that had no effect
}

afx_msg LRESULT CWireInventoryGeneral::OnNewCoilNumber(WPARAM, LPARAM)
{
	CString	OldCoil;

	OldCoil = m_Coil;

	// AfxMessageBox(_T("OnNewCoilNumber()"));
	CString TmpString;
	CString TmpString1;

	TmpString.LoadString(IDS_NOT_IN_INVENTORY);
	TmpString1.LoadString(IDS_NOT_FOUND);
	if ( ::MessageBox(this->m_hWnd, TmpString, TmpString1, MB_YESNO) ==
IDYES) {
		m_Document->m_bAddNew = TRUE;
		m_Status     = m_Document->FindFreeCoilStatus();
		//m_Order      = _T("");
		m_Cycle      = _T("");
		//m_Location   = _T("");
		m_UpdateDate = COleDateTime::GetCurrentTime();
		// m_OrderCtrl.SetFocus();
	} else {
		m_Coil = OldCoil;
		if ( m_Document->GetCoil (OldCoil) ) GetCoilData ();
	}

	return 0;
}

0
5/7/2007 2:29:27 PM
I tried creating as simple a project as possible to illustrate the
problem. I created a dialog project where the dialog has two edit
boxes and a button. I give the first edit box a KillFocus handler that
posts a WM_TEST_MESSAGE event. The WM_TEST_MESSAGE handler pops up a
message box. The button click handler also pops up a message box. When
the first edit box has the focus and I click the button, the test
message handler's message appears but the click handler's message
doesn't. If the second edit box has the focus and I click the button,
the click message appears. If I comment out the message box in the
test handler, give the first message box the focus, and then click the
save button, the click event message appears.

So, I'm still missing something. What is it?

RobR

0
5/7/2007 3:20:52 PM
>I tried creating as simple a project as possible to illustrate the
>problem. I created a dialog project where the dialog has two edit
>boxes and a button. I give the first edit box a KillFocus handler that
>posts a WM_TEST_MESSAGE event. The WM_TEST_MESSAGE handler pops up a
>message box. The button click handler also pops up a message box. When
>the first edit box has the focus and I click the button, the test
>message handler's message appears but the click handler's message
>doesn't.

In the above situation, what probably happens is that the kill focus
handler happens before the button click is determined. You quickly
display a message box which snatches focus from the button before
Windows does anything to determine a button click.

If you want to delay processing further than you can with PostMessage
(to prove a point), start a short time (a few hundred mS perhaps) in
the kill focus handler, and in the timer event handler, kill the timer
and then display a message box.

I'm really not sure what you're trying to do really here though. Doing
validation in response to losing the focus is too messy for words in
all but the simplest of cases.

Dave
0
davidl7375 (2060)
5/7/2007 4:57:57 PM
<CedricCicada@gmail.com> wrote in message 
news:1178551252.673243.44390@w5g2000hsg.googlegroups.com...
>
> I tried creating as simple a project as possible to illustrate the
> problem. I created a dialog project where the dialog has two edit
> boxes and a button. I give the first edit box a KillFocus handler that
> posts a WM_TEST_MESSAGE event. The WM_TEST_MESSAGE handler pops up a
> message box. The button click handler also pops up a message box. When
> the first edit box has the focus and I click the button, the test
> message handler's message appears but the click handler's message
> doesn't. If the second edit box has the focus and I click the button,
> the click message appears. If I comment out the message box in the
> test handler, give the first message box the focus, and then click the
> save button, the click event message appears.
>
> So, I'm still missing something. What is it?
>

Rob, perhaps showing the messagebox causes the button not to receive the 
WM_LBUTTONUP, so it doesn't generate the Click message.  Instead of showing 
the messagebox, have your OnTestMessage() call OutputDebugString so there is 
no additional windows getting the focus when you're testing killfocus!

-- David


0
dc2983 (3206)
5/7/2007 5:05:36 PM
Well, the KillFocus handler will be called before the click occurs.  It is called when the
focus is lost, for example, as the focus moves to the button you are about to click.
Because the PostMessage gets in the queue, it is dequeued long before the button-up (which
triggers the click event) is seen, so in fact it is not at all surprising.  KillFocus
events are very dangerous and should be avoided.
					joe

On 7 May 2007 08:20:52 -0700, CedricCicada@gmail.com wrote:

>
>I tried creating as simple a project as possible to illustrate the
>problem. I created a dialog project where the dialog has two edit
>boxes and a button. I give the first edit box a KillFocus handler that
>posts a WM_TEST_MESSAGE event. The WM_TEST_MESSAGE handler pops up a
>message box. The button click handler also pops up a message box. When
>the first edit box has the focus and I click the button, the test
>message handler's message appears but the click handler's message
>doesn't. If the second edit box has the focus and I click the button,
>the click message appears. If I comment out the message box in the
>test handler, give the first message box the focus, and then click the
>save button, the click event message appears.
>
>So, I'm still missing something. What is it?
>
>RobR
Joseph M. Newcomer [MVP]
email: newcomer@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
0
newcomer (15973)
5/8/2007 3:10:22 AM
Reply:

Similar Artilces:

Filter event
Hi all How can I catch a filter event? I found, that there is no such event, but I rely on, that someone has invented something... WBR Sonnich On Jun 2, 8:04=A0pm, jodleren <sonn...@hot.ee> wrote: > Hi all > > How can I catch a filter event? > > I found, that there is no such event, but I rely on, that someone has > invented something... > > WBR > Sonnich Well, I came up with this: Private Sub Workbook_SheetCalculate(ByVal Sh As Object) Dim iHidden, iRow As Integer Dim ws As Worksheet If Worksheets("BOM").Cells(1...

Event ID 1229
I am running Exchange Server 2003 SP2 and getting Event ID 1229 Description: Error -2147221233 from background index create. Folder 8-25B60B8 Index ?T668f+Q674a Database "First Storage Group\Mailbox Store (XXXXX)" From the Help and Support link it tells me that this error was fixed with Exchange Server 2000 SP1 Any Help? -- Thanks Duane -2147221233 = 8004010f =ecNotFound It's a prop that's missing. try setting HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\MSExchangeIS\ParametersPrivate Ignore Bad Oof Rule = 1 "Duane" <Duane@discussions.mic...

OnSetFocus(), OnKillFocus()
Hi, I have a standard MDI app. My view class is CFormView based. I have a microsoft chart activex control placed on top of the form. In the CFormView class I have a handler for OnSetFocus() and OnKillFocus(). I have this strange behaviour where if I click on another view in my app, the previous view that had focus first gets a kill focus message, then gets a set focus message somehow! It should just get a kill focus and that is it when losing focus. I have no idea why this is happening, but without the control present, the focus problem goes away and the set/kill focus messages are receive...

Event 9790 appears after DST update
enviornment: Running Wk23 Ent. SP1 with W2k3 Exchange Ent. SP2 All of a sudden after applying kb926666 my Application Log is full of the following entry: Event Type: Information Event Source: MSExchangeIS Mailbox Store Event Category: Background Cleanup Event ID: 9790 Date: 3/6/2007 Time: 9:16:08 AM User: N/A Computer: <mailserver> Description: Cleanup of the DeliveredTo table for database 'First Storage Group\<mailstore>' was successful. 13 entries were purged. There are numerous Event 9790 entries for my mailstores and its causing some lag issues. Is there anyt...

Detect "Save" menu event
How can I handle when the "Save" menu option is selected by a user?? I basically want to run some code either before or after a workbook save occurs. Does this mean that I should handle Workbook_BeforeSave()??? If the "SaveAsUI" parameter is false, does that mean that a "save" occurred instead of a "save as"?? thank you Hi Yes, that is the event you want. And your assumption is correct. Note that if the file is previously unsaved, Save will also display the SaveAs dialog. HTH. Best wishes Harald "Robert Crandal"...

Security Log Event Forwarding
Has anyone had success forwarding events from the Security log? I was able to get the Application & System logs working as expected using the machine account, but nothing from the Security log will work. I'm trying to pull DS Access Changes events from all Domain Controllers running Server 2008 R2 in a single forest AD domain to a Server 2008 R2 member server. I've also tried running the subscription as a Domain Admin for testing, but I receive the same error: Code (0x138C): <f:ProviderFault provider="Event Forwarding Plugin" path="%systemroot%...

OnKillFocus
When pNewWind (pointer passed to OnKillFoucus handler) will be NULL? On May 21, 9:25=A0am, rahul <hyrahu...@gmail.com> wrote: > When pNewWind (pointer passed to OnKillFoucus handler) will be NULL? On my machine, when I set focus on any other program's window. Why do you care (IOW, you have a bad question here)? MSDN says that it can be NULL. It means that you are not supposed to know the next window at any given time WM_KILLFOCUS is sent. And indeed, it is possible, especially in Vista+, that random program can't receive a HWND of another program (e.g. that other ...

MSExchangeIS Private = Event ID =1016
how can I user that doesn't have Admin privilege can get into somebody else's mailbox and read it?. I get this "Success Audit" with the event ID 1016. What does this mean? is he reading anybody elses emails? If true, how can I stop it. thx they may have delegate access on a boss' calendar or something like that... "Jose Perez" <anonymous@discussions.microsoft.com> wrote in message news:025b01c3d6fa$02195ad0$a501280a@phx.gbl... > how can I user that doesn't have Admin privilege can get > into somebody else's mailbox and read it?. I get &...

return to first button click event after another button click event
Hi, Is it possible to - after clicking a command button on a form and running some code to then exit that button click event and wait for a user to click another command button, this then returning code execution to the first command button click event at the point were it left from ? regards Trevor. -- Message posted via http://www.accessmonster.com On Fri, 04 Dec 2009 05:31:08 GMT, "trevorC via AccessMonster.com" <u44860@uwe> wrote: >Hi, >Is it possible to - after clicking a command button on a form and running >some code to then exit that button...

Logging into GP Dynamics 7.5 reports error dealing with VBA event
While logging into Great Plains Dynamics, an error occurs stating "A serious error occured during a VBA event". We are running a duel terminal service clustered environment and logging into our main database gives this error. It is only happening on one of our 2 clustered boxes. After a second reboot of the Windows 2003 server, the error seeemed to disappear... During our migration, i am worried this error will reappear... has anyone seen or know of this error that could lead me down the right path? The event logs do not report any errors... is there by chance specific log...

WM_KILLFOCUS or OnKillFocus
I have created a modeless window. When I click on the parent window, I want it to close, something like a context menu that closes. In the modeless window, I tried OnKillFocus, and also trapping WM_KILLFOCUS in OnWindowProc but none works. How to auto close the modeless window? Thanks. Foci Problem fixed. I change the parent window from Dialog type to FormView type. In the FormView's OnSetFocus function will destroy the modeless window. "Allan" <nothing@nothing.com> wrote in message news:OxxhVALbFHA.3196@TK2MSFTNGP14.phx.gbl... >I have created a modeless win...

Event after update of last record in subform
I have a subform which lists specifications for a product. The user enters the specification, hits enter, and the code below concentenates the name of the spec, the value, and the prefix/suffix as required. Multiple specs are strung together into a single memo field. Everything works fine until the last record on the subform. The event fires, but (I assume) because the last record does not lose the focus, the value of the last spec is not updated in the concentenated memo field. If the code is run after focus is moved off the last spec, the spec updates as it should. Any thoughts on how to ...

Events in Year view?
I'm trying to find XP software (perhaps Outlook) that shows events/ appointments in Year view. Will Outlook do this, if so which versions? If not, can you recommend other XP software that will? TIA What format do you want it in? See http://www.slipstick.com/tutorials/yearlycal/yearlycal.asp for the only type outlook supports. Also, if you use outlook 2007, the calendar printing assistant can print one. -- Diane Poremsky [MVP - Outlook] Outlook Tips: http://www.outlook-tips.net/ Outlook & Exchange Solutions Center: http://www.slipstick.com/ Outlook Tips by emai...

Exchange event agent scripting issue
Hi, My client still runs an exchange 5.5 and I have to develop an agent script which needs to do some autoreplying. The problem is that I can't get any script running, even just an empty script that will only log upon the event triggers. In Event log I got a MAPI error with the globalish code 0x80004001. The only info I could find on the net, pointed out to install at least SP3, though this is in fact installed and I'm still getting the errors and logs stay empty. Does anyone have any idea... please! :-( Thanks in advance... Greets, A goofy it-er ...

Force Event log message when checking database size
Is there a way to force the posting of a message, when the database is checked for size? In my case I have changed the size and saw it on the re-start of the service. Now I would like to see it when it does check (at 5:00AM) each day. Can this be done? Thx On Mon, 23 Oct 2006 13:28:51 -0400, "John Leonard - Sage" <sagegrp@adelphia.net> wrote: >Is there a way to force the posting of a message, when the database is >checked for size? In my case I have changed the size and saw it on the >re-start of the service. Now I would like to see it when it does check (a...

event id for shutdown
Are the event id's for a shutdown different to that of a restart in win2000? and if so what are they? Thanks Max ...

Searching past events in Outlook 2000
For years, I've been searching past appointments by going to Advanced Find, entering "start" and "anytime" and then the name of the appointment I'm searching for. Now, all of a sudden, it's not working. I'll look for a specific client name, wanting to find all instances of this client, and find none - even though I know there are many in the calendar. Does anyone have any ideas about what might be wrong? If I search on my PDA (which syncs with Outlook), I can find them just fine. Thanks, .. Joann Any change your calendar items are being auto-archived?...

OnKillfocus event eats OnClick event
Greetings! An application I am trying to debug has several edit boxes and a Save button. One of the edit boxes displays a name retrieved from a database. That edit box has an OnKillfocus event that checks to see if the user has changed the box's contents. If so, then the other edit boxes are initialized from the database. If not, the user is asked if he wants to create a new object. If the user wants to create a new object with the same properties as the current object, he would enter a new name and then click Save. The OnKillfocus event of the edit box fires, correctly asking the us...

Cannot delete xml file - file based event scheduling
Hi, I have a report scheduled with File based event scheduling in CR XI Server version 11.5. The report is created with XML file as its datasource. The report will be triggered if the event of xml file is created in the source directory. As per the schedule, it will look for the event to occur every 1 minute. Since I need to enerate this report everytime the file comes into the directory, I have to delete the file after he report is generated, so that, next time the file comes in, the report will be generated again. After the report is run for the first time successfuly as the event occurs...

Printing certian events
How do I print a calendar with only certian events instead of a calendar with all events and appointments. For example if I want to print a calendar with only Business events? You could Filter the view first, or use Find, depending on what kind of calendar and time frame you were printing. Matt Lujano wrote: > How do I print a calendar with only certian events instead of a calendar with > all events and appointments. For example if I want to print a calendar with > only Business events? Create a filter that shows only items in the Business category. -- Dia...

OnKillFocus problems
Hi all I've created a set of controls derived from CEdit in order to do in-place validation (i.e when you tab out of the field rather than when you complete the form). Mostly (at least 99%), they're working fine, but I've had a couple of problems with one particular form and, sadly, intermittently. Basically, I've over-ridden OnKillFocus() and do my validation there, saving the validated data for later use. When the "save" button is clicked, UpdateData(TRUE) is called which (I believe) then calls DoDataExchange(), where the data is taken from my controls into a set ...

Event 9667
This is a multi-part message in MIME format. ------=_NextPart_000_0013_01C4C1BF.DCDC0EA0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Hi, We have started to get lots of event IDs 9667 on our exchange server = (2003). Failed to create a new named property for database "First Storage = Group\Mailbox Store (MAILFAXSRV)" because the number of named properties = reached the quota limit (16384). User attempting to create the named property: "SYSTEM" Named property GUID: 00020386-0000-0000-c000-000000000046 Named prop...

Recurring event
Is there a way to set a meeting date and have it recurring each month, but not on a certain week or certain date of each month. My meeting happens on a Thursday in each month but not every 2nd, 3rd or 4th Thursday. Some months it is the 3rd Thursday and some it is the 4th Thursday. ...

What event when I click the record selector
I have some labels on the form would like to populate its value (they are calculated values) when I select the record selector, next, previous or last or first. What event should I write the code into? On Tue, 22 May 2007 11:01:23 +1000, "Alan T" <alanpltseNOSPAM@yahoo.com.au> wrote: >I have some labels on the form would like to populate its value (they are >calculated values) when I select the record selector, next, previous or last >or first. > >What event should I write the code into? > Well, don't use Labels for this purpose; use textboxes ...

Events, dear boy, events #3
Aha I pasted: Application.EnableEvents = true into the immediate window and now the code is working. I don't have an code that would have turned it off in the first place, however. Coul this be a default setting somewhere, or is just one of those thing that can happen now and then? Anyway, thanks for the solution, Bo -- Gromi ----------------------------------------------------------------------- Gromit's Profile: http://www.excelforum.com/member.php?action=getinfo&userid=92 View this thread: http://www.excelforum.com/showthread.php?threadid=27721 Just a guess. If you turn...