Solved

Changing Dialog Default Button

Posted on 1998-11-28
9
2,383 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 2

Expert Comment

by:wyy_cq
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 8

Author Comment

by:Answers2000
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Hi! You must use CDialog::SetDefID(uID) function
0
 
LVL 8

Author Comment

by:Answers2000
Comment Utility
Hurray!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
xyBalance chalenge 58 88
repeatEnd java challenge 42 83
Prevent this page from creating additional dialogs. 3 249
dog bark java program 15 81
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

743 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now