I will address questions 2 and 3 for you:
To show the page title in the form¡¯s title bar:
Hook up a Document_Completed event handler for the webBrowser and put this code into it:
this.Text = webBrowser1.DocumentTitle;
(To create a Document_Completed event handler, go to the Design view and select the webBrowser control. Then in the properties window, click on the lightning bolt icon which should show a list of events for the browser. Find the one labeled ¡®DocumentCompleted¡¯ and double-click it. This should take you back to code view and the event handler will be added. Put the code listed above into the handler.)
To prevent IE from opening a new window you need to intercept the NewWindow event. I presume you want to open a new window with another instance of your webBrowser form. And you want to be able to do the same by right-clicking a link.
Hook up a NewWindow event handler using the same technique above and add the following code:
Form1 f1 = new Form1(lastUrl);
f1.Show();
e.Cancel = true;
This creates a new Form1 (I am assuming that this is the form that your browser control is on), passes a string called ¡®lastUrl¡¯ (more on this later). Launches the form and cancels the event. So, whenever the browser needs to create a new window, this routine will make a new browser for you and stop the event from launching a window in IE.
Now, we need to make Form1 display the web page that we just passed to it. Make the constructor of Form1 look like this:
public Form1(string url)
{
InitializeComponent();
webBrowser1.StatusTextChanged += new EventHandler(webBrowser1_StatusTextChanged);
webBrowser1.Navigate(url);
}
This makes Form1 receive a string url and uses it to navigate the webBrowser whenever it is created. The webBrowser1.StatusTextChanged ¡ line hooks up an event handler that is not in the list of handlers that you can choose from in the properties window. So you will have to add this event handler like this:
string lastUrl;
void webBrowser1_StatusTextChanged(object sender, EventArgs e)
{
string s0 = webBrowser1.StatusText;
if (s0.Contains("http"))
{
lastUrl = s0;
}
toolStripStatusLabel1.Text = s0; //optional
}
The status text is what is displayed on the status strip at the bottom of most browsers. When you hover your mouse over a url, you will see this change. This bit of code looks for changes in this status and remembers the last bit of status that shows a url in the string variable ¡®lastUrl¡¯. This is the variable that we use to launch any new window either automatically or by right-clicking and selecting the ¡®open in new window¡± option from the context menu. Optionally, you can add a toolstrip wit a toolStripStatusLabel to the bottom of your form and display the status like most browsers.
Finally, one more bit of housekeeping. Assuming Form1 is the first form your program launches (it may not be since you have a splash screen), you will need to make the call to Form1 in the Program.cs file match your constructor. Change the Application.Run(new Form1()); line to:
Application.Run(new Form1(""));
This will provide the string to Form1 that the constructor is expecting.
I hope this explanation was clear. Here is the complete listing of my Form1 which has a webBrowser, textbox, button, and toolStripStatusLabel:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1(string url)
{
InitializeComponent();
webBrowser1.StatusTextChanged += new EventHandler(webBrowser1_StatusTextChanged);
webBrowser1.Navigate(url);
}
string lastUrl;
void webBrowser1_StatusTextChanged(object sender, EventArgs e)
{
string s0 = webBrowser1.StatusText;
if (s0.Contains("http"))
{
lastUrl = s0;
}
toolStripStatusLabel1.Text = s0;
}
private void button1_Click(object sender, EventArgs e)
{
webBrowser1.Navigate(textBox1.Text);
}
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
this.Text = webBrowser1.DocumentTitle;
}
private void webBrowser1_NewWindow(object sender, CancelEventArgs e)
{
Form1 f1 = new Form1(lastUrl);
f1.Show();
e.Cancel = true;
}
}
}