I'm pretty new to programming and was working on building a webbrowser in visual studio but I have run into an issue that I cannot seem to figure out. I have my form which holds my menubar and tabcontrol. Then I created a user control which contains my toolstrip and web browser control. I then added my user control to the inside of my tabpage on my form. My issue is that when I click my button to create a new tab, the first new tab is correct, it has my usercontrol, but every new tab after that is blank. How can I make it so that every new tab that opens has my usercontrol? Here is the code that I have.
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);
}
}
}
And my User Control
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();
}
}
}
Do I understand well, that you'd like to add your UserControl to new TabPage you create? Then you need to add your UserControl to your new tab's Controls:
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);
}