在C#中在运行时添加和删除选项卡页面

时间:2022-10-31 15:51:59

I have a program that I want each person to have their own tab, each tab would be identical, however I would like to remove a tab if I need to.

我有一个程序,我希望每个人都有自己的选项卡,每个选项卡都是相同的,但我想删除一个选项卡,如果我需要。

private void addPerson(string name)
{
  TabPage tmp = new TabPage();
  ListView tmpList = new ListView();
  Button tmpButton = new Button();
  this.SuspendLayout();
  this.tabFrame.SuspendLayout();
  tmp.SuspendLayout();
  tmpList.SuspendLayout();
  tmpButton.SuspendLayout();
  ...
  //build the controll itself
  tmp.Controls.Add(tmpButton);
  tmp.Controls.Add(tmpList);
  tmp.Location = new System.Drawing.Point(4, 22);
  tmp.Name = name.Replace(' ', '_');
  tmp.Padding = new System.Windows.Forms.Padding(3);
  tmp.Size = new System.Drawing.Size(284, 240);
  tmp.TabIndex = 3;
  tmp.Text = name;
  tmp.UseVisualStyleBackColor = true;
  //add it to frame
  this.tabFrame.Controls.Add(tmp);
  tmpButton.ResumeLayout(true);
  tmpList.ResumeLayout(true);
  tmp.ResumeLayout(true);
  this.tabFrame.ResumeLayout(true);
  this.ResumeLayout(true);
{

Name will be in the form "Scott Chamberlain" so I remove the spaces and use underscores for the name field. I can add tabs fine, they show up correctly formated, however when I try to remove the tab using the code:

名称将采用“Scott Chamberlain”的形式,因此我删除了空格并使用下划线作为名称字段。我可以添加标签,它们显示正确格式化,但是当我尝试使用代码删除标签时:

private void removePerson(string name)
{
  this.SuspendLayout();
  this.tabFrame.SuspendLayout();
  this.tabFrame.Controls.RemoveByKey(name.Replace(' ', '_'));
  this.tabFrame.ResumeLayout(true);
  this.ResumeLayout(true);
}

The tab does not disappear from my program. What am I missing to remove a tab?

该选项卡不会从我的程序中消失。删除标签时我错过了什么?

2 个解决方案

#1


4  

alt text http://www.codinghorror.com/blog/images/works-on-my-machine-starburst.png

替代文字http://www.codinghorror.com/blog/images/works-on-my-machine-starburst.png

Creating a simple TabPage with a specific Name and adding it to Controls or TabPages works and so does removing it with RemoveByKey on both Controls and TabPages.

创建一个具有特定名称的简单TabPage并将其添加到Controls或TabPages工作,因此在控件和TabPages上使用RemoveByKey删除它。

Is there any code that might later change the name?

是否有任何代码可能会在以后更改名称?

#2


1  

Use tabFrame.TabPages instead of tabFrame.Controls, for both the Add() and RemoveByKey() operations.

对于Add()和RemoveByKey()操作,请使用tabFrame.TabPages而不是tabFrame.Controls。

TabPages is a more specified version of Controls, and if such a situation occurs you are better of with the more specialized option.

TabPages是一个更具指定性的控件版本,如果出现这种情况,您可以使用更专业的选项。

#1


4  

alt text http://www.codinghorror.com/blog/images/works-on-my-machine-starburst.png

替代文字http://www.codinghorror.com/blog/images/works-on-my-machine-starburst.png

Creating a simple TabPage with a specific Name and adding it to Controls or TabPages works and so does removing it with RemoveByKey on both Controls and TabPages.

创建一个具有特定名称的简单TabPage并将其添加到Controls或TabPages工作,因此在控件和TabPages上使用RemoveByKey删除它。

Is there any code that might later change the name?

是否有任何代码可能会在以后更改名称?

#2


1  

Use tabFrame.TabPages instead of tabFrame.Controls, for both the Add() and RemoveByKey() operations.

对于Add()和RemoveByKey()操作,请使用tabFrame.TabPages而不是tabFrame.Controls。

TabPages is a more specified version of Controls, and if such a situation occurs you are better of with the more specialized option.

TabPages是一个更具指定性的控件版本,如果出现这种情况,您可以使用更专业的选项。