我的新标签按钮只将我的用户控件放在第一个新标签中,但其余的保持空白

My new tab button only puts my usercontrol in the first new tab but the rest stay blank

我是编程新手,正在 visual studio 中构建网络浏览器,但我 运行 遇到了一个我似乎无法弄清楚的问题。我有我的表单,其中包含我的菜单栏和选项卡控件。然后我创建了一个用户控件,其中包含我的工具条和 Web 浏览器控件。然后我将我的用户控件添加到我的表单上我的标签页的内部。我的问题是,当我单击按钮创建新选项卡时,第一个新选项卡是正确的,它有我的用户控件,但之后的每个新选项卡都是空白的。我怎样才能使打开的每个新选项卡都有我的用户控件?这是我的代码。

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;

    namespace WebBrowser.UI
    {
        public partial class Nnocu : Form
        {
            WebControls WebControls = new WebControls();

            public Nnocu()
            {
                InitializeComponent();
            }


            private void Nnocu_Load(object sender, EventArgs e)
            {

            }

            private void exitWebBrowserToolStripMenuItem_Click(object sender, EventArgs e)
            {
                Application.Exit();
            }

            private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
            {
                MessageBox.Show("");
            }


            private void Navigation()
            {
                string web = null;
                WebControls.navigationTextBox.Text = web;               
                WebControls.webBrowser1.Navigate(web);
            }


            private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
            {

            }

            private void Nnocu_KeyDown(object sender, KeyEventArgs e)
            {
                if (e.Control && (e.KeyCode == Keys.T))
                    addTab();

                if (e.Control && (e.KeyCode == Keys.W))
                    this.tabControl1.TabPages.RemoveAt(this.tabControl1.SelectedIndex);
            }



            private void newTabToolStripMenuItem_Click(object sender, EventArgs e)
            {
                addTab();

            }

            private void addTab()
            {


                TabPage tabPage = new TabPage();
                tabPage.Text = "New Tab";
                tabControl1.Controls.Add(tabPage);
                WebControls.Parent = tabPage;
                WebControls.Dock = DockStyle.Fill;
                WebControls.webBrowser1.Navigate("www.google.com");


            }



            private void closeCurrentTabToolStripMenuItem_Click(object sender, EventArgs e)
            {
                tabControl1.TabPages.RemoveAt(this.tabControl1.SelectedIndex);
            }
        }
    }

还有我的用户控件

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WebBrowser.UI
{
    public partial class WebControls : UserControl
    {

            public WebControls()
        {
            InitializeComponent();
        }

        private void goButton_Click(object sender, EventArgs e)
        {
            try
            {
                webBrowser1.Navigate(navigationTextBox.Text);
            }
            catch (Exception ex)
            {
                // Error message for an invalid Web Address.
                MessageBox.Show(ex.Message);
            }
        }

        private void navigationTextBox_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                try
                {
                    webBrowser1.Navigate(navigationTextBox.Text);
                }
                catch (Exception ex)
                {
                    // Error message for an invalid Web Address.
                    MessageBox.Show(ex.Message);
                }
            }
        }

        private void refreshButton_Click(object sender, EventArgs e)
        {
            webBrowser1.Refresh();
        }

    }
}

我明白吗,您想将 UserControl 添加到您创建的新 TabPage 中? 然后您需要将您的 UserControl 添加到新选项卡的控件中:

private void addTab()
{
      TabPage tabPage = new TabPage();
      tabPage.Text = "New Tab";
      WebControl wc = new WebControl();
      wc.Dock = DockStyle.Fill;
      wc.webBrowser1.Navigate("www.google.com");
      tabPage.Controls.Add(wc);
      tabControl1.Controls.Add(tabPage);
}