Click here to Skip to main content
15,891,597 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
My Parent window contains code

XML
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);


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

C#
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
Posted
Comments
Sergey Alexandrovich Kryukov 5-Mar-14 1:38am    
It all makes no sense at all. And you are mixing up Parent/Child relationship (effectively defunct for forms/windows, if you don't mean MDI which you don't want to use) and Owner/Owned (which is very important to use in most cases when you use non-modal multiple windows).
—SA

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:
C#
// 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;
}
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:
C#
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    secForm.FormClosing -= secForm_FormClosing;
    secForm.Close();
    e.Cancel = false;
}
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.
 
Share this answer
 
v4
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.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900