请问在一个类里边怎么调用另一个类的控件变量?

时间:2021-02-19 20:05:21
在CDesignDlg里有一个列表控件IDD_RULELIST,变量为CListCtrl m_rulelist,
我要在另一个类CRuleDlg里怎么调用才能得到IDD_RULELIST里的数据呢?

我在CRuleDlg里定义CDesignDlg dlg;然后用dlg.m_rulelist.GetItemCount();
为什么编译能通过,但在运行的时候会出错呢?

谢谢!

10 个解决方案

#1


因为你的dlg并未创建对话框,所以会出错。

#2


法律是公正的

#3


最好是把CDesignDlg定义成CRuleDlg的成员,否则你用模态对话框是很难得到这个值的

#4


我在CRuleDlg里定义CDesignDlg dlg;然后用dlg.m_rulelist.GetItemCount();
为什么编译能通过,但在运行的时候会出错呢?

因为你又重新定义了一个CDesignDlg,与你想要操作的CDesignDlg不是同一个,所以就算不出错,你也得不到想要的结果。
至于出错,那是因为你重新定义的CDesignDlg还未创建窗口,所以其中的m_rulelist也未创建,所以你对m_rulelist的操作都将失败。

#5


把CDesignDlg定义成CRuleDlg的成员

#6




CDesignApp theApp;

/////////////////////////////////////////////////////////////////////////////
// CDesignApp initialization

BOOL CDesignApp::InitInstance()
{
AfxEnableControlContainer();

// Standard initialization
// If you are not using these features and wish to reduce the size
//  of your final executable, you should remove from the following
//  the specific initialization routines you do not need.

#ifdef _AFXDLL
Enable3dControls(); // Call this when using MFC in a shared DLL
#else
Enable3dControlsStatic(); // Call this when linking to MFC statically
#endif

CDesignDlg dlg;
m_pMainWnd = &dlg;
int nResponse = dlg.DoModal();
if (nResponse == IDOK)
{
// TODO: Place code here to handle when the dialog is

//  dismissed with OK
}
else if (nResponse == IDCANCEL)
{
// TODO: Place code here to handle when the dialog is

//  dismissed with Cancel
}

// Since the dialog has been closed, return FALSE so that we exit the
//  application, rather than start the application's message pump.



return FALSE;
}

其他类用这种方法是可以的
但这个CDesignDlg是主类,在初始化的时候已经被定义成dlg了(如上所示),不知道怎么在其他类里调用这个dlg.

#7


CDesignDlg *pDlg=(CDesignDlg*)AfxGetMainWnd();
pDlg->m_pathlist.GetWindowText(a);

问题已解决,这分我就不送了!

#8


///问题已解决,这分我就不送了!

真牛X

#9


无语

#10


哈哈....

LZ我来晚了

#1


因为你的dlg并未创建对话框,所以会出错。

#2


法律是公正的

#3


最好是把CDesignDlg定义成CRuleDlg的成员,否则你用模态对话框是很难得到这个值的

#4


我在CRuleDlg里定义CDesignDlg dlg;然后用dlg.m_rulelist.GetItemCount();
为什么编译能通过,但在运行的时候会出错呢?

因为你又重新定义了一个CDesignDlg,与你想要操作的CDesignDlg不是同一个,所以就算不出错,你也得不到想要的结果。
至于出错,那是因为你重新定义的CDesignDlg还未创建窗口,所以其中的m_rulelist也未创建,所以你对m_rulelist的操作都将失败。

#5


把CDesignDlg定义成CRuleDlg的成员

#6




CDesignApp theApp;

/////////////////////////////////////////////////////////////////////////////
// CDesignApp initialization

BOOL CDesignApp::InitInstance()
{
AfxEnableControlContainer();

// Standard initialization
// If you are not using these features and wish to reduce the size
//  of your final executable, you should remove from the following
//  the specific initialization routines you do not need.

#ifdef _AFXDLL
Enable3dControls(); // Call this when using MFC in a shared DLL
#else
Enable3dControlsStatic(); // Call this when linking to MFC statically
#endif

CDesignDlg dlg;
m_pMainWnd = &dlg;
int nResponse = dlg.DoModal();
if (nResponse == IDOK)
{
// TODO: Place code here to handle when the dialog is

//  dismissed with OK
}
else if (nResponse == IDCANCEL)
{
// TODO: Place code here to handle when the dialog is

//  dismissed with Cancel
}

// Since the dialog has been closed, return FALSE so that we exit the
//  application, rather than start the application's message pump.



return FALSE;
}

其他类用这种方法是可以的
但这个CDesignDlg是主类,在初始化的时候已经被定义成dlg了(如上所示),不知道怎么在其他类里调用这个dlg.

#7


CDesignDlg *pDlg=(CDesignDlg*)AfxGetMainWnd();
pDlg->m_pathlist.GetWindowText(a);

问题已解决,这分我就不送了!

#8


///问题已解决,这分我就不送了!

真牛X

#9


无语

#10


哈哈....

LZ我来晚了