Link to home
Start Free TrialLog in
Avatar of johnwalden869050
johnwalden869050

asked on

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

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!
Avatar of deanvanrooyen
deanvanrooyen

try

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

also put try catch blocks to trap any errors...
Avatar of johnwalden869050

ASKER

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
Avatar of Mike Tomlinson
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!";
        }

    }
ASKER CERTIFIED SOLUTION
Avatar of deanvanrooyen
deanvanrooyen

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Dean,

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

All the best,

John