程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C#-TabControl---ShinePans

C#-TabControl---ShinePans

編輯:C#入門知識

\\\

program.cs


using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace GroupBoxTest13
{
    static class Program
    {
        /// 
        /// 應用程序的主入口點。
        /// 
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
            //Appearance 獲取或設置控件選項卡的可視外觀
            //HotTrack 獲取或設置一個值,該值指示鼠標移到控件的選項卡時,這些選項卡是否更改外觀
            //ImageTrack 獲取或設置在控件的選項卡上顯示的圖像
            //Multiline 獲取或設置一個值,該值指示是否可以顯示一行以上的選項卡
            //SelectedIndex 獲取或設置當前選定的選項卡頁
            //TabCount 獲取選項卡條中選項卡的數目
            //TabPages 獲取該選項卡控件中選項卡的集合

            //DeselectTab 使指定的選項卡後面的選項卡成為當前選項卡
            //RemoveAll 從該選項卡控件中移除所有的選項卡和附加的控件
            //SelectTab使指定的選項卡成為當前選項卡



        }
    }
}

Form1.cs


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GroupBoxTest13
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            tabControl1.Appearance = TabAppearance.Normal;  //設置選項卡的外觀樣式

        }

        private void button1_Click(object sender, EventArgs e)
        {
            //聲明一個字符串變量,用於生成新增選項卡的名稱
            string Title = "新增選項卡" + (tabControl1.TabCount + 1).ToString();
            TabPage MyTabPage = new TabPage(Title);
            //使用TabControl控件的TabPages屬性的add方法添加新的選項卡
            tabControl1.TabPages.Add(MyTabPage);
            MessageBox.Show("現有" + tabControl1.TabCount + "個選項卡"); //獲取選項卡的個數

        }

        private void button2_Click(object sender, EventArgs e)
        {
            if(tabControl1.SelectedIndex==0)
            {
                MessageBox.Show("情選擇要移除的選項卡");
            }
            else
            {
                //使用TabControl控件的TabPages屬性的Remove方法移除指定的選項卡
                tabControl1.TabPages.Remove(tabControl1.SelectedTab);
            }
        }
    }
}

Form1設計:

namespace GroupBoxTest13
{
    partial class Form1
    {
        /// 
        /// 必需的設計器變量。
        /// 
        private System.ComponentModel.IContainer components = null;

        /// 
        /// 清理所有正在使用的資源。
        /// 
        /// 如果應釋放托管資源,為 true;否則為 false。
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows 窗體設計器生成的代碼

        /// 
        /// 設計器支持所需的方法 - 不要
        /// 使用代碼編輯器修改此方法的內容。
        /// 
        private void InitializeComponent()
        {
            this.tabControl1 = new System.Windows.Forms.TabControl();
            this.tabPage1 = new System.Windows.Forms.TabPage();
            this.tabPage2 = new System.Windows.Forms.TabPage();
            this.button1 = new System.Windows.Forms.Button();
            this.button2 = new System.Windows.Forms.Button();
            this.tabControl1.SuspendLayout();
            this.SuspendLayout();
            // 
            // tabControl1
            // 
            this.tabControl1.Controls.Add(this.tabPage1);
            this.tabControl1.Controls.Add(this.tabPage2);
            this.tabControl1.Location = new System.Drawing.Point(12, 12);
            this.tabControl1.Name = "tabControl1";
            this.tabControl1.SelectedIndex = 0;
            this.tabControl1.Size = new System.Drawing.Size(341, 128);
            this.tabControl1.TabIndex = 0;
            // 
            // tabPage1
            // 
            this.tabPage1.Location = new System.Drawing.Point(4, 22);
            this.tabPage1.Name = "tabPage1";
            this.tabPage1.Padding = new System.Windows.Forms.Padding(3);
            this.tabPage1.Size = new System.Drawing.Size(333, 102);
            this.tabPage1.TabIndex = 0;
            this.tabPage1.Text = "tabPage1";
            this.tabPage1.UseVisualStyleBackColor = true;
            // 
            // tabPage2
            // 
            this.tabPage2.Location = new System.Drawing.Point(4, 22);
            this.tabPage2.Name = "tabPage2";
            this.tabPage2.Padding = new System.Windows.Forms.Padding(3);
            this.tabPage2.Size = new System.Drawing.Size(192, 74);
            this.tabPage2.TabIndex = 1;
            this.tabPage2.Text = "tabPage2";
            this.tabPage2.UseVisualStyleBackColor = true;
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(151, 150);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(96, 23);
            this.button1.TabIndex = 1;
            this.button1.Text = "添加選項卡";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // button2
            // 
            this.button2.Location = new System.Drawing.Point(253, 150);
            this.button2.Name = "button2";
            this.button2.Size = new System.Drawing.Size(96, 23);
            this.button2.TabIndex = 2;
            this.button2.Text = "移除選項卡";
            this.button2.UseVisualStyleBackColor = true;
            this.button2.Click += new System.EventHandler(this.button2_Click);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(365, 185);
            this.Controls.Add(this.button2);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.tabControl1);
            this.Name = "Form1";
            this.Text = "選項卡";
            this.Load += new System.EventHandler(this.Form1_Load);
            this.tabControl1.ResumeLayout(false);
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.TabControl tabControl1;
        private System.Windows.Forms.TabPage tabPage1;
        private System.Windows.Forms.TabPage tabPage2;
        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.Button button2;

    }
}


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved