Changing Dialog Default Button

I have a dialog with 2 buttons (plus many other controls), each with a CButton bound to it (m_btnOK and m_btnAND).

Depending on what the user has done so far, I want to change the default button (the one with the thick border which also accepts Enter).

I have tried :-

      if ( bOK )
      {
            m_btnAND.SetButtonStyle( BS_PUSHBUTTON ) ;
            m_btnOK.SetButtonStyle( BS_DEFPUSHBUTTON ) ;
      } else
      {
            m_btnOK.SetButtonStyle( BS_PUSHBUTTON ) ;
            m_btnAND.SetButtonStyle( BS_DEFPUSHBUTTON ) ;
      }

This shifts the thick border around, but the AND button can never be "pressed" by hitting Enter.  Therefore it doesn't work to my satisfaction (and this is what I need to fix).

If it makes any difference, the m_btnOK is the OK button, and m_btnAND is just some other button I added to the dialog.

Any hints ???

I seem to remember a Tech Note or something explaining how to change the default button, but I can't find it.

LVL 8
Answers2000Asked:
Who is Participating?
 
migelConnect With a Mentor Commented:
Hi! You must use CDialog::SetDefID(uID) function
0
 
Tommy HuiEngineerCommented:
You need to use CWnd::SetDefaultId() to set the default id of the button that has the default property set.
0
 
Answers2000Author Commented:
I don't think this is the right function name thui

I'm using VC5

I can't find it in the help

I also tried a text search thru MFC source and includes, and the string "SetDefaultId" isn't there!


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.

 
cyrilbdtCommented:
try this:
if ( bOK )
{
m_btnAND.SetButtonStyle( BS_PUSHBUTTON ) ;
m_btnOK.SetButtonStyle( BS_DEFPUSHBUTTON ) ;
m_btnOK.SetFocus();
} else
{
m_btnOK.SetButtonStyle( BS_PUSHBUTTON ) ;
m_btnAND.SetButtonStyle( BS_DEFPUSHBUTTON ) ;
m_btnAND.SetFocus();
}

hope this helps
0
 
wyy_cqCommented:
you can change the button style to make it looks like the default button, but if you click enter key the dialog will call the CDialog::OnOK() not the default button's clicked function.

so you can do like this:
1. set a button 's style to be default_button .

2. reDefine the virtual CDialog::OnOK()
OnOK will be called , when the "enter" key clicked.

3. if you want to quit the dialog and let the DoModal() return IDOK. you can call the CDialog::OnOK();

remember this:
when "enter" clicked , OnOK() be called no matter which button is default.
when "escape" clicked, OnCancel() be called.
0
 
Answers2000Author Commented:
This is kind of a good idea, except it doesn't work in all cases (including my app), and as far I can see is not a workable solution

Here's what I think you're suggesting
-> Basically in OnOK before calling base class, decide if the default appearance has shifted.

The problems are :-
(a) When OnOK is called, I can't tell if this is because of a user keypress or because of a mouse click, therefore there's no way for my program to decide whether OnOK should "pretend" to be the other button
(b) If the OK button is disabled, but the other button is enabled and set to the default [this can happen in my app], then OnOK is never called from pressing Enter.  The behaviour the user would expect based on appearance on screen, is that Enter at this point would activate the other button, but infact this code would not activate any buttons.




0
 
wyy_cqCommented:
if the "enter" clicked the OnOK will be called,
if the mouse clicks the button ,the OnOK will not be called unless the button ID is IDOK.
so you can define another function to deal with the mouse clicked message.

the virtual function you overrided will be called first. and you can decide whether call its father's function.

even if the button is disabled the  onOK will be called when "enter" key pressed or the button whose ID is IDOK be clicked.

in the dialog (MFC), if you want to capture the "enter"key you must reDefine the OnOK(...) and there is no other way.

default button clicked by mouse will not result for the calling of OnOK().

0
 
wyy_cqCommented:
i think i am something wrong ,
i write a little demo a moment ago.

in the dialog, when the "enter" key pressed the default button's BN_CLICKED function will be called.

but if the focus is on another button the "enter"key press will not result in the calling the button's message function. neither the OnOK nor the default button's message function
0
 
Answers2000Author Commented:
Hurray!
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.

All Courses

From novice to tech pro — start learning today.