tags:

views:

300

answers:

2

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 { /// /// Summary description for MDIChild. /// ///

public class MDIChild : System.Windows.Forms.Form { /// /// Required designer variable. /// 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)
    { 
    }
    }

}

}

+1  A: 

If you have the name of the TabPage what is wrong with doing just this...

tabControl1.TabPages.RemoveByKey("tabPage1");

?

Chalkey
Tried the above.. No errors.. But it is not removing the tab.
Anuya
Are you passing it the correct tab name?
Chalkey
Yes it is correct.Anyway, The tabcontrol shows like tabpages.count = o when i debug
Anuya
If the count is zero then i guess the tab pages are not being added correctly.
Chalkey
If the tabpages are not added properly, then how do i get the tabpages.I have already added the tabpages. Now the problem is with removing the tab from tabcontrol.
Anuya
Put some code up showing how you are adding the controls. There could be an issue at that point.
Chalkey
@Chalkey, i have added the code.
Anuya
@karthik - it doesn't look like you are adding the TagPage to the TabControl. After you create your TabPage item, you need to do something like tabControl1.TabPages.Add(tp);
Chalkey
A: 

You tried instead the RemoveByKey()-Sub?

Me.yourTabPageControl.TabPages.RemoveByKey(tabToRemove);

Also check when you're calling this.

Bobby

Bobby
'Me' is the VB.NET syntax. 'this' would be the word you are after.
Chalkey
Tried the above.. No errors.. But it is not removing the tab.
Anuya
The tabcontrol shows like tabpages.count = o when i debug
Anuya
@Chalkey: Oh yeah...I knew there was something wrong.@Karthik: _When_ do you call that code? And a long shot...how about simply disposing the TabPage? (Should also show up in the Controls() collection of the container)
Bobby