简体   繁体   English

打开网页浏览器

[英]Open a web browser

I have created two buttons close and new tab and a web browser which loads google. 我创建了两个按钮close和new tab,以及一个加载Google的网络浏览器。 When I click new tab it opens the new tab but doesn't open a web browser. 当我单击新选项卡时,它会打开新选项卡,但不会打开网络浏览器。 Here's the code I have so far which is from the Microsoft help site. 这是到目前为止,我从Microsoft帮助站点获得的代码。 I'm using Visual Studio 2012 with c#. 我正在使用Visual Studio 2012和c#。

private void newTab_Click(object sender, EventArgs e)
{
    string title = "TabPage " + (tabControl1.TabCount + 1).ToString();
    TabPage myTabPage = new TabPage(title);
    tabControl1.TabPages.Add(myTabPage);
}

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

At the moment I have this code but when I type in the search box it only works in tabpage 1 and not in any of the new tabs that I open so I need to add some code to the searchbox? 目前,我已经有了这段代码,但是当我在搜索框中输入内容时,它只能在选项卡页1中使用,而不能在我打开的任何新标签中使用,因此我需要在搜索框中添加一些代码吗?

my code is: 我的代码是:

private void newTab_Click(object sender, EventArgs e)
{
    string title = "TabPage " + (tabControl1.TabCount + 1).ToString();
    TabPage myTabPage = new TabPage(title);


    var browser = new WebBrowser();
    browser.Dock = DockStyle.Fill;
    browser.Url = new Uri(@"http://www.google.com");


    myTabPage.Controls.Add(browser);
    tabControl1.TabPages.Add(myTabPage);

}

You also have to add a new instance of the WebBrowser control to your tab: 您还必须在选项卡中添加WebBrowser控件的新实例:

private void newTab_Click(object sender, EventArgs e)
{
    string title = "TabPage " + (tabControl1.TabCount + 1).ToString();
    TabPage myTabPage = new TabPage(title);

    // Create new WebBrowser instance
    var browser = new WebBrowser();
    browser.Dock = DockStyle.Fill;
    browser.Url = new Uri(@"http://www.google.de");

    // Add the WebBrowser control to the TabPage.
    myTabPage.Controls.Add(browser);
    tabControl1.TabPages.Add(myTabPage);
}

Each TabPage is initially empty, just like it is if you would have added it using the designer. 每个TabPage最初都是空的,就像使用设计器添加它一样。 It's content depends on what you want to add. 它的内容取决于您要添加的内容。 Sharing a WebBrowser -Control does not make sense, since you want to open different websites on different tabs, I guess. 我想,共享WebBrowser -Control没有意义,因为您想在不同的选项卡上打开不同的网站。

In case you do not want the user to close the first tab you can simply check if the tab the user requests to close is the first one created: 如果您不希望用户关闭第一个选项卡,则只需检查用户请求关闭的选项卡是否是创建的第一个选项卡:

private void closeTab_Click(object sender, EventArgs e)
{
    // Is the Tab Page the first one?
    if (tabControl1.SelectedTab == tabControl1.TabPages[0])
        return;

    tabControl1.TabPages.Remove(tabControl1.SelectedTab);
}

Or in case you want at least one tab page to stay "open" you can use the TabCount -Property: 或者,如果您希望至少一个标签页保持“打开”状态,则可以使用TabCount -Property:

private void closeTab_Click(object sender, EventArgs e)
{
    // Is the Tab Page the last one opened?
    if (tabControl1.TabCount == 1)
        return;

    tabControl1.TabPages.Remove(tabControl1.SelectedTab);
}

You are missing webbrowser control in your code: 您的代码中缺少webbrowser控件:

private void newTab_Click(object sender, EventArgs e)
{
        string title = "TabPage " + (tabControl1.TabCount + 1).ToString();
        TabPage myTabPage = new TabPage(title);
        tabControl1.TabPages.Add(myTabPage);
        WebBrowser wb = new WebBrowser();
        myTabPage.Controls.Add(wb);
        wb.Navigate("google.com");
}

private void closeTab_Click(object sender, EventArgs e)
{
    if(tabControl1.TabPages.Count == 1)
       return;

    tabControl1.TabPages.Remove(tabControl1.SelectedTab);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM