how to save radio button status? - mfc

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).

Related

MFC DDX_Radio causes debug assertion failure when DoDataExchange is called (dlgdata.cpp Line 286)

I have a dialog with radio button with groups of 4 buttons.
DDX_Radio(pDX, D_RADIO_GROUPLEAD, intToStore) in DoDataExchange is there for saving and loading.
It works perfectly fine.
Selection changed, DoDataExchange called, stores and loads data no problem.
Problem occurs when I hide one of the radio button (hide it via ShowWindow(SW_HIDE). Let's just call it 3rd button for reference. And previously, the selection was this 3rd button.
I have logic that will automatically select the default one (one with Group flagged as true in the editor). I call the button's SetCheck(1).
Visually everything seems to be working.
However, when I click on 2nd button, then try to call DoDataExchange (hence DDX_Radio), it will causes debug assertion failure. (dlgdata.cpp Line 286)
AND
the data is not properly populated back in intToStore.
Why could this be and how can I avoid this issue?
Thank you.
The problem is, that the auto radio button stuff in Windows skips buttons that are disabled. In detail. You click on button 2 while button 3 is selected and disabled. Button 2 gets selected but button 3 is not unchecked.
The next problem occurs when DoDataExchange runs. It doesn't check if a button is enabled or disabled. DDX_Radio just loops over all radio buttons, and it find 2 buttons in the group are enabled. This causes the ASSERT. DDX_Radio don't care if a button is enabled or disabled.
My advice: Use a custom OnClick handle by yourself, and disable all other buttons manually.

MFC radio button group message handler

I've got two little questions at Visual Studio MFC-GUI programming technique regarding the handling of a group of radio buttons in VS2015 CE.
I have a little dialog based application. This application draws some lines on my dialog with a specified pen.
Now I made a group of radio buttons to be able do use different colors for the pen. So I created a group box, placed the radio buttons into this group box, enabled the group property of the first radio button and checked the tab sequence so that all of the radio buttons are properly in sequence.
That's ok - testing the GUI I'm able to select just one of the radio button as planned, because I wouldn't be able to draw a line in to different colors at the same time. Now I added an handler for the BN_CLICKED Message of the first radio button object in the ClassWizard to add the selection of different colored pens.
Now comes the interesting part. This handler is executed only when i click the first radio button. The one with group property enabled. I thought it should be executed whenever I click any of the radio buttons in this group. The next thing I tried was to add an BN_CLICKED-Messagehandler to all of those radio buttons, but the ClassWizard does not shows the BN_CLICKED Message for the other radio buttons - only for the one with group proerty enabled.
After googling around I read a post that one would be able to add an BN_CLICKED-Messagehandler for an button by simply double clicking it in the Dialog. So I double clicked all of my radio buttons and added an invocation of the BN_CLICKED-Messagehandler of the first radio button. That's working as a workaround so that now my line is drawn in the different colors whenever I click on any of those radio buttons (black, blue, red , and so on).
I have two questions - is this behaviour intended? I suppose the reason why I'm only able to add a BN_CLICKED-Messagehandler in the ClassWizard for the first radio button is, that it would be invoked by any of the radio buttons in my group, wouldn't it?
Another question is, I would have preferred to write the handler once and be able to tell the class wizard to use this particular handler instead of creating a unique handler for each of the buttons. That's independent of this radio buttons. I tested this with a simple application which consists of an assembly of several buttons - which can have their own BN_CLICKED-Messagehandlers attached by the class wizard. When I didn't accept the automatically generated functionname from the wizard and enter the name of my own handlerfunction it simply states that it wouldn't be able to overwrite it. How can I select an existing handler for an object message in the class wizard or is that not possible and I have to do this manually in code for myself?
Hope there's an VS2015 MFC Guy out there be able to tell me ..
This is indeed by design. You could have wanted different behaviour for each button click - if you don;t simply associate all of the BN_CLICKED to the same message handler and delete the ones that were automatically generated.
You can also use ON_CONTROL_RANGE(BN_CLICKED, id1, id2, memberFxn ) - make sure your buttons are in the same range in your resource.h
Hope that helps!

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)

Radio buttons enabled, user cannot change the value

Is it possible to have enabled radio buttons though the user cannot change the value?
It is possible. If you create your radio buttons with the BS_RADIOBUTTON style instead of BS_AUTORADIOBUTTON then Windows will not automatically change the selection when the user clicks a radio button. (In the dialog editor in Visual Studio, right click the radio button and set the Auto property to False.)
Read the "Using Radio Buttons" section of this page in MSDN for more information.
either you can setchek it to BST_CHECKED and the EnableWindow to FALSE
or..
on every other selection of radio button make the radio button that you need selected

How to hide a modal dialogbox in MFC application?

I have a hard time hiding a modal dialog box. What I am doing is - I am trying to design a UI for my own application in MFC, kind of a setup assistant.
In the 1st dialog box I have NEXT button, so when I click that it has to hide the 1st dialog box and move on to the 2nd dialog box, where I have some controls in 2nd dialog box.
How can I achieve that?
I have never tried to hide a Modal dialog...not sure how it can be done.
Anyway, it seems to me you don't need to hide the dialog but destroy the first one and create the second one. You can use EndDialog to terminate a modal dialog.
But MFC has its own mechanism to create your own wizard, have a look at this class CPropertySheet. I am sure you can find thousand of examples, this is one.
Hope it helps.
You can use ShowWindow() function to hide modal
Its default patametet is SW_SHOW which is equal true value 1 and
To hide modal use SW_HIDE value when you click next button
You just use ShowWindow(SW_HIDE) If you make prev button you should use modal pointer
Or next modal should child modal because you cannot have prev modal variable.
I wish you understand me for my english