2009-10-06 5 views
1

J'ai collé deux fonctions de mon code. Ils servent à ajouter un onglet dans tabcontrol et à supprimer un onglet dans tabcontrol. Les deux fonctions sont dans le même formulaire où tabcontrol réside.Comment trouver l'onglet dans un tabcontrol pour le fermer en utilisant l'application C# windows

Je suis capable d'ajouter les onglets dans le tabcontrol avec facilité. J'appelle AddTab() d'une autre classe. Et ça fonctionne parfaitement. J'essaye de faire la même chose pour enlever un onglet d'une autre classe. Mais tabpage t renvoie toujours null même s'il y a encore deux onglets dans mon tabcontrol.

Qu'est-ce que je manque?

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); 
    } 

J'appelle les RemoveUnusedTabs d'une autre classe comme ci-dessous ..

// créer une instance pour cette classe. Barre des tâches RemoveTabs = nouvelle barre des tâches(); RemoveTabs.RemoveUnusedTabs (strTabtoRemove);

+1

Quelle est l'intention de tp.Controls.Remove (ce); dans RemoveUnusedTabs()? Pouvez-vous également publier le code appelant RemoveUnusedTabs() –

+0

Ajout du code pour appeler la fonction. Juste en créant une instance pour la classe et appelez la fonction. C'est tout?? aucun problème ?? c'est ainsi que je fais ma fonction d'ajout aussi .. – Anuya

+0

@Frank Bollack, de toute façon l'onglet tp renvoie NULL dans la première ligne elle-même .... @ .... TabPage tp = tabControl1.TabPages [strTabToRemove]; – Anuya

Répondre

2

Je vous recommande de jeter un oeil à l'exemple de la page d'onglet this pour ajouter/retirer des onglets.

Voici un exemple simple:

fichier 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 = ""; 
     } 
    } 
} 

est ici 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; 
    } 
} 
Questions connexes