WinForm TabControl tab page contents does not update until the new tab is shown

风流意气都作罢 提交于 2019-12-11 11:48:53

问题


I have a tabcontrol which allows the creation of new tabs. Each new tab has a web browser control CEFSharp on it. When the new tab is created it is not shown the previously opened tab is shown; which is what we want.

However, the browser on the newly created tab is only added to the tab page, and only partially runs... it does not go to the loading state until the tab page is shown.

Here is the Tabpage creation code:

private void AddNewBrowser()
        {
            Log("Adding New Tab and Browser");

            UiBrowser browser = new UiBrowser();

            TabPage tp = new TabPage();

            tp.Controls.Add(browser);
            customTabControl1.TabPages.Add(tp);
        }

The UiBrowser is a UserControl which contains the CEFSharp Browser Control plus some extra UI.

And here is the Startup code for the Browser itself.

private void UiBrowser_Load(object sender, EventArgs e)
        {
            Execute();
        }

private void Execute()
        {
            webBrowser = new ChromiumWebBrowser("http://google.co.uk")
            {
                Dock = DockStyle.Fill,
                Text = "Loading...",
                Tag = Tag
            };

            webBrowser.TitleChanged += Browser_TitleChanged;
            webBrowser.AddressChanged += Browser_AddressChanged;
            webBrowser.ConsoleMessage += Browser_ConsoleMessage;
            webBrowser.LoadingStateChanged += Browser_LoadingStateChanged;
            webBrowser.StatusMessage += Browser_StatusMessage;

            browserPanel.Controls.Add(webBrowser);
            Application.DoEvents();
        }

The code has been simplified for clarity and I have not found a solution on SO or elsewhere for this problem.

Question: How do I get the browser control to load the webpage whilst remaining in the background? That is while the TabPage that the control is on is NOT shown to the user.


回答1:


The Load event will only happen when the control becomes visible the first time:

Occurs before the control becomes visible for the first time.

so try moving your Execute method into the UserControl's constructor code.




回答2:


There is no "official" way of doing that.

But if you really need it and don't afraid using internals, you may take a look at my answer to WinForms: Respond to BindingSource being applied.

The solution (or hack) is encapsulated in this little helper

public static class ControlUtils
{
    static readonly Action<Control, bool> CreateControlFunc = (Action<Control, bool>)Delegate.CreateDelegate(typeof(Action<Control, bool>),
        typeof(Control).GetMethod("CreateControl", BindingFlags.Instance | BindingFlags.NonPublic, null, new[] { typeof(bool) }, null));

    public static void CreateControls(this Control target)
    {
        if (!target.Created)
            CreateControlFunc(target, true);
        else
            for (int i = 0; i < target.Controls.Count; i++)
                target.Controls[i].CreateControls();
    }
}

At the end of your form load event, add the following

this.CreateControls();

or

customTabControl1.CreateControls();

and also here

private void AddNewBrowser()
{
    Log("Adding New Tab and Browser");

    UiBrowser browser = new UiBrowser();

    TabPage tp = new TabPage();

    tp.Controls.Add(browser);
    customTabControl1.TabPages.Add(tp);

    if (customTabControl1.Created)
        tp.CreateControls();
}


来源:https://stackoverflow.com/questions/34907430/winform-tabcontrol-tab-page-contents-does-not-update-until-the-new-tab-is-shown

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!