当我将每个标签引用到子表单时,如何从tabcontrol中删除标签。
我正在使用tabcontrol,我想从控件中删除特定的选项卡。我必须在动态字符串中执行此操作。
如何使用字符串中现有的选项卡名称从tabcontrol中删除选项卡?
我尝试了类似......
string tabToRemove = "tabPageName";
for (int i = 0; i < myTabControl.TabPages.Count; i++)
{
if (myTabControl.TabPages[i].Name.Equals(tabToRemove, StringComparison.OrdinalIgnoreCase))
{
myTabControl.TabPages.RemoveAt(i);
break;
}
}
但是在上面的代码中,myTabControl.TabPages.Count始终为零。
下面是代码,显示我正在创建选项卡。这很完美。
public void TabIt(string strProcessName)
{
this.Show();
//Creating MDI child form and initialize its fields
MDIChild childForm = new MDIChild();
childForm.Text = strProcessName;
childForm.MdiParent = this;
//child Form will now hold a reference value to the tab control
childForm.TabCtrl = tabControl1;
//Add a Tabpage and enables it
TabPage tp = new TabPage();
tp.Parent = tabControl1;
tp.Text = childForm.Text;
tp.Show();
//child Form will now hold a reference value to a tabpage
childForm.TabPag = tp;
//Activate the MDI child form
childForm.Show();
childCount++;
//Activate the newly created Tabpage.
tabControl1.SelectedTab = tp;
tabControl1.ItemSize = new Size(200, 32);
tp.Height = tp.Parent.Height;
tp.Width = tp.Parent.Width;
}
public void GetTabNames()
{
foreach (string strProcessName in Global.TabProcessNames)
{
TabIt(strProcessName);
}
}
儿童表格:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Diagnostics;
using System.Drawing.Drawing2D;
namespace Daemon
{
/// <summary>
/// Summary description for MDIChild.
/// </summary>
///
public class MDIChild : System.Windows.Forms.Form
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
private TabControl tabCtrl;
private TabPage tabPag;
public MDIChild()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//MDIChild TargerForm = new MDIChild();
//WinApi.SetWinFullScreen(TargerForm.Handle);
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
public TabPage TabPag
{
get
{
return tabPag;
}
set
{
tabPag = value;
}
}
public TabControl TabCtrl
{
set
{
tabCtrl = value;
}
}
#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.SuspendLayout();
//
// MDIChild
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
this.BackColor = System.Drawing.SystemColors.InactiveCaptionText;
this.ClientSize = new System.Drawing.Size(0, 0);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "MDIChild";
this.Opacity = 0;
this.ShowIcon = false;
this.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Hide;
this.Text = "MDIChild";
this.Activated += new System.EventHandler(this.MDIChild_Activated);
this.Closing += new System.ComponentModel.CancelEventHandler(this.MDIChild_Closing);
this.ResumeLayout(false);
}
#endregion
private void MDIChild_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
try
{
//Destroy the corresponding Tabpage when closing MDI child form
this.tabPag.Dispose();
//If no Tabpage left
if (!tabCtrl.HasChildren)
{
tabCtrl.Visible = false;
}
}
catch (Exception ex)
{
}
}
private void MDIChild_Activated(object sender, System.EventArgs e)
{
try
{
//Activate the corresponding Tabpage
tabCtrl.SelectedTab = tabPag;
if (!tabCtrl.Visible)
{
tabCtrl.Visible = true;
}
Global.ExistingTabProcessNames.Add(tabPag.Text);
}
catch (Exception ex)
{
}
}
}
}
答案 0 :(得分:2)
对于初学者来说,你应该反过来循环:
for (int i = myTabControl.TabPages.Count - 1; i >= 0 ; i--)
{
......
}
编辑:不理我。我错过了休息;是的,它也应该是&gt; = :(
我的下一个理论是你错过了这一行:
// add the page to the tab control
tabControl1.Controls.Add(tp);
PS:为什么从SO复制代码不能维护CRLF。那太烦人了!
答案 1 :(得分:1)
我不知道你的代码的目的,但如果你最终会重新添加标签,为什么不隐藏它呢?它更容易,一旦页面被隐藏,您就不必担心反向循环逻辑和无效参数。如果您需要一种方法来同时处理所有选项卡,只需检查可见选项卡......
答案 2 :(得分:1)
正如jussij建议的那样,你需要这样做:
tabControl1.Controls.Add(tp);
您可以更轻松地找到这样的标签:
var foundTab = (from System.Windows.Forms.TabPage tab in tabControl1.TabPages.Cast<TabPage>()
where tab.Name == "tabName"
select tab).First();
答案 3 :(得分:0)
我建议您查看this标签页示例,以添加/删除标签。