Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

How do I set the mdiparent of a child of a MDIChild?

 

Scenario:

 

MDIParent calls a form. The .mdiparent is set to this. works great.

now, on the MDIChild another button is pressed to show another form which is also supposed to be a child of the original MDI.

 

Here is my code from the mdichild (frmListofUsers):

 

//new instance

frmUsers fmUserDetail = new frmUsers();

 

//set the top and left properties so the window is nicely centerred

fmUserDetail.Top = ((this.Height - fmUserDetail.Height ) / 2) + 50;

fmUserDetail.Left = (this.Width - fmUserDetail.Width ) /2;

fmUserDetail.Show();

 

Now once the processing on fmUserDetail is done, this mdichild (frmListofUsers) must reload / refresh the data shown.

 

grdUsers.Cols = 0;

grdUsers.Rows = 0;

Cursor.Current = Cursors.WaitCursor;

this.LoadUsers();

Cursor.Current = Cursors.Default ;

this.Show ();

 

The question here is: how do set mdiparent for fmUserDetail so that it can also be a mdichild?

Posted

You could try to set it when you initialize the new form, like:

 

//new instance

frmUsers fmUserDetail = new frmUsers();

frmUsers.MdiParent = theInstanceOfParentForm;

 

Your problem is most likely that you don't know the instance of the MDIParent, right?

 

Just pass it into this form and then you can refer to it at any time.

 

I hope this works ...

IS
Posted

Thanx but no go. I do set the instance (I think) by

 

frmUsers.MdiParent = this.MdiParent.

 

Then in order to use the statusbar on the mdi I use the following code:

 

((frmMain)this.Parent.Parent).SetStatusBar ("Restricted Goods Description");

 

This works great if the sequence is = MDI to child. But the moment the path changes to MDI to child to child of child an exception error occurs.

Posted

Some more information:

 

The system consist of an MDI form (frmMain) and several children. On the MDI form is a statusbar called stBar.

 

The scenario:

 

I load the child form using the following code:

public void SetStatusBar(string txt)

 

{

 

stBar.Panels.Clear();

 

StatusBarPanel panel1 = new StatusBarPanel();

 

panel1.AutoSize = StatusBarPanelAutoSize.Spring;

 

panel1.Text = " " + txt;

 

stBar.Panels.Add(panel1);

 

}

 

private void mnuUser_Click(object sender, System.EventArgs e)

 

{

 

SetStatusBar ("Creating Window ...");

 

frmUserList fmUserList = new frmUserList();

 

fmUserList.MdiParent = this;

 

fmUserList.Top = 100;

 

fmUserList.Left = (this.Width - fmUserList.Width ) /2 -5;

 

fmUserList.Show();

 

}

 

The child form frmUserList is then loaded. If the mouse if moved over a textbox, the statusbar on the MDI reflects the applicable text.

 

private void txtName_MouseHover(object sender, EventArgs e)

 

{

 

((frmMain)this.Parent.Parent).SetStatusBar ("This is the Name");

 

}

 

Now up and until this point all is working well. The problem occurs when frmUserList loads another form:

 

frmUsers fmUserDetail = new frmUsers();

 

fmUserDetail.Top = 100;

 

fmUserDetail.Left = 10;

 

fmUserDetail.MdiParent = this.MdiParent;

 

fmUserDetail.Show();

 

Now if the same code is used when the cursor moves over a textbox in this form (fmUserDetail), an exception error occurs:

 

"Object reference not set to an instance of an object"

 

I have to be able to manipulate the statusbar from a child form called by another child form.

Posted

I see, you are having a problem in the parenting definition, who owns who.

 

I would like to give you an idea of how I solved a similar problem:

 

I created a singleton (shared) class that maintains nothing but an arraylist to hold any instance of a form that I wish it should hold.

 

If you are not familiar with the concept of a singleton class, it is invoked only once and stays open until all references to it from the app are removed (in my case, until I close the app).

 

Now, I always add the frmMain as the first entry to this arraylist, then, at any point, I can reference right back to it with code like:

 

Dim ArrList As ArrayList = SharedForms.FormArrayList

CType(ArrList.Item(0), frmMain).Panel1.Text = "Whatever"

 

[sorry, I am VB guy, but I am sure you can translate to C#]

 

That will for sure resolve your referencing problem...

 

Please let me know if this is helpful to you ...

IS

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...