-1

I'm working on a Web browser in Visual Studio 2010, but I can't update the tab's name to the website's name. For example, when you visit a website like CNN.Com, I want the tab to also say, "cnn.com". The project isn't using the default WebBrowser form, by the way. Please explain it in the simplest way possible since I'm new to C#(Just moved from C++ and Java) so I'm not familiar with working with Windows forms. Thanks. Any help is appreciated.

Here's an image of the problem: http://postimage.org/image/5ym4yx0pt/

....

    public Form1()
    {
        InitializeComponent();
    }

    int i = 1;
    private void Form1_Load(object sender, EventArgs e)
    {
        WebBrowser Browse = new WebBrowser();
        //Load a tab when loading form
        tabControl1.TabPages.Add("Tab");//problem
        tabControl1.SelectTab(i - 1);
        Browse.Name = "Lithium Browser";
        Browse.Dock = DockStyle.Fill;
        tabControl1.SelectedTab.Controls.Add(Browse);
        i++;
        ((WebBrowser)tabControl1.SelectedTab.Controls[0]).Navigate("www.google.com");
    }

    private void button1_Click(object sender, EventArgs e)
    {
        ((WebBrowser)tabControl1.SelectedTab.Controls[0]).Navigate(textBox1.Text);
    }

    private void toolStripButton1_Click(object sender, EventArgs e)
    {
        WebBrowser Browse = new WebBrowser();
        tabControl1.TabPages.Add("Tab"); //problem
        tabControl1.SelectTab(i - 1);
        Browse.Name = "Lithium Browser";
        Browse.Dock = DockStyle.Fill;
        tabControl1.SelectedTab.Controls.Add(Browse);
        i++;
    }

    private void toolStripButton2_Click(object sender, EventArgs e)
    {
        tabControl1.TabPages.RemoveAt(tabControl1.SelectedIndex);
        tabControl1.SelectTab(tabControl1.TabPages.Count - 1);
        i = i- 1;

    }

    private void toolStripButton3_Click(object sender, EventArgs e)
    {
        ((WebBrowser)tabControl1.SelectedTab.Controls[0]).GoBack();
    }

    private void toolStripButton4_Click(object sender, EventArgs e)
    {
        ((WebBrowser)tabControl1.SelectedTab.Controls[0]).GoForward();
    }

    private void toolStripButton5_Click(object sender, EventArgs e)
    {
        ((WebBrowser)tabControl1.SelectedTab.Controls[0]).GoHome();
    }

    private void toolStripButton6_Click(object sender, EventArgs e)
    {
        ((WebBrowser)tabControl1.SelectedTab.Controls[0]).Refresh();
    }

    private void toolStripButton7_Click(object sender, EventArgs e)
    {
        ((WebBrowser)tabControl1.SelectedTab.Controls[0]).Stop();
    }

    private void yahooSearchToolStripMenuItem_Click(object sender, EventArgs e)
    {
        toolStripDropDownButton1.Text = yahooSearchToolStripMenuItem.Text;
    }

    private void youtubeSearchToolStripMenuItem_Click(object sender, EventArgs e)
    {
        toolStripDropDownButton1.Text = youtubeSearchToolStripMenuItem.Text;
    }

    private void googleSearchToolStripMenuItem_Click(object sender, EventArgs e)
    {
        toolStripDropDownButton1.Text = googleSearchToolStripMenuItem.Text;
    }

    private void toolStripButton8_Click(object sender, EventArgs e)
    {
        if (toolStripDropDownButton1.Text == googleSearchToolStripMenuItem.Text)
        {
            ((WebBrowser)tabControl1.SelectedTab.Controls[0]).Navigate("http://www.google.com/search?q=" + toolStripTextBox1.Text);
        }

        if (toolStripDropDownButton1.Text == yahooSearchToolStripMenuItem.Text)
        {
            ((WebBrowser)tabControl1.SelectedTab.Controls[0]).Navigate("http://search.yahoo.com/search?p=" + toolStripTextBox1.Text);
        }

        if (toolStripDropDownButton1.Text == youtubeSearchToolStripMenuItem.Text)
        {
            ((WebBrowser)tabControl1.SelectedTab.Controls[0]).Navigate("http://www.youtube.com/results?search_query=" + toolStripTextBox1.Text);
        }
    }

    private void textBox1_TextChanged(object sender, EventArgs e)
    {

    }

    //add KeyUp event for detecting 'Enter' key
    //navigate to specified URL withoud pressing the 'Go' button
    private void textBox1_KeyUp(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Enter)
        {
            ((WebBrowser)tabControl1.SelectedTab.Controls[0]).Navigate(textBox1.Text);
        }

    }

    private void toolStripTextBox1_KeyUp(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Enter)
        {
            if (toolStripDropDownButton1.Text == googleSearchToolStripMenuItem.Text)
            {
                ((WebBrowser)tabControl1.SelectedTab.Controls[0]).Navigate("http://www.google.com/search?q=" + toolStripTextBox1.Text);
            }

            if (toolStripDropDownButton1.Text == yahooSearchToolStripMenuItem.Text)
            {
                ((WebBrowser)tabControl1.SelectedTab.Controls[0]).Navigate("http://search.yahoo.com/search?p=" + toolStripTextBox1.Text);
            }

            if (toolStripDropDownButton1.Text == youtubeSearchToolStripMenuItem.Text)
            {
                ((WebBrowser)tabControl1.SelectedTab.Controls[0]).Navigate("http://www.youtube.com/results?search_query=" + toolStripTextBox1.Text);
            }

            if (toolStripDropDownButton1.Text == youtubeSearchToolStripMenuItem.Text)
            {
                ((WebBrowser)tabControl1.SelectedTab.Controls[0]).Navigate("http://en.wikipedia.org/wiki/" + toolStripTextBox1.Text);
            }

            if (toolStripDropDownButton1.Text == youtubeSearchToolStripMenuItem.Text)
            {
                ((WebBrowser)tabControl1.SelectedTab.Controls[0]).Navigate("http://en.wikipedia.org/wiki/" + toolStripTextBox1.Text);
            }

        }
    }

    private void newTabToolStripMenuItem_Click(object sender, EventArgs e)
    {
        WebBrowser Browse = new WebBrowser();
        tabControl1.TabPages.Add("Tab");
        tabControl1.SelectTab(i - 1);
        Browse.Name = "Lithium Browser";
        Browse.Dock = DockStyle.Fill;
        tabControl1.SelectedTab.Controls.Add(Browse);
        i++;
    }

    private void closeTabToolStripMenuItem_Click(object sender, EventArgs e)
    {
        tabControl1.TabPages.RemoveAt(tabControl1.SelectedIndex);
        tabControl1.SelectTab(tabControl1.TabPages.Count - 1);
        i = i - 1;
    }

    private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
    {

    }

    private void printToolStripMenuItem_Click(object sender, EventArgs e)
    {
        PrintDialog printDialog = new PrintDialog();
        printDialog.ShowDialog();
    }

    private void printPreviewDialog1_Load(object sender, EventArgs e)
    {

    }

    private void printPreviewToolStripMenuItem_Click(object sender, EventArgs e)
    {
        //Associate PrintPreviewDialog with PrintDocument.
        printPreviewDialog1.Document = printDocument1;

        // Show PrintPreview Dialog
        printPreviewDialog1.ShowDialog();
    }

    private void exitToolStripMenuItem_Click(object sender, EventArgs e)
    {
        if (MessageBox.Show("Exit?", "Exit", MessageBoxButtons.OKCancel) == DialogResult.OK)
        {

            Application.Exit();

        }
    }

    // Bring up 'Print Dialog'
    private void pageSetupToolStripMenuItem_Click(object sender, EventArgs e)
    {
        PageSetupDialog pageSetup = new PageSetupDialog();
        pageSetup.PrinterSettings = new System.Drawing.Printing.PrinterSettings();
        pageSetup.PageSettings = new System.Drawing.Printing.PageSettings();
        pageSetup.EnableMetric = false;
        pageSetup.ShowDialog();
    }

    private void stopToolStripMenuItem_Click(object sender, EventArgs e)
    {
        ((WebBrowser)tabControl1.SelectedTab.Controls[0]).Stop();
    }

    private void refreshToolStripMenuItem_Click(object sender, EventArgs e)
    {
        ((WebBrowser)tabControl1.SelectedTab.Controls[0]).Refresh();
    }

    private void homeToolStripMenuItem_Click(object sender, EventArgs e)
    {
        ((WebBrowser)tabControl1.SelectedTab.Controls[0]).GoHome();
    }

    private void previousPageToolStripMenuItem_Click(object sender, EventArgs e)
    {
        ((WebBrowser)tabControl1.SelectedTab.Controls[0]).GoBack();
    }

    private void nextPageToolStripMenuItem_Click(object sender, EventArgs e)
    {
        ((WebBrowser)tabControl1.SelectedTab.Controls[0]).GoForward();
    }

    private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }



    private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
    { 
        Form2 about = new Form2();
        about.Show();
    }



    private void calenderToolStripMenuItem_Click(object sender, EventArgs e)
    {
        calenForm cal = new calenForm();
        cal.Show();
    }
 }

}

