我想从tabcontrol中删除特定的标签页。为此,我必须关闭标签名称的值。
但是,当我使用..
for (int i = 0; i < myTabControl.TabPages.Count; i++)
{
if (myTabControl.TabPages[i].Name.Equals(tabToRemove, StringComparison.OrdinalIgnoreCase))
{
myTabControl.TabPages.RemoveAt(i);
break;
}
}
它不会进入循环内部,因为计数为零。 而tabcontrol是可见的,其中有两个标签。
问题是什么?
这就是我添加标签的方式 - &gt;
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
{
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 :(得分:1)
如果你有TabPage的名字那么做这个有什么问题......
tabControl1.TabPages.RemoveByKey("tabPage1");
答案 1 :(得分:1)
对不起,Anuya,如果没有完整的代码,很难说出哪些对您有用或可能没用,但是您可以考虑以下三个想法或想法:
第一个考虑因素:
当你将TabPages添加到TabControl时,我注意到你似乎并没有真正将它添加到TabControl的Controls集合中。我真的认为你可能需要在添加TabPage时添加tabControl1.Controls.Add(tp);
以将其真正添加到TabControl的TabPages的“Count”中。
第二个考虑因素:
我不知道调用TabPage.Dispose将从TabControl中删除TabPage,但它可能会起作用。当我删除TabPages时,我使用TabControl.Remove()方法。
第三个考虑因素:
我有一些TabControl问题,我解决它的唯一方法是为我的项目添加一个计时器,然后在下一个计时器滴答,做一些事情。所以我最终用自己能做的不同动作编写了自己的动作处理器,并且我有一个动作列表,其中包含应该处理该动作的时间戳。动作列表最终对很多东西非常有用,但我首先需要它用于我的TabControl。在我的情况下,我需要显示一个标签页,但是当我添加一个TabPage时,即使我在添加后设置了选定的标签页,它也无法正确显示。但是当我在添加标签页后选择了我想要的大约100毫秒的标签页时,它完美无缺,只有一点点丑陋:标签控件'闪烁'。也就是说,用户可以看到标签页闪烁,因为它实际绘制了新添加的标签页,然后100毫秒后它选择了我想要的标签页。但这是我解决问题的唯一方法。我想知道你的问题是否与我的问题有关,也许你现在可以在100毫秒后删除标签页。
这是三个考虑因素,我怀疑我的第一个选择可能会解决你的第一个问题。
快乐的编码!!
答案 2 :(得分:0)
foreach(TabPage page in tabctr.TabPages)
{
if (page.Name == "tabPage2")
{
tabctr.TabPages.Remove(page);
}
}
答案 3 :(得分:0)
虽然您的代码像添加任何控件一样将选项卡页添加到窗体,但是并没有将选项卡页添加到TabControl的TabPages集合中,这就是为什么以后找不到它的原因。
添加如下所示的标签页,这样它将显示在TabPages集合中:
// helper - create a new tabpage
static private TabPage _AddTabPage( TabControl tabControl, string caption )
{
int pageIndex = tabControl.TabPages.Count;
tabControl.TabPages.Add(caption);
return tabControl.TabPages[pageIndex];
}