?
Solved

Having trouble accessing a MDI Parent from a child when using a cast.

Posted on 2006-06-06
5
Medium Priority
?
380 Views
Last Modified: 2008-02-01
Hi!

I am designing a tabbed web browser using C# and have run into a few problems. If anyone out there can help I would be much obliged!

My problem is accessing common fields on the MDI Parent from the child. Basically I want to update the address bar (and other fields) on the MDI Parent when the child form's "webbrowser.navigated" event fires. The code I am using is...

        private void webMainBrowser_Navigated(object sender, WebBrowserNavigatedEventArgs e)
        {
            frmMain MainForm = (frmMain)this.MdiParent;
            MainForm.cmbAddress.Text = this.webMainBrowser.Url.ToString();
        }

There are no errors being thrown up in my code but nothing is happening either! Help!

Regards

John

PS Hate to state the obvious but I am a newbie!
0
Comment
Question by:johnwalden869050
  • 2
  • 2
5 Comments
 
LVL 12

Expert Comment

by:deanvanrooyen
ID: 16845360
try

frmMain MainForm = frmMain()
MainForm = (frmMain)this.MdiParent;

also put try catch blocks to trap any errors...
0
 

Author Comment

by:johnwalden869050
ID: 16845658
Hi!

Thanks for your interest. I have tried as you have suggested and got a compiler error...

'frmMain' is a 'type' but is used like a 'variable'

Any ideas?

Thanks again

John
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 16847464
I don't see any obvious reasons why this wouldn't work...

This worked in a simple project for me:

    public partial class frmMain : Form
    {

        private void frmMain_Load(object sender, EventArgs e)
        {
            Form9 f9 = new Form9();
            f9.MdiParent = this;
            f9.Show();
        }
    }

    public partial class Form9 : Form
    {

        private void button1_Click(object sender, EventArgs e)
        {
            frmMain MainForm = (frmMain)this.MdiParent;
            MainForm.textBox1.Text = "Hello world!";
        }

    }
0
 
LVL 12

Accepted Solution

by:
deanvanrooyen earned 2000 total points
ID: 16849522
How to Access MDI Parent Forms From MDI Child Form
By RL Smith

How to access MDI Parent forms from MDI Child forms: This code shows how to access objects contained in an MDI parent form, from an MDI Child form. Simply using MDIParent will not work, you have to cast MDIParent to your MDI Parent forms. Here is the code:

  //In the MDIChild Form

  private void btnAddToTree_Click(object sender, System.EventArgs e)
  {
   
    //Cast MDIParent to your MDIParent Form
    theMDIParentForm theParent = (theMDIParentForm) this.MDIParent;
   
      //Now you can access objects on the MDI Parent form from MDI Children
      theParent.myTreeViewControl.Nodes[0].Add(new TreeNode("New Child Node"));

  }

0
 

Author Comment

by:johnwalden869050
ID: 16850178
Dean,

Thanks for your help. I must be missing something further up the line.

All the best,

John
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Loops Section Overview
Suggested Courses

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question