none
浏览器多选项卡? RRS feed

  • 问题

  • 我的问题是:C#中,怎样使用TabContronl来实现多选项卡,请详细的给出每一步和相关的代码(VBasic2010、VC#2010),最好配上附图。急!!
    2012年5月4日 10:31

答案

  • 我的问题是:C#中,怎样使用TabContronl来实现多选项卡,请详细的给出每一步和相关的代码(VBasic2010、VC#2010),最好配上附图。急!!

    以下给出一个示例(在文本框输入地址后回车,将自动创建新选项卡,地址栏网页之后自动显示)

    【Form1的设计界面代码Form1.designer.cs】

    partial class Form1
        {
            /// <summary>
            /// Required designer variable.
            /// </summary>
            private System.ComponentModel.IContainer components = null;
    
            /// <summary>
            /// Clean up any resources being used.
            /// </summary>
            /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
            protected override void Dispose(bool disposing)
            {
                if (disposing && (components != null))
                {
                    components.Dispose();
                }
                base.Dispose(disposing);
            }
    
            #region Windows Form Designer generated code
    
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent()
            {
                this.tableLayoutPanel1 = new System.Windows.Forms.TableLayoutPanel();
                this.textBox1 = new System.Windows.Forms.TextBox();
                this.tabControl1 = new System.Windows.Forms.TabControl();
                this.tableLayoutPanel1.SuspendLayout();
                this.SuspendLayout();
                // 
                // tableLayoutPanel1
                // 
                this.tableLayoutPanel1.ColumnCount = 1;
                this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 100F));
                this.tableLayoutPanel1.Controls.Add(this.textBox1, 0, 0);
                this.tableLayoutPanel1.Controls.Add(this.tabControl1, 0, 1);
                this.tableLayoutPanel1.Dock = System.Windows.Forms.DockStyle.Fill;
                this.tableLayoutPanel1.Location = new System.Drawing.Point(0, 0);
                this.tableLayoutPanel1.Name = "tableLayoutPanel1";
                this.tableLayoutPanel1.RowCount = 2;
                this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 10F));
                this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 80F));
                this.tableLayoutPanel1.Size = new System.Drawing.Size(292, 273);
                this.tableLayoutPanel1.TabIndex = 0;
                // 
                // textBox1
                // 
                this.textBox1.Dock = System.Windows.Forms.DockStyle.Fill;
                this.textBox1.Location = new System.Drawing.Point(3, 3);
                this.textBox1.Name = "textBox1";
                this.textBox1.Size = new System.Drawing.Size(286, 21);
                this.textBox1.TabIndex = 0;
                this.textBox1.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.textBox1_KeyPress);
                // 
                // tabControl1
                // 
                this.tabControl1.Dock = System.Windows.Forms.DockStyle.Fill;
                this.tabControl1.Location = new System.Drawing.Point(3, 33);
                this.tabControl1.Name = "tabControl1";
                this.tabControl1.SelectedIndex = 0;
                this.tabControl1.Size = new System.Drawing.Size(286, 237);
                this.tabControl1.TabIndex = 1;
                // 
                // Form1
                // 
                this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.ClientSize = new System.Drawing.Size(292, 273);
                this.Controls.Add(this.tableLayoutPanel1);
                this.Name = "Form1";
                this.Text = "Form1";
                this.Load += new System.EventHandler(this.Form1_Load);
                this.tableLayoutPanel1.ResumeLayout(false);
                this.tableLayoutPanel1.PerformLayout();
                this.ResumeLayout(false);
    
            }
    
            #endregion
    
            private System.Windows.Forms.TableLayoutPanel tableLayoutPanel1;
            private System.Windows.Forms.TextBox textBox1;
            private System.Windows.Forms.TabControl tabControl1;
    
        }

    【Form1的cs后台运行代码】

     public partial class Form1 : Form
        {
            int counter = 0;
    
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
    
            }
    
            private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
            {
                //敲下回车,创建一个新页面(假设输入没有http……)
                if (e.KeyChar == (char)13)
                {
                    string url = textBox1.Text;
                    Uri uri = null;
                    if (url.IndexOf("http://") < 0)
                    {
                        url = "http://" + url;
                    }
                    //判断是否可以转化成一个正常的url
                    if (Uri.TryCreate(url, UriKind.Absolute, out uri))
                    {
                        //不存在,直接添加
                        if (tabControl1.TabPages[url] == null)
                        {
                            tabControl1.TabPages.Add(url, (++counter).ToString());
                            //动态添加WebBrowser,并指定url打开预览
                            WebBrowser wb = new WebBrowser();
                            wb.Dock = DockStyle.Fill;
                            wb.Url = uri;
                            tabControl1.TabPages[url].Controls.Add(wb);
                        }
    
                        //把页面切换为当前的
                        tabControl1.SelectedTab = tabControl1.TabPages[url];
                    }
                }
            }
        }

       QQ我:讨论(Talk)
    下载MSDN桌面工具(Vista,Win7)
    我的博客园
    慈善点击,点击此处


    2012年5月4日 12:03
    版主

