• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 466
  • Last Modified:

Getting and killing focus in edit control?

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
slumber
Asked:
slumber
  • 2
  • 2
  • 2
  • +4
1 Solution
 
MirkwoodCommented:
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
 
LischkeCommented:
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
 
Seeker092397Commented:
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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
luisrCommented:
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
 
PinTailCommented:
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
 
slumberAuthor Commented:
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
 
NickRepinCommented:
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
 
Seeker092397Commented:
Hi Slumber.
Have you tried my approach?
0
 
LischkeCommented:
slumber,

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

Ciao, Mike
0
 
PinTailCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 2
  • 2
  • 2
  • +4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now