Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在tabcontrol中查找选项卡以使用c#windows应用程序将其关闭_C#_Winforms - Fatal编程技术网

如何在tabcontrol中查找选项卡以使用c#windows应用程序将其关闭

如何在tabcontrol中查找选项卡以使用c#windows应用程序将其关闭,c#,winforms,C#,Winforms,我已经从代码中粘贴了两个函数。它们用于在tabcontrol中添加选项卡和删除tabcontrol中的选项卡。这两个函数位于tabcontrol所在的同一表单中 我能够轻松地在tabcontrol中添加选项卡。我正在从另一个类调用AddTab()。而且它工作得很好 我正试图做同样的事情,从另一个类中删除选项卡。但是tabpage tp总是返回null,即使我的tabcontrol中还有两个选项卡 我错过了什么 public void AddTab(string strProcessNam

我已经从代码中粘贴了两个函数。它们用于在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,如下所示

//为该类创建一个实例。 任务栏移除abs=新任务栏();
移除选项卡。移除未使用的选项卡(strTabtoRemove)

我建议看一下选项卡页面示例,以添加/删除选项卡

下面是一个简单的例子:

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;
    }
}
名称空间选项卡页示例
{
部分类Form1
{
/// 
///必需的设计器变量。
/// 
private System.ComponentModel.IContainer components=null;
/// 
///清理所有正在使用的资源。
/// 
///如果应释放托管资源,则为true;否则为false。
受保护的覆盖无效处置(布尔处置)
{
if(处理和(组件!=null))
{
组件。Dispose();
}
基地。处置(处置);
}
#区域Windows窗体设计器生成的代码
/// 
///设计器支持所需的方法-不修改
///此方法的内容与代码编辑器一起使用。
/// 
私有void InitializeComponent()
{
this.mTabControl=new System.Windows.Forms.TabControl();
this.maddtabutton=new System.Windows.Forms.Button();
this.mremovetabutton=new System.Windows.Forms.Button();
this.mTabNameTextBox=new System.Windows.Forms.TextBox();
this.label1=new System.Windows.Forms.Label();
这个.SuspendLayout();
// 
//MTABC控制
// 
this.mTabControl.Location=新系统.Drawing.Point(12,12);
this.mTabControl.Name=“mTabControl”;
this.mTabControl.SelectedIndex=0;
this.mTabControl.Size=新系统.Drawing.Size(499379);
this.mTabControl.TabIndex=0;
// 
//马德塔布顿
// 
this.maddtabutton.Location=新系统图点(162408);
this.maddtabutton.Name=“maddtabutton”;
this.maddtabutton.Size=新系统.Drawing.Size(75,23);
this.maddtabutton.TabIndex=1;
this.maddtabutton.Text=“添加选项卡”;
this.maddtabutton.UseVisualStyleBackColor=true;
this.maddtabutton.Click+=newsystem.EventHandler(this.maddtabutton\u Click);
// 
//埃莫维塔布顿先生
// 
this.mremovetabutton.Location=新系统.图纸.点(243408);
this.mremovetabutton.Name=“mremovetabutton”;
this.mremovetabutton.Size=新系统.Drawing.Size(75,23);
this.mremovetabutton.TabIndex=2;
this.mremovetabutton.Text=“删除选项卡”;
this.mremovetabutton.UseVisualStyleBackColor=true;
this.mremovetabutton.Click+=newsystem.EventHandler(this.mremovetabutton\u Click);
// 
//mTabNameTextBox
// 
this.mTabNameTextBox.Location=新系统.Drawing.Point(194444);
this.mTabNameTextBox.Name=“mTabNameTextBox”;
this.mTabNameTextBox.Size=新系统.Drawing.Size(100,20);
this.mTabNameTextBox.TabIndex=3;
// 
//标签1
// 
this.label1.AutoSize=true;
this.label1.Location=新系统图纸点(30447);
this.label1.Name=“label1”;
this.label1.Size=新系统图纸尺寸(158,13);
this.label1.TabIndex=4;
this.label1.Text=“输入要添加/删除的选项卡名称”;
// 
//表格1
// 
此.AutoScaleDimensions=新系统.Drawing.SizeF(6F,13F);
this.AutoScaleMode=System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize=新系统图尺寸(523476);
this.Controls.Add(this.label1);
this.Controls.Add(this.mTabNameTextBox);
this.Controls.Add(this.mremovetabutton);
this.Controls.Add(this.maddtabutton);
this.Controls.Add(this.mTabControl);
this.Name=“Form1”;
this.Text=“Form1”;
此选项为.resume布局(false);
这个。执行布局();
}
#端区
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.label1;
}
}

tp.Controls.Remove(此)的意图是什么;在RemoveUnusedTabs()中?您还可以发布调用RemoveUnusedTabs()的代码,并添加调用该函数的代码。只是为类创建一个实例并调用函数。就这样??有什么问题吗??这也是我执行add函数的方式。@Frank Bollack,无论如何,tabpage tp在第一行本身返回NULL。@…tabpage tp=tabControl1.TabPages[strTabToRemove];