0

I am trying to check a radio button, I am new to c++ and MFC so after some search I wrote :

INT m_nIndex;

CButton* pButton1 = (CButton*) GetDlgItem(IDC_RADIO1);
CButton* pButton2 = (CButton*) GetDlgItem(IDC_RADIO2);

pButton1->SetCheck(m_nIndex == 0);
pButton2->SetCheck(m_nIndex == 1);

    if (pButton1.IsChecked){ //Here pButton1 shows an error saying expression much have a class type
    }
  • What can I do to call the radioButton?

  • Another Question, how to put a radioButton checked by default?

demonplus
  • 5,613
  • 12
  • 49
  • 68
user3552658
  • 115
  • 1
  • 11
  • Isn't pButton1->SetCheck(m_nIndex == 0); calling the radio button? – Seth Kitchen Oct 29 '15 at 21:12
  • 1
    `pButton` is a pointer type, that needs to be dereferenced before you can access its members. Either use `(*pButton1).IsChecked` or `pButton1->IsChecked` (assuming that `IsChecked` is a class member). Apart from that, [CButton::SetCheck](https://msdn.microsoft.com/en-us/library/31deb72x.aspx) expects an integer argument. Trying to learn C++ with MFC isn't going to end well. Learning MFC without a solid understanding of C++ isn't going to end well either. – IInspectable Oct 29 '15 at 21:14
  • [The Definitive C++ Book Guide and List](http://stackoverflow.com/q/388242/1889329). – IInspectable Oct 29 '15 at 21:16
  • So you advice me to learn c++ without MFC first? – user3552658 Oct 29 '15 at 21:17
  • http://tinypic.com/r/34zhkbc/9 Not working @ IInspectable – user3552658 Oct 29 '15 at 21:20
  • That's hardly surprising. It's certainly not one of the options I suggested. – IInspectable Oct 29 '15 at 21:25

2 Answers2

1

The if is solved by this:

    if (IsDlgButtonChecked(IDC_RADIO1))
    {

    }

I just call SetCheck in my initializer to set up my radio buttons. Not sure if this is what you mean by default

Ajay
  • 18,086
  • 12
  • 59
  • 105
Seth Kitchen
  • 1,526
  • 19
  • 53
  • Thank You .. It helps me in the if statement.. But what I meant in default that when runnig I want the radiobutton1 be pressed – user3552658 Oct 29 '15 at 21:25
0

To set the default radio button use the following method of CWnd class:

CheckRadioButton(int nIDFirstButton, int nIDLastButton, int nIDCheckButton);

Also I'd suggest using pButton->GetCheck() to get the current radio button state. You can also use it for check boxes.

Andrew Komiagin
  • 6,446
  • 1
  • 13
  • 23