如何将导航的webbrowser控件传递给另一个表单并显示它?

时间:2013-07-07 13:22:35

标签: c# forms

// Its called from my main form in the following code

BetaScreen form = new BetaScreen(wbCache);
form.ShowDialog();
form.Dispose();

// through here

public partial class BetaScreen : Form // this is where I want to display
{
    public BetaScreen(WebBrowser browser)
    {
        InitializeComponent();
        wbMain = browser;
        wbMain.PerformLayout(); // just tried something to make it work
    }
}

我想要做的是,我有一个webbrowser导航到一个页面,比如stackoverflow.com。我可以在我的webbrowser中看到我的主要形式的网站图片等。我想做一些类似弹出窗口的东西,以弹出显示这个webbrowser(BetaScreen)。但是我做不到,它只是向我展示了第二种形式的空白白色网页浏览器(BetaScreen)。

代码更新:

object cache;
        public BetaScreen(object browser)
        {
            InitializeComponent();
            cache = browser;
        }

        private void BetaScreen_Load(object sender, EventArgs e)
        {
            WebBrowser browser = (WebBrowser)cache;
            browser.Dock = DockStyle.Fill;
            this.Controls.Add(browser);
        }

我通过传递

`ShowDialog((object)wbCache);

但是这次它从我的主要形式= D

开始

1 个答案:

答案 0 :(得分:0)

此时您正在传递WebBrowser控件的相同引用。您需要使用反射克隆控件本身并传递它,而不是传递原始引用。我修改了this code,因为那里的代码不能与WebBrowser控件一起使用。

PropertyInfo[] controlProperties = typeof(WebBrowser).GetProperties(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);

WebBrowser instance = Activator.CreateInstance<WebBrowser>();

foreach (PropertyInfo propInfo in controlProperties)
{
    if (propInfo.CanWrite)
    {
         if (propInfo.Name != "WindowTarget")
             propInfo.SetValue(instance, propInfo.GetValue(wbCache, null), null);
     }
}

然后通过instance

using(BetaScreen form = new BetaScreen(instance))
{
    form.ShowDialog();
}

您可以更改BetaScreen构造函数以获取WebBrowser控件。

public BetaScreen(WebBrowser browser)
相关问题