寫Visual的同學們都會用到這個TabControl的控件,然後會分好幾頁的TabPage,每頁都有很多控件和業務邏輯,但是每頁的關係也不是很大,但是好幾頁的代碼都集中到一個Form裏面了。就造成了一個Form.cs有幾百幾千行的代碼,很擁擠,不合適。
現在推薦一個方法,就能夠把每頁的TabPage單獨分割成一個Form
新建TabPageForm1
調整屬性this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
隨意擺4個button
新建TabPageForm2
同樣調整屬性this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
隨意放一個picturebox
新建Form1.cs
就放一個TabControl
然後代碼
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 TabControlForm
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
TabPageForm1 tabPageForm1 = new TabPageForm1();
TabPageForm2 tabPageForm2 = new TabPageForm2();
List<Form> formList = new List<Form>();
private void Form1_Load(object sender, EventArgs e)
{
formList.Add(tabPageForm1);
formList.Add(tabPageForm2);
for (int i = 0; i < tabControl1.TabPages.Count; i++)
{
if (i >= formList.Count)
break;
Form f = formList[i];
f.Dock = DockStyle.Fill;
f.TopLevel = false;
f.FormBorderStyle = FormBorderStyle.None;
f.Show();
tabControl1.TabPages[i].Controls.Clear();
tabControl1.TabPages[i].Controls.Add(f);
}
}
}
}
大功告成
這樣就大功告成了,代碼完美的分隔開了