ezrarieben
ezrarieben

Reputation: 96

Keep layout the same when adding a tab [SOLUTION BELOW CODE]

I am quite new at C# so I need everything as simple as possible, I have coded a web browser with tabs, when I click add tab however it only Opens a webrowser in the new tab on google.co.uk but what I need is the new tab to have an adressbar and a navigate button for me to be able to navigate to another URL in the new tab. Basically what I want to get is an add tab button that adds a tab with a totally seperate Web Browser and a seperate adressbar and a seperate Navigate button. Here is my code so far:

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

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button2_Click(object sender, EventArgs e)
    {
        TabPage tb = new TabPage("Tab");
        WebBrowser wb = new WebBrowser();
        wb.Dock = DockStyle.Fill;
        wb.Navigate("www.google.co.uk");
        tabControl1.TabPages.Add(tb);
        tb.Controls.Add(wb);
        tabControl1.SelectTab(tb);
    }

    private void closeAltF4ToolStripMenuItem_Click(object sender, EventArgs e)
    {
        this.Close();
    }

    private void addTabToolStripMenuItem_Click(object sender, EventArgs e)
    {
        TabPage tb = new TabPage("Tab");
        WebBrowser wb = new WebBrowser();
        wb.Dock = DockStyle.Fill;
        wb.Navigate("www.google.co.uk");
        tabControl1.TabPages.Add(tb);
        tb.Controls.Add(wb);
        tabControl1.SelectTab(tb);
        tb.Controls.Add(new TextBox());


    }

    private void button1_Click_1(object sender, EventArgs e)
    {
        webBrowser1.Navigate(textBox1.Text);
    }

    private void removeTabToolStripMenuItem_Click(object sender, EventArgs e)
    {
        tabControl1.TabPages.Remove(tabControl1.SelectedTab);
    }

}

}

SOLUTION:

I got it to work with the Usercontrol, I designed a Usercontrol form and then added it into the tabs using this code:

TabPage tb = new TabPage("Tab");
        MenuStrip ms = new MenuStrip();
        tabControl1.TabPages.Add(tb);
        tb.Controls.Add(ms);
        tb.Controls.Add(new UserControl1()); 
        tabControl1.SelectTab(tb);

Upvotes: 0

Views: 431

Answers (1)

GodsCrimeScene
GodsCrimeScene

Reputation: 1270

A user control is the best way to keep the same layout. You can think of it like a panel. It can keep your controls grouped and maintain the same look. Add your website GUI to the panel then add the panel control to the TabControl.

Something like

 TabPage tb = new TabPage("Tab");
    MenuStrip ms = new MenuStrip();
    ms.Items.Add("Add");
    ms.items[0].Click += new EventHandler(AddMenu_Click);
    tb.Controls.Add(ms);
    tb.Controls.Add(new UserControl(tabControl1)); //If you need to update tab text
      tabControl1.TabPages.Add(tb);

This would create a menu strip on each tabpage and then your user control or "panel" would fill in the rest.

Upvotes: 1

Related Questions