Jump to content
Xtreme .Net Talk

Recommended Posts

Posted
Every example I've seen of mdi uses "this" as the parent. However, I've created a treeview component that I place on numerous child forms as needed. When the user clicks the a node it should open another child and I just can't get it to work. Keep in mind I want the events code in the component so this will not work.
  • *Experts*
Posted

You could try a number of approaches, two common ones come to mind:

1. Use the control's FindForm method to get it's hosted form then use that form's MdiParent property.

2. Keep a static reference to the MDIForm in a "global" type of class. This works well even when you use separate assemblies IF all assemblies know about the "global" class.

 

For #1, use something like:

// Event handler for the treeview, in some common class
private void Tree_Click(object sender, ...)
{
Control ctrl = sender as Control;
if(ctrl==null) return; // Not a control? Hopefully shouldn't happen

// Get the form the control is on. Use this form's
// MdiParent property below
Form parent = ctrl.FindForm();

Form2 frm = new Form2();
frm.MdiParent = parent.MdiParent;
frm.Show();
}

 

For option #2, in Main (if that's where you create your MdiForm), you can set a static reference to the variable before showing it.

Suppose you had a class like this:

public sealed class GlobalVars
{
public Form MDIForm;
}

 

In Main, you might have code like this:

static void Main() 
{
frmMainForm frm = new frmMainForm();
GlobalVars.MDIForm = frm;
Application.Run(frm);
}

 

Then wherever you need to get to the MDI form, you can use it like a global variable:

// Event handler for the treeview, in some common class
private void Tree_Click(object sender, ...)
{
Form2 frm = new Form2();
frm.MdiParent = GlobalVars.MDIForm;
frm.Show();
}

 

-Nerseus

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Posted

Thanks a bunch! I think I'll go with the first one

You could try a number of approaches, two common ones come to mind:

1. Use the control's FindForm method to get it's hosted form then use that form's MdiParent property.

2. Keep a static reference to the MDIForm in a "global" type of class. This works well even when you use separate assemblies IF all assemblies know about the "global" class.

 

For #1, use something like:

// Event handler for the treeview, in some common class
private void Tree_Click(object sender, ...)
{
Control ctrl = sender as Control;
if(ctrl==null) return; // Not a control? Hopefully shouldn't happen

// Get the form the control is on. Use this form's
// MdiParent property below
Form parent = ctrl.FindForm();

Form2 frm = new Form2();
frm.MdiParent = parent.MdiParent;
frm.Show();
}

 

For option #2, in Main (if that's where you create your MdiForm), you can set a static reference to the variable before showing it.

Suppose you had a class like this:

public sealed class GlobalVars
{
public Form MDIForm;
}

 

In Main, you might have code like this:

static void Main() 
{
frmMainForm frm = new frmMainForm();
GlobalVars.MDIForm = frm;
Application.Run(frm);
}

 

Then wherever you need to get to the MDI form, you can use it like a global variable:

// Event handler for the treeview, in some common class
private void Tree_Click(object sender, ...)
{
Form2 frm = new Form2();
frm.MdiParent = GlobalVars.MDIForm;
frm.Show();
}

 

-Nerseus

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...