How to delete a tab page created in one method from another method?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Alex Dransfield
    New Member
    • Jan 2011
    • 46

    How to delete a tab page created in one method from another method?

    For example, if I want to do this

    Code:
    private void ProcessLogin()
                {
                    TabPage welcomeTab = new TabPage("Welcome");
                    welcomeTab.BackColor = SystemColors.Control;
    
                    tabControl1.TabPages.Remove(loginTab);
                    tabControl1.TabPages.Add(welcomeTab);
                }
    and my tab control is created in this method

    Code:
    private void FirstLogin()
            {
                TabPage loginTab = new TabPage("Login");
                
                loginTab.BackColor = SystemColors.Control;
    
                tabControl1.TabPages.Add(loginTab); // add the tab
    
                Label loginLabel = new Label();
                loginLabel.Text = "You are required to log in before you can access the system.";
                loginLabel.AutoSize = true;
    
                Label userName = new Label();
                userName.Text = "Username: ";
                userName.AutoSize = true;
                userName.Location = new System.Drawing.Point(0, 30);
    
                TextBox userTB = new TextBox();
                userTB.Location = new System.Drawing.Point(61, 28);
                userTB.KeyPress += new System.Windows.Forms.KeyPressEventHandler(CheckKey);
    
                Label password = new Label();
                password.Text = "Password: ";
                password.AutoSize = true;
                password.Location = new System.Drawing.Point(0, 60);
                
                TextBox userPass = new TextBox();
                userPass.Location = new System.Drawing.Point(61, 58);
                userPass.PasswordChar = '*';
                userPass.KeyPress += new System.Windows.Forms.KeyPressEventHandler(CheckKey);
    
                Button loginButton = new Button();
                loginButton.Location = new System.Drawing.Point(0, 90);
                loginButton.Text = "Login";
                loginButton.Click += new System.EventHandler(loginButton_Click);
    
                loginTab.Controls.Add(loginLabel);
                loginTab.Controls.Add(userName);
                loginTab.Controls.Add(userTB);
                loginTab.Controls.Add(userPass);
                loginTab.Controls.Add(password);
                loginTab.Controls.Add(loginButton);
            }
    How do I remove it?
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    Doesn't feel like the best design, but you could retain the login form's tab as a property of the class. Then you'd have access to it from the other methods. After removing the tab from the TabPage, you could nullify it, and then it would be a candidate for GC (I assume).

    Mark.

    P.S. Not a .NET guy.

    Comment

    Working...