views:

65

answers:

2

I have this layout:

A UserControl inside a Panel. Panel is inside Form1.

WebBrowser is in another panel that is inside of Form1 as well.

So:

             Form1
Panel1                  Panel2
MyUserControl           TheWebBrowser

How could I set the URL for my webbrowser in Panel2, from withing MyUserControl?

I've tried something like this but it doesn't work well.

this.Parent.Parent.Controls["panel2"].Controls["webBrowser1"]
A: 

Have you considered creating a property dependency on your UserControl? This way your user control doesn't have to know about where it is located, only that it has a reference to a WebBrowser control.

public class UserControl: Control
{
    .
    .
    .
    public WebBrowser Browser{ get; set; }
    .
    .
    .
}

This way your main form is responsible for doing the hookup, and you avoid the nasty coupling to the parent form.

Josh
A: 

I could decouple more and put the business logic in a business tier class. Then I create the business tier object in a place where it can cache the references to controls it needs to operate, and cache the business tier object's reference in the objects that needs to invoke the logic. This would benefit me when I need to replace the WebBrowser control with a new class (such as the one generated from aximp). I can invoke the business logic object by accessing the cached reference pretty much like MFC's doc/view/frame where you can use GetDocument()->UpdateAllViews to update other views when something is changed.

Sheng Jiang 蒋晟