积极答复者
浏览器多选项卡?

问题
答案
-
我的问题是: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我:
下载MSDN桌面工具(Vista,Win7)
我的博客园
慈善点击,点击此处- 已建议为答案 Bob ShenModerator 2012年5月8日 2:45
- 已标记为答案 Bob ShenModerator 2012年5月17日 8:41
- 取消答案标记 求知与释疑 2012年7月14日 11:28
- 取消建议作为答案 求知与释疑 2012年7月14日 11:28
- 已标记为答案 ThankfulHeartModerator 2012年7月14日 23:58
- 已编辑 ThankfulHeartModerator 2012年7月15日 6:34
- 取消答案标记 求知与释疑 2012年7月21日 12:17
- 已标记为答案 求知与释疑 2012年8月13日 8:37
全部回复
-
我的问题是: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我:
下载MSDN桌面工具(Vista,Win7)
我的博客园
慈善点击,点击此处- 已建议为答案 Bob ShenModerator 2012年5月8日 2:45
- 已标记为答案 Bob ShenModerator 2012年5月17日 8:41
- 取消答案标记 求知与释疑 2012年7月14日 11:28
- 取消建议作为答案 求知与释疑 2012年7月14日 11:28
- 已标记为答案 ThankfulHeartModerator 2012年7月14日 23:58
- 已编辑 ThankfulHeartModerator 2012年7月15日 6:34
- 取消答案标记 求知与释疑 2012年7月21日 12:17
- 已标记为答案 求知与释疑 2012年8月13日 8:37
-
counter如何定义
你仔细看我代码了吗?请仔细看啊……我的counter如何定义,先自己动脑筋再说
int counter = 0;
- 已编辑 ThankfulHeartModerator 2012年7月14日 23:58