[英]Adding and removing tab pages at runtime in C#
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. 名称将采用“ Scott Chamberlain”的形式,因此我删除了空格,并在名称字段中使用了下划线。 I can add tabs fine, they show up correctly formated, however when I try to remove the tab using the code:
我可以很好地添加选项卡,它们显示的格式正确,但是当我尝试使用代码删除选项卡时:
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?
我缺少删除标签的内容吗?
(source: codinghorror.com ) (来源: codinghorror.com )
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
. 创建具有特定
Name
的简单TabPage
并将其添加到Controls
或TabPages
是可行的,使用Controls
和TabPages
上的RemoveByKey
将其删除也是如此。
Is there any code that might later change the name? 是否有任何可能稍后更改名称的代码?
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是控件的指定版本,如果发生这种情况,最好使用更专业的选项。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.