Changing UI state depending on a checkbox value - mfc

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.

Related

MFC: How to set the focus of CEdit boxes?

I'm working on my first simple MFC project, but I'm struggling with one problem: want to set the focus of all CEdit boxes in one of the dialogs. My idea is when open the dialog, the focus to be on the first edit box and then to swap between them with 'tab'.
I saw the method SetFocus(), but I couldn't apply it correctly. Also I couldn't find a solution for implementing the order of the focus with a specific key.
Thanks in advance to everybody who takes time to help me!
You can set the focus to a given control when your dialog box is first shown by calling SetFocus in your OnInitDialog() function. However, if you do so, your OnInitDialog() must return FALSE:
BOOL MyDialog::OnInitDialog() {
CDialog::OnInitDialog(); // Call base class member
GetDlgItem(IDC_MYEDIT)->SetFocus();
//..
return FALSE; // Otherwise, the framework will reset the focus to its default
}
From the M/S documentation:
Return Value
Specifies whether the application has set the input focus to one of
the controls in the dialog box. If OnInitDialog returns nonzero,
Windows sets the input focus to the default location, the first
control in the dialog box. The application can return 0 only if it has
explicitly set the input focus to one of the controls in the dialog
box.

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

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)

Bitmap in Buttons disappear

I have a CFormView SDI which calls and opens up a CDialog.
In the CDialog I have a button which has a bitmap displayed.
All works well, until I close the CDialog.
When I open up the same CDialog (using create function), the button appears, and the button's functionality is there, however the bitmap disappears.
Can someone please tell me why the bitmap on the button disappears on subsequent calls to CDialog?
Thank you in advance.
Here is the code:
In the CFormView I have a button that creates the CDialog:
void CTest4View::OnButton1()
{
m_dialog_calculator.Create(IDD_DIALOG1, GetDesktopWindow());
m_dialog_calculator.ShowWindow(SW_SHOW);
}
In the CDialog I have the bitmap put on the button in the InitDialog:
BOOL CCalculator::OnInitDialog()
{
CDialog::OnInitDialog();
if(!bitmapNew.LoadBitmap(IDB_BITMAP_NEW)){
MessageBox("problem in loadbitmap");
}
if(!m_button.SetBitmap(bitmapNew)){
MessageBox("problem in SetBitmap");
}
bitmapOpen.LoadBitmap(IDB_BITMAP_OPEN);
m_buttonOpen.SetBitmap(bitmapOpen);
//==========================
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
Upon further investigation, the problem seems to be in: m_button.SetBitmap(bitmapNew) since this returns FALSE. Can someone please help me?
Quick fix:
void CTest4View::OnButton1()
{
// only create the dialog once
if (m_dialog_calculator.m_hWnd==NULL)
m_dialog_calculator.Create(IDD_DIALOG1, GetDesktopWindow());
m_dialog_calculator.ShowWindow(SW_SHOW);
}
Additional information 1:
The information that OP provided in his question/code is quite little, so I actually have to recreate a similar test project to guess what is wrong with the missing bitmap. What I found is that the CDialog and CBitmap is being created multiple times when the button is pressed, this causes subsequent creation api call to fail, other than the first creation call. The result is some unexpected behavior as you can see now.
The program is supposed to generate some assertion errors when run in debug mode due to the creation failure but I guess the OP compiled it in release mode so all warnings are being suppressed.
The problem occurred because the calculator dialog is created as a modeless dialog as compared to a normal DoModal way of activation. The usual way of doing such modeless dialog is to create the dialog only once, by monitoring the m_hWnd member variable. When the user want the dialog to be dismissed, it is simply being hidden instead of being destroyed. This will avoid all the multiple creation problems altogether.
I guess presently, the calculator dialog is assumed to be closed and destroy by clicking the "X" button on the top right of dialog, well, actually it is only hidden but not actually being destroyed by the default handling of CDialog. The correct way to dismiss the modeless calculator dialog is thus to overide the OnClose event to hide it using ShowWindow(SW_HIDE). To activate it again, use ShowWindow(SW_SHOWNORMAL).

How hide dialog MFC?

I need to create dialog and button.When I click on button with arrow dialog hide, it should looks like dialog moves to top and in ahother case shows for user.
ShowWindow(SW_HIDE). I know about it. I guess to get CRect from window, change it and after that set it to ScreeToClient().
Probably somebody knows some examples with it or another ways?
thanks
To hide or show a window, you use ShowWindow() as you indicated. No rectangle needed.
Otherwise, please re-explain your question, I can't make head or tails of it.
Normally, if you need to show a dialog that contains settings (similar to the Visual Studio "Options" dialog) then what you would typically do is this:
List item.
Create a CDialog-derived class.
Create an instance of that class.
call DoModal on that object. This shows the dialog.
Do nothing else. Once DoModal returns the dialog has been closed.

How to prevent closing QMessageBox after clicking a button

I have 3 buttons on QMessageBox added by QMessageBox::addButton() method. Is it possible to prevent closing the message box if a button has been clicked? By default every button closes the window, but I don't want to do it for one button.
One interesting way to approach it that worked for me is to completely disconnect the signals for the target button created, and then re-add the intended functionality. This won't work for everyone, especially if the button isn't created this way and/or you still want to close the dialog correctly. (There might be a way to add it back and/or simulate the behavior with QDialog::accept, QDialog::reject, QDialog::done - haven't tried yet.)
Example:
QMessageBox *msgBox = new QMessageBox(this);
QAbstractButton *doNotCloseButton = msgBox->addButton(tr("This button will not close anything"), QMessageBox::ActionRole);
// Disconnect all events - this will prevent the button from closing the dialog
doNotCloseButton->disconnect();
connect(doNotCloseButton, &QAbstractButton::clicked, this, [=](){ doNotCloseButton->setText("See? Still open!"); });
If you can get a pointer to the QMessageBox widget, you can try to install a QObject::eventFilter on it which filters the QEvent::Close.
Just had the same problem but I wanted to add a checkbox and it kept closing the dialog on clicked even with the ButtonRole set to QMessageBox::ActionRole (tried others too). For this scenario I just called blockSignals(true) on the QCheckBox and now it allows check/uncheck behaviour without closing the dialog. Luckily QCheckBox works fine without signals but assume you want a signal from your button.
They should likely add a new role that doesn't close the dialog as it's a pain to derive a class for simple customizations.
I looked through the addButton() functions overloads, but there is no custom behavior for the buttons you add with this method. They will behave like the standard buttons on a messagebox should.
However if you want to create a fully customizable dialog, then your best option is to extend the QDialog class and use whatever controlls you like on it.
Thanks to #Albert's Answer, I found that this also possible in python:
messagebox = QMessageBox()
button = QPushButton("This button will not close anything")
messagebox.addButton(button, QMessageBox.ButtonRole.NoRole)
button.disconnect()