我想从TabControl的删除一个特定的标签页。对此我有它已被关闭的标签名称的价值。
但是,当我使用..
的for(int i = 0; I< myTabControl.TabPages.Count;我++)
{
如果(myTabControl.TabPages [I] .Name.Equals(tabToRemove,StringComparison.OrdinalIgnoreCase))
{
myTabControl.TabPages.RemoveAt(ⅰ);
打破;
}
}
有不会在循环内,因为计数为零。 而在TabControl的是,在这两个选项卡可见。
什么问题?
这是我如何添加选项卡 - >
公共无效TabIt(字符串strProcessName)
{
this.Show();
//创建MDI子窗体和初始化其字段
MDIChild childForm =新MDIChild();
childForm.Text = strProcessName;
childForm.MdiParent =这一点;
//子窗体将现在持有的参考价值标签控件
childForm.TabCtrl = tabControl1;
//添加一个标签页,并把它
TabPage的TP =新的TabPage();
tp.Parent = tabControl1;
tp.Text = childForm.Text;
tp.Show();
//子窗体将现在持有的参考值,以一个标签页
childForm.TabPag = TP;
//激活MDI子窗体
childForm.Show();
childCount ++;
//激活新创建的标签页。
tabControl1.SelectedTab = TP;
tabControl1.ItemSize =新的大小(200,32);
tp.Height = tp.Parent.Height;
tp.Width = tp.Parent.Width;
}
公共无效GetTabNames()
{
的foreach(在Global.TabProcessNames串strProcessName)
{
TabIt(strProcessName);
}
}
该子窗体:
使用系统;使用System.Drawing中; System.Collections中使用;使用System.ComponentModel;使用System.Windows.Forms的;使用System.Data这;使用System.Diagnostics程序;使用System.Drawing.Drawing2D;
命名空间守护进程
{
公共类MDIChild:System.Windows.Forms.Form中
{
///<总结>
///必需的设计变量。
///< /总结>
私人System.ComponentModel.Container成分= NULL;
私人的TabControl tabCtrl;
私人TabPage的tabPag;
公共MDIChild()
{
//
//必需的Windows窗体设计器支持
//
的InitializeComponent();
// MDIChild TargerForm =新MDIChild();
//WinApi.SetWinFullScreen(TargerForm.Handle);
//
// TODO:在InitializeComponent调用后添加任何构造函数code
//
}
///<总结>
///清理所有正在使用的资源。
///< /总结>
保护覆盖无效的Dispose(BOOL处置)
{
如果(处置)
{
如果(部件!= NULL)
{
components.Dispose();
}
}
base.Dispose(处置);
}
公共TabPage的TabPag
{
得到
{
返回tabPag;
}
组
{
tabPag =价值;
}
}
公众的TabControl TabCtrl
{
组
{
tabCtrl =价值;
}
}
#region Windows窗体设计器生成的code
///<总结>
///设计器支持所需的方法 - 不要修改
///此方法的code编辑器的内容。
///< /总结>
私人无效的InitializeComponent()
{
this.SuspendLayout();
//
// MDIChild
//
this.AutoScaleBaseSize =新System.Drawing.Size(5,13);
this.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
this.BackColor = System.Drawing.SystemColors.InactiveCaptionText;
this.ClientSize =新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 + =新System.EventHandler(this.MDIChild_Activated);
this.Closing + =新System.ComponentModel.CancelEventHandler(this.MDIChild_Closing);
this.ResumeLayout(假);
}
#endregion
私人无效MDIChild_Closing(对象发件人,System.ComponentModel.CancelEventArgs E)
{
尝试
{
//关闭MDI子窗体时销毁相应的标签页
this.tabPag.Dispose();
//如果没有标签页留下
如果(!tabCtrl.HasChildren)
{
tabCtrl.Visible = FALSE;
}
}
赶上(例外前)
{
}
}
私人无效MDIChild_Activated(对象发件人,发送System.EventArgs)
{
尝试
{
//激活相应的标签页
tabCtrl.SelectedTab = tabPag;
如果(!tabCtrl.Visible)
{
tabCtrl.Visible = TRUE;
}
Global.ExistingTabProcessNames.Add(tabPag.Text);
}
赶上(例外前)
{
}
}
}
}
解决方案
如果您有TabPage的什么是错的这样做只是这...
的名称 tabControl1.TabPages.RemoveByKey(tabPage1);
I want to remove a particular tabpage from the tabcontrol. For which i have the value of tab name which has to be closed.
But, when i use..
for (int i = 0; i < myTabControl.TabPages.Count; i++)
{
if (myTabControl.TabPages[i].Name.Equals(tabToRemove, StringComparison.OrdinalIgnoreCase))
{
myTabControl.TabPages.RemoveAt(i);
break;
}
}
It is not going inside the loop because the count is zero. whereas the the tabcontrol is visible with two tabs in it.
whats the problem ?
This is how i am adding the tabs ->
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);
}
}
The child form :
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
{
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)
{
}
}
}
}
解决方案
If you have the name of the TabPage what is wrong with doing just this...
tabControl1.TabPages.RemoveByKey("tabPage1");
?