I have a tabcontrol named tabControl1, which includes tabs. Button1 adds a tab to it when clicked and adds a browser and then navigate it to google.com. Button2 should be able to navigate the browser in the selected tab to the text inside Text1.
I could do this easily if C# had pointers like this:
WebBrowser* thebrowser;
Button1_Click(object sender, EventArgs e){
TabPage newtab = new TabPage();
WebBrowser newbrowser = new WebBrowser();
newtab.Controls.Add(newbrowser);
newbrowser.Navigate("google.com");
newtab.Click+=delegate {thebrowser = &newbrowser;};
tabControl1.TabPages.Add(newtab);
}
Button2_Click(object sender, EventArgs e){
thebrowser->Navigate(Text1.Text);
}
That if C# had pointers. Now how can I achieve this with proper C# code? I hope you understood my question and sorry for any errors.
Thanks in advance :)
For what I understand you need to have more than one tab, so more than one browser open at the same time, so a lobal variable will not work.
One way of doing it would be to search the browser on the selected tab.
Button2_Click(object sender, EventArgs e)
{
foreach (Control item in page.Controls)
{
if (item is WebBrowser)
{
((WebBrowser)item).Navigate(Text1.Text);
break;
}
}
}
Well, i'd probably go the Dictionary<int,Webbrowser>
way. Something like:
Dictionary<int, WebBrowser> browsers = new Dictionary<int, WebBrowser>();
Button1_Click(object sender, EventArgs e){
TabPage newtab = new TabPage();
WebBrowser newbrowser = new WebBrowser();
newtab.Controls.Add(newbrowser);
newbrowser.Navigate("google.com");
tabControl1.TabPages.Add(newtab);
browsers.Add(tabControl1.TabCount-1, newbrowser);
}
Button2_Click(object sender, EventArgs e){
browsers[tabControl1.SelectedIndex].Navigate(Text1.Text);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.