2009-10-06 34 views
1

我已經從我的代碼中粘貼了兩個函數。它們用於在tabcontrol中添加一個選項卡並刪除tabcontrol中的一個選項卡。這兩個函數都在tabcontrol駐留的相同表單中。如何找到tabcontrol中的選項卡使用c#windows應用程序關閉它

我能夠輕鬆添加的選項卡中的tabcontrol。我從另一個類調用AddTab()。它完美的作品。

我試圖做同樣的事情從另一個類中刪除選項卡。但是tabpage tp總是返回null,即使tabcontrol中仍然有兩個選項卡。

我在想什麼?

public void AddTab(string strProcessName) 
    { 
     try 
     { 
       Global.ExistingTabProcessNames.Add(strProcessName); 

       this.Show(); 
       //this below line dosent makes duplicate tabs. 
       TabPage tp = new TabPage(); 
       tp.Text = strProcessName; 
       tp.Name = strProcessName; 

       tabControl1.TabPages.Add(tp); 

       //Activate the newly created Tabpage. 
       tabControl1.SelectedTab = tp; 
       tabControl1.ItemSize = new Size(200, 32); 
       tp.Height = tp.Parent.Height; 
       tp.Width = tp.Parent.Width; 
     } 
     catch (Exception ex) 
     { 
     } 
    } 

    public void RemoveUnusedTabs(string strTabToRemove) 
    { 
     TabPage tp = tabControl1.TabPages[strTabToRemove]; 
     tp.Controls.Remove(this); 
     tabControl1.TabPages.Remove(tp); 
    } 

我從下面像其他類調用RemoveUnusedTabs ..

//該類創建一個實例。 任務欄RemoveTabs =新的任務欄(); RemoveTabs.RemoveUnusedTabs(strTabtoRemove);

+1

什麼是tp.Controls.Remove的意圖(這個);在RemoveUnusedTabs()中?你還可以發佈代碼調用RemoveUnusedTabs() – 2009-10-06 09:55:46

+0

增加了調用函數的代碼。只需爲該類創建一個實例並調用該函數即可。而已??任何問題 ?? 這就是我怎麼做我的附加功能也... – Anuya 2009-10-06 09:57:06

+0

@Frank Bollack,反正標籤頁TP在第一線返回NULL本身.... @ .... TabPage的TP = tabControl1.TabPages [strTabToRemove] – Anuya 2009-10-06 10:07:15

回答

2

我建議考慮看看this標籤頁例如用於添加/刪除選項卡。

下面是一個簡單的例子:

Form1.cs的文件:

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Text; 
using System.Windows.Forms; 

namespace TabPageExample 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
     } 

     private void mAddTabButton_Click(object sender, EventArgs e) 
     { 
      TabPage new_tab_page = new TabPage(); 
      if (!mTabNameTextBox.Text.Equals("")) 
      { 
       new_tab_page.Text = mTabNameTextBox.Text; 
       new_tab_page.Name = mTabNameTextBox.Text; 
       mTabControl.TabPages.Add(new_tab_page); 
       mTabNameTextBox.Text = ""; 
      } 
     } 

     private void mRemoveTabButton_Click(object sender, EventArgs e) 
     { 
      if (mTabControl.TabPages.Count > 0) 
      { 
       TabPage tab_page = mTabControl.TabPages[mTabNameTextBox.Text]; 
       if (tab_page != null) 
       { 
        mTabControl.TabPages.Remove(tab_page); 
       } 
      } 
      mTabNameTextBox.Text = ""; 
     } 
    } 
} 

這裏是Form1.Designer.cs

namespace TabPageExample 
{ 
    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.mTabControl = new System.Windows.Forms.TabControl(); 
      this.mAddTabButton = new System.Windows.Forms.Button(); 
      this.mRemoveTabButton = new System.Windows.Forms.Button(); 
      this.mTabNameTextBox = new System.Windows.Forms.TextBox(); 
      this.label1 = new System.Windows.Forms.Label(); 
      this.SuspendLayout(); 
      // 
      // mTabControl 
      // 
      this.mTabControl.Location = new System.Drawing.Point(12, 12); 
      this.mTabControl.Name = "mTabControl"; 
      this.mTabControl.SelectedIndex = 0; 
      this.mTabControl.Size = new System.Drawing.Size(499, 379); 
      this.mTabControl.TabIndex = 0; 
      // 
      // mAddTabButton 
      // 
      this.mAddTabButton.Location = new System.Drawing.Point(162, 408); 
      this.mAddTabButton.Name = "mAddTabButton"; 
      this.mAddTabButton.Size = new System.Drawing.Size(75, 23); 
      this.mAddTabButton.TabIndex = 1; 
      this.mAddTabButton.Text = "Add Tab"; 
      this.mAddTabButton.UseVisualStyleBackColor = true; 
      this.mAddTabButton.Click += new System.EventHandler(this.mAddTabButton_Click); 
      // 
      // mRemoveTabButton 
      // 
      this.mRemoveTabButton.Location = new System.Drawing.Point(243, 408); 
      this.mRemoveTabButton.Name = "mRemoveTabButton"; 
      this.mRemoveTabButton.Size = new System.Drawing.Size(75, 23); 
      this.mRemoveTabButton.TabIndex = 2; 
      this.mRemoveTabButton.Text = "Remove Tab"; 
      this.mRemoveTabButton.UseVisualStyleBackColor = true; 
      this.mRemoveTabButton.Click += new System.EventHandler(this.mRemoveTabButton_Click); 
      // 
      // mTabNameTextBox 
      // 
      this.mTabNameTextBox.Location = new System.Drawing.Point(194, 444); 
      this.mTabNameTextBox.Name = "mTabNameTextBox"; 
      this.mTabNameTextBox.Size = new System.Drawing.Size(100, 20); 
      this.mTabNameTextBox.TabIndex = 3; 
      // 
      // label1 
      // 
      this.label1.AutoSize = true; 
      this.label1.Location = new System.Drawing.Point(30, 447); 
      this.label1.Name = "label1"; 
      this.label1.Size = new System.Drawing.Size(158, 13); 
      this.label1.TabIndex = 4; 
      this.label1.Text = "Enter tab name to Add/Remove"; 
      // 
      // Form1 
      // 
      this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); 
      this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 
      this.ClientSize = new System.Drawing.Size(523, 476); 
      this.Controls.Add(this.label1); 
      this.Controls.Add(this.mTabNameTextBox); 
      this.Controls.Add(this.mRemoveTabButton); 
      this.Controls.Add(this.mAddTabButton); 
      this.Controls.Add(this.mTabControl); 
      this.Name = "Form1"; 
      this.Text = "Form1"; 
      this.ResumeLayout(false); 
      this.PerformLayout(); 

     } 

     #endregion 

     private System.Windows.Forms.TabControl mTabControl; 
     private System.Windows.Forms.Button mAddTabButton; 
     private System.Windows.Forms.Button mRemoveTabButton; 
     private System.Windows.Forms.TextBox mTabNameTextBox; 
     private System.Windows.Forms.Label label1; 
    } 
} 
相關問題