MFC CListBox.AddString()不显示字符串

卢卡什·科特里克

我正在创建MFC应用程序,并尝试在对话框类中添加列表框。当我使用调试配置时,它会出错并导致应用程序崩溃。当我使用发布配置时,该对话框会出现,但为空。

我刚刚开始,所以我的代码是基本的:

//code... (it is including #include "MyDialog.h") 

CMyDialog dialog; //CMyDialog includes public CListBox variable m_listBox
CString str;
str = L"Hello";
dialog.m_listBox.AddString(str);
dialog.DoModal();

//code...

但这还是行不通的。

在调试配置中,我收到以下消息:“调试断言失败!” 调试断言失败!

在发行版配置中,对话框为空(而不是带有“ Hello”的行):

MyDialog:

我的对话

该错误在调用AddString()函数时立即发生,即使在CMyDialog类中恰好调用了该函数时也会发生(例如,我尝试在构造函数中调用它)。

I would like to know what I am doing wrong, I suppose that the problem isn't in dialog class itself, but somewhere else. I tried to search through the internet, but I found no solution, so I am here :)

acraig5075

What you are doing wrong is trying to use a member (m_listBox) too early. Members that are dialog controls, as opposed to non-window types like a CString member, are only in a state to be used once the dialog has been invoked with DoModal().

Assertions are debug macros which is why you get no indication of the problem in Release configuration.

The correct place to populate a listbox control will typically be the OnInitDialog override member function. The dialog then has been invoked by that stage, and controls owned by the dialog will have window handles and can be used.

如果您特别想在调用对话框之前将字符串文字从对话框传递到对话框,则可以将CString作为对话框类的成员变量。这样的成员在构造类实例时就存在,并且可以通过构造函数或公共设置器或具有公共可访问性来设置。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章