Click here to Skip to main content
15,886,007 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hai All

I am developing MFC based SDI application in VC++ derived from CFormView class.In my dialog I have a checkbox. When this check box is clicked or not I want to get the state of this check box in main class say CDemoView.cpp and use that particular state for some calculations in another class say OServer.cpp which is a C++ class. I tried using SetCheck(), GetCheck() functions and I am failing.How can I get the state of checkbox in my other class??

I used the following code to get pointer to my view class from OServer.cpp:
//get pointer to MDI frame
	CMDIFrameWnd *pFrame = (CMDIFrameWnd*)AfxGetApp()->m_pMainWnd;

	// Get pointer to the active MDI child window.
	CMDIChildWnd *pChild = (CMDIChildWnd *) pFrame->GetActiveFrame();

	// or CMDIChildWnd *pChild = pFrame->MDIGetActive();

	// Get the active view attached to the active MDI child window.
	pView = (CDemoView *) pChild->GetActiveView();


In my view class I used the following code to check the state of checkbox

void CDemoView::OnBnClickedEnableLog()
{
	checked = ::IsDlgButtonChecked(m_hWnd, IDC_ENABLE_LOG);//check if the check box is checked

	if( checked == 1)
		IsSetLog = true;
	else
		IsSetLog = false;

	UpdateData(FALSE);
}


Here the variables are declared in following manner

UINT checked;
bool IsSetLog;


I am using the state of this checked variable in OServer.cpp as follows.

if(pView->checked)
	{
		if(pView->IsSetLog > 0)
		LogResponse(strData);
	}


While debugging I am getting pView->checked value as the same value as in view class. But in OServer.cpp class the function LogResponse is not working for both cases i.e control doesnot gets into this if block

What is probably the problem? If I am wrong anywhere please help me with code portions..

Thanks..
Posted
Comments
Richard MacCutchan 23-Oct-13 9:56am    
Why do you need two variables when all you want to do is test whether the checkbox is checked, and if so, call ther log handler?
Lekshmi KR 24-Oct-13 0:20am    
Actually I just need the variable checked to determine the state of check box. But to confirm the state I used another variable IsSetLog also.Where am I wrong..Please help me with code portions if my code is wrong some where..Thank you
Richard MacCutchan 24-Oct-13 3:08am    

checked = ::IsDlgButtonChecked(m_hWnd, IDC_ENABLE_LOG);
if( checked == TRUE)
LogResponse(strData);
Lekshmi KR 24-Oct-13 4:46am    
But LogResponse function is in the other class OServer.cpp and how can i access that function in my view class?? I need to use a pointer or what??
Richard MacCutchan 24-Oct-13 4:55am    
Yes, I would expect you need to use a pointer, or call some static method. Maybe you should look at your design to see how this can be best achieved.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900