在Dialog中访问控件的问题 [英] Problem with accessing controls in Dialog

查看:110
本文介绍了在Dialog中访问控件的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



很抱歉昨天重新发布了一个问题,我在我的页面中找到了回复链接,所以只有我去了那个选项。



请大家明白我的疑问。我有一个对话框类(CSampleDlg),我拖放一个ListControl(m_gridOutput)和一个编辑控件。我添加了一个派生自CSampleDlg的类(CDerivedOne)。



在listcontrol中,我有CheckBox项目。现在我试图通过函数调用检查/取消选中Derived类中的Listcontrol项,如下所示。

Hi everyone,

Sorry for the repost of a question yesterday, I dint find the 'reply' link in my page, so only i went for that option.

Kindly glarify my doubt.I have a dialog class(CSampleDlg), in that I drag and drop, a ListControl(m_gridOutput) and a edit control. And I've added one class(CDerivedOne) derived from CSampleDlg.

In listcontrol, i have CheckBox items. Now i am trying to Check/Uncheck the Listcontrol item from Derived class through function call, like this.

m_gridOutput.SetCheck(Id,TRUE);



但是我现在得到断言错误。断言错误来自以下行(在SetCheck函数中),


But i am getting assertion error at this point. The assertion error comes at the follwing line(in SetCheck function),

ASSERT(::IsWindow(m_hWnd));



类似地,如果我尝试从派生类访问我的Dialog中的任何控件,结果将是相同的。但是如果我尝试在我的对话框类中访问,则没有断言。



我该怎么做,从任何课程访问我的对话框中的控件,我在这里缺少什么?



非常感谢任何帮助。



Shiva。


Similarly if i try to access any control in my Dialog from derived class, the result will be the same.But there is no assertion, if i try to access within my dialog class.

What should i do, to access the controls in my dialog from any class, What i am missing here?

Any help is greatly appreciated.

Shiva.

推荐答案

如果 ASSERT(:: IsWindow(m_hWnd) ))失败,这意味着您尝试访问的对话框(或控件)尚未创建。



通常使一个类继承现有的 CDialog 一个很棘手。我想问几个问题:

- CSampleDlg 一个完整的功能对话框?我的意思是你可以使用 DoModal 显示它而没有问题吗?

- 为什么 CDerivedOne 继承来自 CSampleDlg

- >你使用不同的资源 CDerivedOne

- >或者这个类只是为了在没有修改UI的情况下将一些方法和成员添加到 CSampleDlg



你能发帖吗? .h文件和.cpp文件(至少是构造函数, DoDataExchange OnInitDialog ,以及像<$的宏c $ c> BEGIN_MESSAGE_MAP , DECLARE_DYNAMIC ,...)





----------------



我从您的评论中读取您的代码,问题很简单修复:



If ASSERT(::IsWindow(m_hWnd)) failed, it means that the dialog (or the control) you are trying to access was not created yet.

It is often tricky to make a class inherit from an existing CDialog one. I would like to ask several questions:
- Is CSampleDlg a fully functionnal dialog? I mean can you display it using DoModal without problem?
- Why does CDerivedOne inherit from CSampleDlg?
--> do you use a different resource for CDerivedOne?
--> or is this class just to add some methods and members to CSampleDlg without modifying the UI?

Could you post the .h files and the .cpp files (at least the constructors, DoDataExchange, OnInitDialog, and macros like BEGIN_MESSAGE_MAP, DECLARE_DYNAMIC, ...)


----------------

I read your code from your comments and the problem is easy to fix:

void CMlkOne::ChangeVariableValue(int VarId, int VarValue)
{
    CSampleDialogLogicDlg objBaseDialog;
    objBaseDialog.ChangeCheckStatus(2); // Assertion Error bcos of this line
}





这里有一个断言因为即使你实例化了对话框对象,

对话框资源尚未创建(例如,在 DoModal 被调用后创建)。

此外,你想要的不是实现另一个对象,而是调用基本方法。

所以只需用这个替换你的代码:





You have an asssertion here because even though you instanciated the dialog object,
the dialog resource is not created yet (it is created after DoModal is called for example).
Moreover, what you want here is not instanciating another object, but calling the base method.
So just replace your code by this one:

void CMlkOne::ChangeVariableValue(int VarId, int VarValue)
{
    //call the base class method
    CSampleDialogLogicDlg::ChangeCheckStatus(2);
}







-------------- ---------



湿婆,我刚刚看到一个我以前没见过的错误:你覆盖了 OnInitDialog in CMlkOne 这样的类:




-----------------------

Shiva, I just saw an error I hadn't seen before: you overrided OnInitDialog in CMlkOne class like this:

BOOL CMlkOne::OnInitDialog()
{
    return TRUE;
}



这不行。所有初始化都在 OnInitDialog 中完成,并且使用您的代码,您只需绕过初始化,这样就不会创建对话框!

您必须调用基本方法:


This can't work. All initialization is done in OnInitDialog and with your code, you just by-pass the initialization so your dialog is not created!
You must call the base method:

BOOL CMlkOne::OnInitDialog()
{
    return __super::OnInitDialog();
}



如果你想在 OnInitDialog中调用 ChangeVariableValue ,这样做:


And if you want to call ChangeVariableValue in OnInitDialog, do like this:

BOOL CMlkOne::OnInitDialog()
{
    //initialize the dialog first
    __super::OnInitDialog();
    //now you can call this because all your controls are initialized
    ChangeVariableValue(2);
    return TRUE;
}





我希望这次很清楚。



I hope it is clear this time.


你在哪里?从<?c $ c> m_gridOutput.SetCheck(Id,TRUE)调用?如果我不得不猜测发生了什么,那么当你还没有创建窗口时,你会尝试这样做。如果您有一个CDialog派生类,所有GUI初始化应该在 OnInitDialog()调用而不是构造函数上进行。出于同样的原因,当从拥有 CDialog 的类访问这些变量时,仍然必须先创建窗口,然后才能访问它们。



这些控件通常是私有成员,不允许您从派生类或实际所有者以外的任何其他类访问它们,这是为了防止您访问这些控件时窗口不存在,就像你的情况一样。如果您真的知道自己在做什么,可以让他们 protected 从派生类访问或 public 访问从任何地方,但我只建议你,如果你足够了解MFC。
Where are you calling m_gridOutput.SetCheck(Id,TRUE) from? If I had to guess what's happening, you're trying to do this at some point when the window hasn't been created. If you have a CDialog derived class, all GUI initialization should take place on the OnInitDialog() call and NOT the contructor. By the same token, when accessing these variables from a class who owns the CDialog, the window still has to be created before any of them can be accessed.

Those controls are typically private members, not allowing you to access them from a derived class or any other class other than the actual owner, this is meant to keep you from accessing those controls when the window doesn't exist, as is your case. If you REALLY know what you're doing, you can make them protected to access from derived classes or public to access from anywhere, but I'd only recommend that if you know MFC well enough.


请发布 void CDerivedOne :: DoDataExchange(CDataExchange * pDX)的代码此处的功能:)


这篇关于在Dialog中访问控件的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