MFC - Radio Buttons


Advertisements

A radio button is a control that appears as a dot surrounded by a round box. In reality, a radio button is accompanied by one or more other radio buttons that appear and behave as a group.

Here is the list of messages mapping for Radio Button control −

Message Map entry Description
BN_CLICKED ON_BN_CLICKED( <id>, <memberFxn> ) The framework calls this member function when is button clicked.
BN_DISABLE ON_BN_DISABLE( <id>, <memberFxn> ) The framework calls this member function when button is disabled.
BN_DOUBLECLICKED ON_BN_DOUBLECLICKED( <id>, <memberFxn> ) The framework calls this member function when button is double clicked.
BN_PAINT ON_BN_PAINT( <id>, <memberFxn> ) The framework calls this member function when an application makes a request to repaint a button.

Let us look into an example of Radio button by creating a new MFC dialog based application.

Step 1 − Drag a group box and three radio buttons and remove the Caption of Static Text control.

Radio Button

Step 2 − Add event handler for all the three radio buttons.

Radio Button

Step 3 − Add the Value variable for the Static Text control.

Radio Button

Step 4 − Here is the implementation of three event handlers.

void CMFCRadioButtonDlg::OnBnClickedRadio1() {
   // TODO: Add your control notification handler code here
   m_strTextControl = _T("Radio Button 1 Clicked");
   UpdateData(FALSE);
}

void CMFCRadioButtonDlg::OnBnClickedRadio2() {
   // TODO: Add your control notification handler code here
   m_strTextControl = _T("Radio Button 2 Clicked");
   UpdateData(FALSE);
}

void CMFCRadioButtonDlg::OnBnClickedRadio3() {
   // TODO: Add your control notification handler code here
   m_strTextControl = _T("Radio Button 3 Clicked");
   UpdateData(FALSE);
}

Step 5 − When the above code is compiled and executed, you will see the following output. When you select any radio button, the message is displayed on Static Text control.

Radio Button
mfc_windows_controls.htm
Advertisements