簡體   English   中英

如何控制Web瀏覽器應用程序中的打開的標簽頁?

[英]How control opened tab in my web browser application?

嗨,我是C#的新手,我制作了一些帶有標簽的簡單Web瀏覽器。

我的問題是我不知道如何控制選定的標簽。 我介意如果我單擊以支持它,請返回到選定的選項卡。

對不起,我的英語不好。 這是我的代碼:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace HardRam
{
    public partial class Form1 : Form
    {
        int i = 1;
        public Form1()
        {
            InitializeComponent();
        }           
        private void toolStripButton1_Click(object sender, EventArgs e)
        {          
          webBrowser1.GoBack();
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
           webBrowser1.GoForward();
        }
        private void toolStripButton3_Click(object sender, EventArgs e)
        {            
            webBrowser1.Navigate("http://www.google.com");
        }
        private void toolStripButton4_Click(object sender, EventArgs e)
        {            
            webBrowser1.Refresh();
        }
        private void toolStripButton5_Click(object sender, EventArgs e)
        {           
            webBrowser1.Stop();
        }
        private void button1_Click(object sender, EventArgs e)
        {           
            webBrowser1.Navigate(textBox1.Text);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            webBrowser1.Navigate("http://www.google.com/search?&q=" + textBox2.Text);
        }

        private void label2_Click(object sender, EventArgs e) { }

        private void toolStripLabel1_Click(object sender, EventArgs e) { }

        private void toolStripButton7_Click(object sender, EventArgs e)
        {
            TabPage newTp = new TabPage();
            WebBrowser newWB = new WebBrowser();
            newWB.Name = "Page" + tabControl1.TabPages.Count + 1;
            newWB.Dock = DockStyle.Fill;
            newWB.Url = new Uri(@"http://www.google.com");
            newTp.Controls.Add(newWB);
            tabControl1.TabPages.Add(newTp);    
        }

        private void toolStripButton8_Click(object sender, EventArgs e)
        {
            webBrowser1.Navigate("http://www.google.com/search?&q=" + textBox2.Text );
        }
    }
}

您為每個選項卡創建一個web-browser實例,因此必須獲取當前選項卡及其web-browser控件,因此請參見以下示例:

private void btnaddtab_Click(object sender, EventArgs e)
        {
            TabPage newTp = new TabPage();
            WebBrowser newWB = new WebBrowser();
            newWB.Name = "Page" + tabControl1.TabPages.Count + 1;
            newWB.Dock = DockStyle.Fill;
            newWB.Url = new Uri(@"http://www.bing.com");
            newTp.Controls.Add(newWB);
            tabControl1.TabPages.Add(newTp);   
        }

        private void btnback_Click(object sender, EventArgs e)
        {
            (tabControl1.TabPages[tabControl1.SelectedIndex].Controls[0] as WebBrowser).GoBack();
        }

        private void btnforward_Click(object sender, EventArgs e)
        {
            (tabControl1.TabPages[tabControl1.SelectedIndex].Controls[0] as WebBrowser).GoForward();
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM