Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Changing Dialog Default Button

Posted on 1998-11-28
9
Medium Priority
?
2,440 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 80 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
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.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

618 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