c#为tabPage添加控件以及为TabControl控件新增选项卡

需要注意的是为tabPage添加控件使用的是TabPage的Control属性Add方法,而新增选项卡使用的是TabControl控件的TabPage属性Add方法

代码

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

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

        private void Form1_Load(object sender, EventArgs e)
        {
            
            tabPage1.ImageIndex = 0;
            tabPage2.ImageIndex = 0;
            Button b = new Button();
            b.Text = "测试";
            tabPage1.Controls.Add(b);//使用Controls属性Add方法将按钮添加到tabPage1中

            string tp3 = "tabPage3";//新的选项卡名称
            TabPage t = new TabPage(tp3);
            tabControl1.TabPages.Add(t);
            tabControl1.SizeMode = TabSizeMode.Normal;
            //设置选项卡属性为fixed
        }
    }
}

使用上述代码前,需要添加TabControl控件和一个ImageList控件,同时将TabControl的ImageList属性设置为你添加的那个ImageList控件

运行结果

运行结果.PNG

你可能感兴趣的:(c#为tabPage添加控件以及为TabControl控件新增选项卡)