1
// 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
    }
}

What I want to do is, I have a webbrowser navigate to a page, say stackoverflow.com. I can see the website pictures etc in my webbrowser which is in my main form. I want to do something like popup to show this webbrowser in pop up (BetaScreen). But I can't do it, it just shows me a blank white webbrowser in 2nd form (BetaScreen).

CODE UPDATE:

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);
        }

I got it work with passing

`ShowDialog((object)wbCache);

but this time it goes off from my main form =D

user1046403
  • 555
  • 1
  • 5
  • 11
  • Is it sufficient to just pass the url the webbrowser is currently on and load that in the popup webbrowser? – keyboardP Jul 07 '13 at 13:34
  • I have to do this in this way. There are images and stuffs being changed every navigate – user1046403 Jul 07 '13 at 13:36
  • Depending on how the images change, you could pass the [DocumentStream](http://msdn.microsoft.com/en-us/library/system.windows.forms.webbrowser.documentstream.aspx) or [DocumentText](http://msdn.microsoft.com/en-us/library/system.windows.forms.webbrowser.documenttext.aspx) instead. – keyboardP Jul 07 '13 at 13:39
  • Images aren't shown with both methods – user1046403 Jul 07 '13 at 13:48

1 Answers1

0

At the moment you're passing the same reference of the WebBrowser control. You need to clone the control itself using reflection and passing that, rather than passing the reference of the original. I've modified this code because the code there doesn't work with the WebBrowser control.

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);
     }
}

Then pass instance

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

You can change the BetaScreen constructor to take a WebBrowser control.

public BetaScreen(WebBrowser browser)
Community
  • 1
  • 1
keyboardP
  • 68,824
  • 13
  • 156
  • 205