Solved

Changing Dialog Default Button

Posted on 1998-11-28
9
2,397 Views
Last Modified: 2013-11-20
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.

0
Comment
Question by:Answers2000
9 Comments
 
LVL 15

Expert Comment

by:Tommy Hui
ID: 1325435
You need to use CWnd::SetDefaultId() to set the default id of the button that has the default property set.
0
 
LVL 8

Author Comment

by:Answers2000
ID: 1325436
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
 
LVL 2

Expert Comment

by:cyrilbdt
ID: 1325437
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
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.

 
LVL 2

Expert Comment

by:wyy_cq
ID: 1325438
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
 
LVL 8

Author Comment

by:Answers2000
ID: 1325439
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
 
LVL 2

Expert Comment

by:wyy_cq
ID: 1325440
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
 
LVL 2

Expert Comment

by:wyy_cq
ID: 1325441
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
 
LVL 12

Accepted Solution

by:
migel earned 20 total points
ID: 1325442
Hi! You must use CDialog::SetDefID(uID) function
0
 
LVL 8

Author Comment

by:Answers2000
ID: 1325443
Hurray!
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Unix / Linux grid computing 5 163
notReplace  challenge 53 142
Unix Command -- Challenging  question 7 96
Authenticate using sesu from script 7 58
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

831 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