如何使父窗口和子窗口都可访问 [英] How to make parent window and child window both are accessible

查看:113
本文介绍了如何使父窗口和子窗口都可访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的父窗口包含代码



My Parent window contains code

Curation.frmMultiplePARs objMultiPARs = new Curation.frmMultiplePARs();
                   objMultiPARs.SelectedTANInfo = SelectedTANInfo;
                   objMultiPARs.dtFilter = dgvPARs.DataSource as DataTable;

                   objMultiPARs.Show();
                   //objMultiPARs.ShowDialog(this);
                   if (objMultiPARs.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                   {
                       TANInfo objTANInfo = new TANInfo();
                       SelectedTANInfo.PAR_Text = new List<string>(objMultiPARs.LstNewPAR);
                       SelectedTANInfo.PAR_Rtf = new List<string>(objMultiPARs.LstNewPAR_RTF);





它将打开子窗口但子窗口仅在活动模式下。一次儿童窗口关闭父窗口将激活



我想同时访问两个窗口



我实施childform Actived方法





it will open child window but child window only in active mode.Once child window closes parent window will get active

I want both windows access at same time

I implemented childform Actived method

private void frmMultiplePARs_Activated(object sender, System.EventArgs e)
{
    if (this.Owner != null)
    {
        this.Owner.Enabled = true;
    }
}





但是所有者/父母正在变空



任何建议



but owner/parent am getting null

Any suggestions

推荐答案

创建拥有表单是实现辅助窗口的好方法,当它们可见时,始终保持在前面你的主要表格。使用拥有的窗口的例子是搜索和替换对话框,颜色选择器等。



这是一个我认为有用的设计模型: br $>


1.在主窗体中创建辅助窗体的实例,设置其所有者属性,并将'FormClosing EvenHandler连接到它:
Creating "owned" Forms is a good way to implement secondary windows that will, when they are visible, always stay in front of your Main Form(s). Examples of use of owned windows are search-and-replace dialog boxes, color-pickers, etc.

Here's a "design model" I've found useful:

1. Create an instance of the secondary Form in your Main Form, set its 'Owner Property, and wire-up a 'FormClosing EvenHandler to it:
// declare the Secondary Form instance in Form scope
private SecondaryForm secForm;

private void MainForm_Load(object sender, EventArgs e)
{
    secForm = new SecondaryForm();
    secForm.Owner = this;
    secForm.FormClosing += secForm_FormClosing;

    // install Main Form 'FormClosing EventHandler
    // see #2 below
    this.FormClosing += secForm_FormClosing;
}

private void secForm_FormClosing(object sender, FormClosingEventArgs e)
{
    secForm.Hide();
    e.Cancel = true;
}

此时,您在WinForms中遇到了所有者/拥有关系的一个有趣的副作用:如果您尝试关闭主窗体,它将不会关闭,因为:它不能关闭中学表格!



2.此问题的解决方法是为主表单定义'FormClosing事件:

At this point, you encounter an interesting side-effect of the Owner/Owned relationship in WinForms: if you try and close the Main Form, it will not close, because: it cannot close the Secondary Form !

2. The fix for this problem is to define a 'FormClosing Event for the Main Form:

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    secForm.FormClosing -= secForm_FormClosing;
    secForm.Close();
    e.Cancel = false;
}

在这个EventHandler中,我们从SecondaryForm的实例中删除'FormClosing EventHandler,并关闭它,然后应用程序可以通常的方式关闭。



以上示例中缺少的(显然)是使二级表格可见的代码('显示,或将'可见属性设置为'真实)。您可能希望以各种方式实现,具体取决于您的应用程序。

In this EventHandler, we remove the 'FormClosing EventHandler from the instance of the SecondaryForm, and close it, and then the Application can close in the usual way.

What's (obviously) missing from the above example is the code to make the secondary Form visible ('Show, or set 'Visible Property to 'true). You may wish to implement that in various ways depending on your Application.


创建子窗口时,通过调用Show显示它,而不是ShowDialog。

但在调用Show方法之前,您可能希望为FormClosed或FormClosing添加事件处理程序,并获取结果(即DialogResult属性,并根据其值存储的一些值)事件处理程序中该子窗口的子窗口的公共/内部属性。根据基本要求,建议在子窗口的父窗口中使用memeber变量,以便确保一次只能使用该类型的一个子窗口。
When you create the "child window", show it with a call to Show, not ShowDialog.
But before you call the Show method, you might want to add event handlers for FormClosed or FormClosing, and get the "result" (i.e. DialogResult property, and depending on its value some values stored in public/internal properties of the child window) of that child window in the event handler. Depending on the underlying requirements, it could be advisable to use a memeber variable in the parent window for the child window such that you can ensure that only one child windows of that type is available at a time.


这篇关于如何使父窗口和子窗口都可访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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