Solved

Getting and killing focus in edit control?

Posted on 1999-01-25
10
452 Views
Last Modified: 2013-12-03
I'm trying to show a dialog box when user clicks on the edit control.
I process EN_SETFOCUS so that when application receives an EN_SETFOCUS, it
makes a dialog box by DialogBox.
The problem with this method is that when I close the dialog box by EndDialog, it
dispaly dialog again and again. I think it's because that the edit control still has input
focus. So I fall into a kind of infinite loop to display dialog box.
Could someone tell me a better way to implement this?
0
Comment
Question by:slumber
  • 2
  • 2
  • 2
  • +4
10 Comments
 
LVL 13

Expert Comment

by:Mirkwood
ID: 1419216
First of all your conclusion is right. After the dialog is destroyed, you receive focus again and therefor you are in an infinite loop.
The best solution is to test where the focus came from. If it comes from an other window then do not show the dialog.
Please let me know if this helped.
0
 
LVL 10

Expert Comment

by:Lischke
ID: 1419217
Hi slumber,

wouldn't it be better to process the mouse down message rather than set focus? You want the dialog to appear when a click occurs, not by a focus change, doesn't you?
0
 
LVL 1

Expert Comment

by:Seeker092397
ID: 1419218
You can do the following trick:

In the handler for EN_SETFOCUS after you return from DialogBox you can remove additional EN_SETFOCUS messages sent to your window right after dialog box got closed(i suppose they will be sent right away, but may be i'm wrong and all this won't work). You can do it by following code:

MSG msg;
while (PeekMessage(&msg, hWnd/*or NULL*/, EN_SETFOCUS, EN_SETFOCUS, PM_REMOVE));

I think it would work, but again...

Best regards.
0
 
LVL 3

Expert Comment

by:luisr
ID: 1419219
You don't say what are you trying to achieve, but I suppose is some kind of data entry (that is, the dialog allows you to select some data to put in the edit control). Maybe you could delay the display of the dialog until a key is pressed (WM_KEYDOWN or WM_CHAR).
0
 
LVL 3

Expert Comment

by:PinTail
ID: 1419220
If the mouse click option is no good, why not just call NextDlgItem() before launching the Dialog, this way when the dialog closes, the focus is on another control
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:slumber
ID: 1419221
Dear PinTail:

Even though I change the focus to other control just before calling a dialog box, the original edit control gain again when I destroy dialog box. So infinite loop still continues.
Isn't there any other way?

Dear Lischke:
How can I interpret a mouse event as selecting edit control?
0
 
LVL 15

Expert Comment

by:NickRepin
ID: 1419222
slumber, it seems that you doing something wrong. May be, the best way is to do not use this strange behavior of edit box?
0
 
LVL 1

Expert Comment

by:Seeker092397
ID: 1419223
Hi Slumber.
Have you tried my approach?
0
 
LVL 10

Expert Comment

by:Lischke
ID: 1419224
slumber,

process the WM_LBUTTONDOWN or WM_LBUTTONDBLCLK message to get notified when the user clicks in the edit control window.

Ciao, Mike
0
 
LVL 3

Accepted Solution

by:
PinTail earned 100 total points
ID: 1419225
I beg your pardon, I said NextDlgItem(), I should have said NextDlgCtrl().  I have tested this and it works for me, It definitely does not get called more than once just because the CDummy Dlg object gets destroyed.  Are you changing the value in the edit box via the child dialog ??

void CTestDlg::OnSetfocusEdit1()
{
NextDlgCtrl();      
CDummy oDlg;
oDlg.DoModal();
}
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

With most software applications trying to cater to multiple user needs nowadays, the focus is to make them as configurable as possible. For e.g., when creating Silverlight applications which will connect to WCF services, the service end point usuall…
For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
This video discusses moving either the default database or any database to a new volume.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now