在 CDialog 类型的子窗体之间传递数据

发布于 2024-09-29 17:23:24 字数 2246 浏览 0 评论 0原文

我有一个 MS Visual C++ 2005 项目,我试图在其中创建一个主对话框,其中有一个部分专门用于显示可选择的子窗体对话框。每个子表单的大小相同,但布局不同。使用组合框控件执行选择。我搜索了实现此功能的最佳方法,并发现了有人在 1999 年开发的这个类,用于处理主对话框中的子对话框:

http://www.codeproject.com/KB/dialog/childdlg.aspx

进行额外修改:

http://www.codeproject.com/KB/dialog/childdlg.aspx?msg=1287#xx1287xx

代码有效非常适合选择和显示各种子表单,但我似乎无法将数据从一个子表单传递到另一个子表单。具体来说,每个子表单都由多个编辑控件组成。我希望用户能够在子窗体 #1 的编辑控件中输入一个值,并在子窗体 #2 的编辑控件或静态文本控件中回显该值。

我尝试通过使用子窗体对话框类(名为 CSubFormType 和 CDialog 的子类)的成员函数来实现此功能,该函数将在终止数据输入编辑控件(即 Box1a)的焦点时执行。

void CSubFormType::OnEnKillfocusBox1a(){

p2WndControl = (CWnd*)(GetDlgItem(IDC_Box1a));  //Get pointer to the control dialog box for data entry

//Inserted here some specific code used to place data entered into IDC_Box1a into extern wchar_t outstr[32]

SetDlgItemText (IDC_Box1b, outstr);  //Echo data entered in IDC_Box1a to IDC_Box1b on same Subform #1


SetDlgItemText (IDC_Box2, outstr);//Echo data entered in IDC_Box1a to IDC_Box2 on different subform, Subform #2

}

CSubFormType 直接调用 OnEnKillfocusBox1a 的消息映射为:

ON_EN_KILLFOCUS(IDC_Box1a, &CSubFormType:: OnEnKillfocusBox1a)

或间接调用:

ON_BN_CLICKED(IDOK, &CSubFormType::OnBnClickedOk) // CSubFormType ::OnBnClickedOk 然后调用 OnEnKillfocusBox1a

我遇到的问题是:

1) ON_BN_CLICKED(IDOK, &CSubFormType::OnBnClickedOk) 在将数据输入 Box1a 后按 Enter 键时不会被调用。相反,主对话框的 OnBnClickedOk 成员函数被调用。

2) 当 CSubFormType::OnEnKillfocusBox1a 确实被执行时(通过鼠标单击其他地方),它能够将 Box1a 中输入的值回显到同一子窗体 #1 上的 Box1b,但不会将值回显到子窗体 #2 上的 Box2。

任何关于我需要做什么来解决这些问题的指导或更好的方法来实现相同的通用功能将不胜感激。

正在使用的主对话框的组合框选择成员函数:

void CMain::OnCbnSelchange() { 整数选择;

selection = ((CComboBox*)GetDlgItem(IDC_Select))->GetCurSel();

switch(selection)
{
    case 0: // Select Subform 1
        //Do nothing
        break;

    case 1: // Select Subform 2
        SetDlgItemText (IDC_Box2, outstr); //Set Box2 text on Subform #2
        break;
 }

m_SubForms.ShowSubForm(selection);

}

I have a MS Visual C++ 2005 project where I am trying to have a main dialog box with a section devoted to displaying selectable subform dialogs boxes. Each subform will be of the same size but have a different layout. The selection is performed using a combo-box control. I searched on the best way to implement this functionality and I came across this class that someone developed in 1999 for handling child dialogs within a main dialog:

http://www.codeproject.com/KB/dialog/childdlg.aspx

With the additional modification:

http://www.codeproject.com/KB/dialog/childdlg.aspx?msg=1287#xx1287xx

The code works quite well for selecting and displaying the various subforms but I don’t seem able to pass data from one subform to another. Specifically each subform is comprised of several edit controls. I would like to have the user be able to enter a value in an edit control on Subform #1 and for this value to be echoed in an edit control or static text control on Subform #2.

I tried implementing this by using a member function of the subform dialog class (named CSubFormType and a subclass of CDialog) that would execute upon killing the focus to the data input edit control (i.e. Box1a).

void CSubFormType::OnEnKillfocusBox1a(){

p2WndControl = (CWnd*)(GetDlgItem(IDC_Box1a));  //Get pointer to the control dialog box for data entry

//Inserted here some specific code used to place data entered into IDC_Box1a into extern wchar_t outstr[32]

SetDlgItemText (IDC_Box1b, outstr);  //Echo data entered in IDC_Box1a to IDC_Box1b on same Subform #1


SetDlgItemText (IDC_Box2, outstr);//Echo data entered in IDC_Box1a to IDC_Box2 on different subform, Subform #2

}

The message map for CSubFormType meant to call OnEnKillfocusBox1a directly is:

ON_EN_KILLFOCUS(IDC_Box1a, &CSubFormType:: OnEnKillfocusBox1a)

Or indirectly by:

ON_BN_CLICKED(IDOK, &CSubFormType::OnBnClickedOk) // CSubFormType::OnBnClickedOk then calls OnEnKillfocusBox1a

The problems I am having are:

1) ON_BN_CLICKED(IDOK, &CSubFormType::OnBnClickedOk) does not get called upon hitting Enter key after typing data into Box1a. Instead the OnBnClickedOk member function of the main dialog box gets called instead.

2) When CSubFormType::OnEnKillfocusBox1a does get executed (by mouse click elsewhere) it is able to echo the value entered in Box1a to Box1b on the same Subform #1 but will not echo the value to Box2 on Subform #2.

Any guidance as to what I need to do to resolve these problems or a better approach to implementing the same general functionality would be greatly appreciated.

Combo box selection member function for main dialog box being used:

void CMain::OnCbnSelchange()
{
int selection;

selection = ((CComboBox*)GetDlgItem(IDC_Select))->GetCurSel();

switch(selection)
{
    case 0: // Select Subform 1
        //Do nothing
        break;

    case 1: // Select Subform 2
        SetDlgItemText (IDC_Box2, outstr); //Set Box2 text on Subform #2
        break;
 }

m_SubForms.ShowSubForm(selection);

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

羅雙樹 2024-10-06 17:23:24

当主组合框选择更改时同步所有表单。您尝试在文本框文本更改时立即执行此操作,但没有必要,因为只有一种表单可见。

Synchronize all forms when main combobox selection is changed. You try to make this immediately when textbox text is changed, but it is not necessary because only one form is visible.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文