Monday, June 4, 2012

HOW TO ADD TABPAGES IN TABCONTROL DYNAMICALLY IN A C#.NET WINDOW APPLICATION

Today I will tell you about usingTab Control in a Window Application Form. The Tab Control is mainly used to show ourWinForms together as aTabPages. Also I am going to share that how to add TabPages at run time or we can say that addTabpages dynamically. In this project you will learn many useful things about C# WinForms andTab Control.

Free Download





Namespaces

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
 
Global Variables:
private int childFormNumber = 0;
 public bool access;
 public int pgidx = -1;

Code Page:

1. Show Item after click on Menu StripControl

private void menuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            string itemname = e.ClickedItem.Text;
            switch (itemname)
            {
                case "Insert Ticket": InsertTicket objticket = new InsertTicket();
                    access = addTabPage(itemname, objticket);
                    if (access == true)
                    {
                        objticket.Show();
                    }
                    break;
                case "Show Ticket": Show_Tickets objshowticket =
new Show_Tickets();
                    access = addTabPage(itemname, objshowticket);
                    if (access == true)
                    {
                        objshowticket.Show();
                    }
                    break;
                case "Show All Lottery": ShowAllLottery objshowlottery =
new ShowAllLottery();
                    access = addTabPage(itemname, objshowlottery);
                    if (access == true)
                    {
                        objshowlottery.Show();
                    }
                    break;
                case "Generate Lottery": GenerateLottery objgenlottery =
new GenerateLottery();
                    access = addTabPage(itemname, objgenlottery);
                    if (access == true)
                    {
                        objgenlottery.Show();
                    }
                    break;
                case "How to use Software": Help objhelp = new Help();
                    access = addTabPage(itemname, objhelp);
                    if (access == true)
                    {
                        objhelp.Show();
                    }
                    break;
                case "Logout": Application.Exit();
                    break;
            }
        }
2. Adding TabPages dynamically
public bool addTabPage(string PageName, Form FormName)
        {
            bool access = true;
            for (int j = 0; j < tbLottery.TabCount; j++)
            {

                if (tbLottery.TabPages[j].Text == PageName)
                {
                    access = false;
                }
            }
            if (access == true)
            {
                FormName.MdiParent = this;
                TabPage tbPage = new TabPage();
                tbPage.Text = PageName;
                tbPage.Controls.Add(FormName);
                tbPage.CreateControl();
                tbLottery.TabPages.Add(tbPage);
                int i = tbLottery.TabPages.Count;
                pgidx += 1;
                tbLottery.SelectedIndex = pgidx;
            }
            return access;

        }
3. Close TabPages by their order
private void btnClose_Click(object sender, EventArgs e)
        {
            if (pgidx >= 0)
            {
                int i = this.tbLottery.SelectedIndex;
                tbLottery.TabPages.RemoveAt(i);
                pgidx -= 1;
                tbLottery.SelectedIndex = pgidx;
            }
        }

No comments:

Post a Comment