代码之家  ›  专栏  ›  技术社区  ›  Scott Chamberlain

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

  •  1
  • Scott Chamberlain  · 技术社区  · 16 年前

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

    名称将采用“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);
    }
    

    2 回复  |  直到 16 年前
        1
  •  4
  •   Glorfindel Doug L.    6 年前

    alt text
    (来源: codinghorror.com

    TabPage 与特定 Name 并将其添加到 Controls TabPages RemoveByKey 双方 控制 标签页 .

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

        2
  •  1
  •   Henk Holterman    16 年前

    使用 tabFrame.TabPages 而不是tabFrame。控件,用于Add()和RemoveByKey()操作。

    TabPages是控件的一个更具体的版本,如果发生这种情况,您最好使用更专门的选项。