Solved

Changing Dialog Default Button

Posted on 1998-11-28
9
2,394 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Is it possible to combine several text files into one big one? (redhat v5 enterprise) 4 92
sameEnds challenge 3 159
Annoying "thing" blocks my view 4 100
mapAB Challlenge 35 144
Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
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.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

773 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