I've been coding a little program which works perfectly, but I decided to upgrade it and add a few checkboxes. Now since i've never made one before I'm having trouble and I can't seem to find anywhere that can solve my problem.

void TheAppDlg::OnFirstcheckbox()
{
	if(OnFirstcheckbox !=0)
	{
		m_cCharSet.SetWindowText(_T("123"));	
	}
	if (OnFirstcheckbox == 0)
	m_cCharSet.SetWindowText(_T("abc"));
}

Is pretty much the code I am using to see if it's checked or not, now it will know it's checked and that works, but unchecking it doesn't work it just sticks with 123. I've probably missed something stupidly easy out.

The variable name on line 3 is the same as the function name on line 1 -- bad idea. Read this article. Your program has to call one of the checkboxes methods to find out whether it was checked or not.

Comments
pointed out a few things and the users post helped me a lot

Thanks for pointing out the function and variable usage there, and thanks for the link I know exactly where I was going wrong.

I figured I'd reopen this because I still need help on just one thing, and to point out what helped me before for other people in case they need help and come across this thread (as just saying "oh I know what I was missing" doesn't help anyone).

So I figured out I needed this line:

int nCheck = ((CButton*)GetDlgItem(IDC_IDCNAMEHERE))->GetCheck();

Example:

int nCheck = ((CButton*)GetDlgItem(IDC_IDCNAMEHERE))->GetCheck();
  if (  nCheck == BST_CHECKED )
  {
	m_cCharSet.SetWindowText(_T("on"));
  }
  if ( nCheck == BST_UNCHECKED  )
  {
    m_cCharSet.SetWindowText(_T("off"));
  }

Now onto my other problem. Now it's not so much a problem, but a "how can I keep the code clean and not horribly messy", because doing it the long way works fine, up until it gets really messy and it's hard to read your own code and figure out what you have and haven't done.

Problem example:

if ( nCheck && nCheck2 && nCheck3 && nCheck4 == BST_CHECKED )
   {
	 m_cCharSet.SetWindowText(_T("1234on"));
   }

      if ( nCheck && nCheck2 && nCheck3 == BST_CHECKED && nCheck4 == BST_UNCHECKED )
   {
	 m_cCharSet.SetWindowText(_T("123on 4 off"));
   }

   if ( nCheck && nCheck2 && nCheck4 == BST_CHECKED && nCheck3 == BST_UNCHECKED )
   {
	 m_cCharSet.SetWindowText(_T("124on 3 off"));
   }

    if ( nCheck && nCheck3 && nCheck4 == BST_CHECKED && nCheck2 == BST_UNCHECKED )
   {
	 m_cCharSet.SetWindowText(_T("134on 2 off"));
   }
   
    if ( nCheck2 && nCheck3 && nCheck4 == BST_CHECKED && nCheck == BST_UNCHECKED )
   {
	 m_cCharSet.SetWindowText(_T("234on 1 off"));
   }

Is there any other way I can check to see if any other checkboxes are checked when for example checkbox1 and checkbox2 is checked? Because after a while it gets pretty messy, and putting if statements for every combination there is doesn't exactly seem the best way to go about it as it kind of gets messed up after a while and doesn't work as planned, although that could be a mistake on my part.

Edited 6 Years Ago by _siren2: n/a

>>if ( nCheck && nCheck2 && nCheck3 && nCheck4 == BST_CHECKED )

I hope you know that is the same thing as this: if ( nCheck != 0 && nCheck2 != 0 && nCheck3 != 0 && nCheck4 == BST_CHECKED )

This article has been dead for over six months. Start a new discussion instead.