4

I want to recreate a simple browser with tab capability. A new tab has to be created every time the user clicks on "button_addTab" or when the selected web site tries to open a new window.

Here is my code:

Add a new tab by pressing the dedicated button:

private void button_addTab_Click(object sender, EventArgs e) { TabPage addedTabPage = new TabPage("tab title"); //create the new tab tabControl_webBrowsers.TabPages.Add(addedTabPage); //add the tab to the TabControl WebBrowser addedWebBrowser = new WebBrowser() { Parent = addedTabPage, //add the new webBrowser to the new tab Dock = DockStyle.Fill } addedWebBrowser.Navigate("www.google.com"); 

Prevent a site from opening new windows (if you know every webBrowsers name) :

private void specificWebBrowser_NewWindow(object sender, CancelEventArgs e) { WebBrowser thisWebBrowser = (WebBrowser)sender; e.Cancel = true; TabPage addedTabPage = new TabPage("tab title"); tabControl_webBrowsers.TabPages.Add(addedTabPage); WebBrowser addedWebBrowser = new WebBrowser() { Parent = addedTabPage, Dock = DockStyle.Fill } addedWebBrowser.Navigate(thisWebBrowser.StatusText.ToString()); } 

Now my question is: How can I modify the second branch of code in order to match all the web browsers created with no names when the first code is called (at one time)?

2

1 Answer 1

4

Try to modify the first function to attach an event handler to newly created browser:

private void button_addTab_Click(object sender, EventArgs e) { TabPage addedTabPage = new TabPage("tab title"); //create the new tab tabControl_webBrowsers.TabPages.Add(addedTabPage); //add the tab to the TabControl WebBrowser addedWebBrowser = new WebBrowser() { Parent = addedTabPage, //add the new webBrowser to the new tab Dock = DockStyle.Fill }; addedWebBrowser.NewWindow += specificWebBrowser_NewWindow; addedWebBrowser.Navigate("www.google.com"); } 
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, it works. As suggested by Nikolay Khil I've looked at the previous asked question, but the statement [ addedWebBrowser.NewWindow += new EventHandler(specificWebBrowser_NewWindow); ] seems to have an error whereas [ addedWebBrowser.NewWindow += specificWebBrowser_NewWindow; ] properly runs (I have only to change function name). Thank you all.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.