全部回复

  • 我的问题是:C#中,怎样使用TabContronl来实现多选项卡,请详细的给出每一步和相关的代码(VBasic2010、VC#2010),最好配上附图。急!!

    以下给出一个示例(在文本框输入地址后回车,将自动创建新选项卡,地址栏网页之后自动显示)

    【Form1的设计界面代码Form1.designer.cs】

    partial class Form1
        {
            /// <summary>
            /// Required designer variable.
            /// </summary>
            private System.ComponentModel.IContainer components = null;
    
            /// <summary>
            /// Clean up any resources being used.
            /// </summary>
            /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
            protected override void Dispose(bool disposing)
            {
                if (disposing && (components != null))
                {
                    components.Dispose();
                }
                base.Dispose(disposing);
            }
    
            #region Windows Form Designer generated code
    
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent()
            {
                this.tableLayoutPanel1 = new System.Windows.Forms.TableLayoutPanel();
                this.textBox1 = new System.Windows.Forms.TextBox();
                this.tabControl1 = new System.Windows.Forms.TabControl();
                this.tableLayoutPanel1.SuspendLayout();
                this.SuspendLayout();
                // 
                // tableLayoutPanel1
                // 
                this.tableLayoutPanel1.ColumnCount = 1;
                this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 100F));
                this.tableLayoutPanel1.Controls.Add(this.textBox1, 0, 0);
                this.tableLayoutPanel1.Controls.Add(this.tabControl1, 0, 1);
                this.tableLayoutPanel1.Dock = System.Windows.Forms.DockStyle.Fill;
                this.tableLayoutPanel1.Location = new System.Drawing.Point(0, 0);
                this.tableLayoutPanel1.Name = "tableLayoutPanel1";
                this.tableLayoutPanel1.RowCount = 2;
                this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 10F));
                this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 80F));
                this.tableLayoutPanel1.Size = new System.Drawing.Size(292, 273);
                this.tableLayoutPanel1.TabIndex = 0;
                // 
                // textBox1
                // 
                this.textBox1.Dock = System.Windows.Forms.DockStyle.Fill;
                this.textBox1.Location = new System.Drawing.Point(3, 3);
                this.textBox1.Name = "textBox1";
                this.textBox1.Size = new System.Drawing.Size(286, 21);
                this.textBox1.TabIndex = 0;
                this.textBox1.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.textBox1_KeyPress);
                // 
                // tabControl1
                // 
                this.tabControl1.Dock = System.Windows.Forms.DockStyle.Fill;
                this.tabControl1.Location = new System.Drawing.Point(3, 33);
                this.tabControl1.Name = "tabControl1";
                this.tabControl1.SelectedIndex = 0;
                this.tabControl1.Size = new System.Drawing.Size(286, 237);
                this.tabControl1.TabIndex = 1;
                // 
                // Form1
                // 
                this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.ClientSize = new System.Drawing.Size(292, 273);
                this.Controls.Add(this.tableLayoutPanel1);
                this.Name = "Form1";
                this.Text = "Form1";
                this.Load += new System.EventHandler(this.Form1_Load);
                this.tableLayoutPanel1.ResumeLayout(false);
                this.tableLayoutPanel1.PerformLayout();
                this.ResumeLayout(false);
    
            }
    
            #endregion
    
            private System.Windows.Forms.TableLayoutPanel tableLayoutPanel1;
            private System.Windows.Forms.TextBox textBox1;
            private System.Windows.Forms.TabControl tabControl1;
    
        }

    【Form1的cs后台运行代码】

     public partial class Form1 : Form
        {
            int counter = 0;
    
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
    
            }
    
            private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
            {
                //敲下回车,创建一个新页面(假设输入没有http……)
                if (e.KeyChar == (char)13)
                {
                    string url = textBox1.Text;
                    Uri uri = null;
                    if (url.IndexOf("http://") < 0)
                    {
                        url = "http://" + url;
                    }
                    //判断是否可以转化成一个正常的url
                    if (Uri.TryCreate(url, UriKind.Absolute, out uri))
                    {
                        //不存在,直接添加
                        if (tabControl1.TabPages[url] == null)
                        {
                            tabControl1.TabPages.Add(url, (++counter).ToString());
                            //动态添加WebBrowser,并指定url打开预览
                            WebBrowser wb = new WebBrowser();
                            wb.Dock = DockStyle.Fill;
                            wb.Url = uri;
                            tabControl1.TabPages[url].Controls.Add(wb);
                        }
    
                        //把页面切换为当前的
                        tabControl1.SelectedTab = tabControl1.TabPages[url];
                    }
                }
            }
        }

       QQ我:讨论(Talk)
    下载MSDN桌面工具(Vista,Win7)
    我的博客园
    慈善点击,点击此处


    2012年5月4日 12:03
    版主
  • 好像有点错误哦:

    错误列表:

    请解决

    2012年7月14日 11:22
  • 你的counter没有定义,同时请完全按照我的代码来做。(在一个窗体上拖拽一个WebBrowser和一个TextBox)。

    下载MSDN桌面工具(Vista,Win7)
    我的博客园
    慈善点击,点击此处

    2012年7月14日 11:26
    版主
  • counter如何定义
    2012年7月14日 12:28
  • counter如何定义

    你仔细看我代码了吗?请仔细看啊……我的counter如何定义,先自己动脑筋再说

    int counter = 0;


    下载MSDN桌面工具(Vista,Win7)
    我的博客园
    慈善点击,点击此处

    2012年7月14日 23:58
    版主
  • 希望你能把运行后的效果图贴出来,看一下吧!
    2012年7月15日 5:13
  • 希望你能把运行后的效果图贴出来,看一下吧!

    在WinForm上拖拽一个TextBox和一个WebBrowser,TabControl。


    下载MSDN桌面工具(Vista,Win7)
    我的博客园
    慈善点击,点击此处

    2012年7月15日 6:34
    版主
  • 谢谢!请问你的能够自动根据网页内的链接点击新建选项卡吗?
    还有你的是使用123的数字来给每个选项卡命名,那么该怎样做才能使选项卡的名字是当前网页的名称呢?
    2012年7月16日 2:34
  • 谢谢!请问你的能够自动根据网页内的链接点击新建选项卡吗?

    不能,因为暂时木有提供这个功能。啊哈哈

    下载MSDN桌面工具(Vista,Win7)
    我的博客园
    慈善点击,点击此处

    2012年7月16日 2:36
    版主