How to use CDialog::SetDefId with a non button control? - c++

I've come across CDialog::SetDefId and while it is pretty easy and clear that it is for "pushbuttons" I wanted to use this functionality with a non button control.I understand that you have to press Enter or Return to make the dialog use that ID
If I set nID to 0 in the CDialog::OnInitDialog and have no default button setted the dialog will default to CDialog::OnOk, If I do have a default button setted, the dialog will push that button as expected.The thing is that I want to make this work for a non button control, so if I set nID to a control that is not a pushbutton, the dialog will do nothing even If I set the message handler for the keydown event or NM_RETURN, it doesn't matter if the control has focus or not, the dialog will still do nothing if I press Enter or Return.How can I make the control be the default control without using stuff like PreTranslateMessage? and which message is sent to the control?thanks in advance

Yes, it only works with buttons but you can use SetFocus to change focus to any other control:
BOOL CMyDlg::OnInitDialog()
{
CDialog::OnInitDialog();
GetDlgItem(IDC_CHECK1)->SetFocus();
return 0; // return TRUE unless you set the focus to a control
}
In this example the OK button may still be the default button. Enter key will go to default button, probably IDOK. But space key it will change the check box IDC_CHECK1.
There has to be a default button. If you don't want one, then add a fake button, lets say IDC_BUTTON1, and make it the default button and not visible, then you don't see a default button (you can still add IDC_BUTTON1 to message map and decide what to do with Enter key)

Related

MessageBox - return value when box has no Cancel button

according to the MessageBox documentation:
Return value
Type: int
If a message box has a Cancel button, the function returns
the IDCANCEL value if either the ESC key is pressed or the Cancel
button is selected. If the messagebox has no Cancel button, pressing
ESC has no effect.
What if I wish to have a a box that has no Cancel button but I want to distinguish between OK and close/ESC?
MessageBox() does not support the behavior you are looking for. You would have to hook the dialog directly, using SetWindowsHookEx() or SetWinEventHook(), in order to detect it being closed.
Use TaskDialogIndirect() instead. It has a TDF_ALLOW_DIALOG_CANCELLATION flag:
Indicates that the dialog should be able to be closed using Alt-F4, Escape, and the title bar's close button even if no cancel button is specified in either the dwCommonButtons or pButtons members.
All of those conditions will return IDCANCEL.
What if I wish to have a a box that has no Cancel button but I want to distinguish between OK and close/ESC?
Standard dialogs don't offer such behaviour because it is very poor design. As a point of principle, a GUI should allow actions to be performed by mouse or keyboard. A hidden action only accessible by keyboard is a sign of poor design.
If you really want to make such a dialog, you'll have to implement it yourself. However, you should not. Present the dialog with OK and Cancel buttons.

How to invoke a button using C++ Win32 API when press the enter button?

i have created a dialogbox using c++ win32 API...
there are 3 text box,1 combo box and 3 buttons...
now i have 2 problems...
1.when i press the ENTER button,it invoke second button(ID_OK) function,but i want to invoke first button(ID_MYBUTTON)...
2.i am using the code to focus a textbox is,
SetFocus(GetDlgItem(_hwnd, IDC_NAME));
But it cant focus that dialogbox,i mean cursor position is there,but cant get any value,when i typed...
Can anyone resolve it?
This may answer both your questions:
http://blogs.msdn.com/b/oldnewthing/archive/2004/08/02/205624.aspx:
Use the DM_SETDEFID message to set the default button in a dialog
Use the WM_NEXTDLGCTL message instead of SetFocus()
// set default button
SendMessage(_hwnd, DM_SETDEFID, (WPARAM)ID_MYBUTTON, 0);
//TODO: if the former default button's style remains, update with BM_SETSTYLE
// set focus
SendMessage(_hwnd, WM_NEXTDLGCTL, (WPARAM)GetDlgItem(_hwnd, IDC_NAME), TRUE);

Changing UI state depending on a checkbox value

I have the following situation.
There is a dialog with a check-box and a text-box. Check-box's click is bound to a function that toggles if the text-box is enabled or grayed out. It works fine but I also need to preset some values to the dialog before creating and displaying it. If the variable that is connected to the check-box is set to ture I want to disable the text-box.
I tried to accomplish this in different ways, but it all boils down to the fact that I can not change the GUI of the dialog before calling DoModal (I get assertion falure when I try).
This is probably a common problem, but I could bot find a solution online. Am I completely off track?
MyDialog d(this);
d.bFlag = TRUE; // Because it is true, I want the text-box to be disabled
// I could call a function of d here that would set the state of the text-box correctly,
// but an assertion falure would happen.
if (d.DoModal() == IDOK){
...
}
You need to override OnInitDialog function in your dialog class MyDialog and have the code to check the check box value and enable/disable the text box.

How to change the default QPushButton in a QDialogBox

My goal is to have two buttons, "Cancel" and "Connect", and to have Cancel be the default button when the user presses ENTER. I also want the user to be able to TAB to the next button ("Connect") and press ENTER and have "Connect" get pushed. Here's my code:
QPushButton * cancelButton = new QPushButton(tr("&Cancel"));
cancelButton->setAutoDefault(true);
cancelButton->setDefault(true);
cancelButton->setFocus();
QPushButton * continueButton = new QPushButton(tr("Co&nnect"));
continueButton->setAutoDefault(true);
continueButton->setDefault(false);
ui->buttonBox->addButton(cancelButton, QDialogButtonBox::RejectRole);
ui->buttonBox->addButton(continueButton, QDialogButtonBox::AcceptRole);
Setting the Cancel button to the the default button doesn't actually seem to work.
Setting the autoDefault property on all the butons seems to be necessary in allow the buttons to get pushed after pressing TAB and ENTER for example. This seems to jive with the documentation for autoDefault. However, the documentation for the default property seems to indicate that the default button will get pushed only if there are no buttons that have the autoDefault property set. Otherwise, the button that gets pushed when ENTER is pressed will be the currently selected autoDefault button. So it seems like what I need to do is to make the cancelButton have focus by default, but I can't seem to figure out how to do this.
You have to call cancelButton->setFocus(); after adding the buttons to the QDialogButtonBox, not before.
Try adding below line before you call dialog->show
button->isEnabled(true)

how to save radio button status?

i have dialogbox as a singleton class and on that dialog box i hv 2 radio button on and off which are grouped.when i make on group true in resource and add variable then if i clicked on off radio button and then if i open again that dialog box its focus is on "off" radio button which is right. but when i make "off" button group true i.e. initially its on "off" raio button then if i cliked on "on" button and closed an reopen the dialog focus is neither on "off" nor on "on".i hv used setfocus also but nothing working
You can use SetCheck to select the proper radio button when the dialog opens and GetCheck on each radio button to see which is selected. The easier option is to use DDX_Radio to automatically associate an index with the selection of a radio button.
If I am not wrong with you description, when you close the dialog box you call the destructor. If the dialog is in a 'greater' executable - the dialog is called by another dialog in the same .exe - you can declare the variable as global instead of member (as I suppose is declared).
Otherwise you send data to your .exe by using shared data.
Got the answer:)
suppose i have made two radio button Radio1 and Radio2 under group box then we hv to make group true for first radio button i.e. Radio1 then add varible integer on Radio1 button.If we want focus on Radio2 initially or by default then in constructor of dialogbox on which these radio button exist make variable value as 1 (as index start from 0).