...........

pcnThird
  • 2,362
  • 2
  • 19
  • 33

2 Answers2

3

Assuming WebBrowser is the built-in WebBrowser, you can fire the OnDocumentTitleChanged event to change the tab text every time the WebBrowser document title is changed.

to do this, in the form load event, after declaring browse, start typing browse.DocumentTitleChanged += and a tooltip should come up saying 'tab to insert this code' or something along those lines. Just tab twice and Visual Studio will insert a new method for you, with a throw new NotImplementedException(); line. Delete that line and replace it with the code changing your tab's text to the browser's DocumentTitle.

If you need any more information, I suggest you check the documentation: http://msdn.microsoft.com/en-us/library/system.windows.forms.webbrowser.aspx

Though I am confident that using events is the best solution. Events are designed to execute upon certain significant programming 'events' happening, and changing a webpage is one example of such an event. (Events are roughly C#'s equivalent of C++'s function pointers if that helps your understanding at all. Though they are more akin to a std::vector of function pointers.)

Pharap
  • 3,826
  • 5
  • 37
  • 51
1

Set the HTML title tag for the page text contained within the two tags will show up in the tab that the web page is displayed in.

See the following for more about setting the title in the code behind How to use Eval in codebehind to set Page.Title

And this link as well

http://www.asprobot.com/ASP.NET/ASPNET-Title-Tag-and-Meta-Tags

Community
  • 1
  • 1
Mike Beeler
  • 4,081
  • 2
  • 29
  • 44