Как найти вкладку в tabcontrol, чтобы закрыть ее с помощью приложения c # windows - PullRequest
1 голос
/ 06 октября 2009

Я вставил две функции из моего кода. Они предназначены для добавления вкладки в tabcontrol и удаления вкладки в tabcontrol. Обе функции находятся в одной и той же форме, где находится tabcontrol.

Я могу с легкостью добавлять вкладки в 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 Ответ

2 голосов
/ 06 октября 2009

Я бы рекомендовал взглянуть на этот пример страницы вкладок для добавления / удаления вкладок.

Вот простой пример:

Файл 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;
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...