1

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"]
4

2 回答 2

0

您是否考虑过在您的 UserControl 上创建属性依赖项?这样,您的用户控件不必知道它的位置,只需知道它具有对 WebBrowser 控件的引用。

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

这样,您的主窗体负责进行连接,并且您避免了与父窗体的讨厌耦合。

于 2009-10-23T02:02:02.067 回答
0

我可以更多地解耦并将业务逻辑放在业务层类中。然后我在一个可以缓存对它需要操作的控件的引用的地方创建业务层对象,并将业务层对象的引用缓存在需要调用逻辑的对象中。当我需要用一个新类(例如从 aximp 生成的类)替换 WebBrowser 控件时,这将使我受益。我可以通过访问缓存的引用来调用业务逻辑对象,这与 MFC 的 doc/view/frame 非常相似,您可以在其中使用 GetDocument()->UpdateAllViews 在某些内容发生更改时更新其他视图。

于 2009-10-23T21:49:46.213 回答