C # Use the tabControl control to embed and disable multiple forms

Source: Internet
Author: User

Create a main form (Formmain) and two secondary forms (Form1, Form2). Add a menuStrip control and a tabControl control respectively to the main form, add a main menu and two sub-menus to the menu control. For example, select the tabControl control attribute and modify DrawMode = OwnerDrawFixed. Then, add the following code: public void Add_TabPage (string str, form myForm) {if (tabControlCheckHave (this. mainTabControl, str) {return;} else {MainTabControl. tabPages. add (str); MainTabControl. selectTab (MainTabControl. tabPages. count-1); myForm. formBorderStyle = F OrmBorderStyle. none; myForm. dock = DockStyle. fill; myForm. topLevel = false; myForm. show (); myForm. parent = MainTabControl. selectedTab;} public bool tabControlCheckHave (System. windows. forms. tabControl tab, String tabName) {for (int I = 0; I <tab. tabCount; I ++) {if (tab. tabPages [I]. text = tabName) {tab. selectedIndex = I; return true;} return false;} private void form1ToolStripMenuItem_Cl Ick (object sender, EventArgs e) {Form1 f1 = new Form1 (); Add_TabPage ("f1 People's Republic of China forms", f1);} private void form2ToolStripMenuItem_Click (object sender, EventArgs e) {Form2 f2 = new Form2 (); Add_TabPage ("f2 North Korean form", f2);} private void MainTabControl_DrawItem (object sender, DrawItemEventArgs e) {/* If you set the DrawMode attribute to OwnerDrawFixed, it will trigger the DrawItem event */try {this. mainTabCont Rol. tabPages [e. index]. backColor = Color. lightBlue; Rectangle tabRect = this. mainTabControl. getTabRect (e. index); e. graphics. drawString (this. mainTabControl. tabPages [e. index]. text, this. font, SystemBrushes. controlText, (float) (tabRect. X + 2), (float) (tabRect. Y + 2); using (Pen pen = new Pen (Color. white) {tabRect. offset (tabRect. width-15, 2); tabRect. width = 15; tabRect. height = 15; e. graphics. dra WRectangle (pen, tabRect);} Color color = (e. State = DrawItemState. Selected )? Color. lightBlue: Color. white; using (Brush brush = new SolidBrush (color) {e. graphics. fillRectangle (brush, tabRect);} using (Pen pen2 = new Pen (Color. red) {Point point = new Point (tabRect. X + 3, tabRect. Y + 3); Point point2 = new Point (tabRect. X + tabRect. width)-3, (tabRect. Y + tabRect. height)-3); e. graphics. drawLine (pen2, point, point2); Point point3 = new Point (tabRect. X + 3, (tabRect. Y + tabRect. height)-3); Point point4 = new Point (tabRect. X + tabRect. width)-3, tabRect. Y + 3); e. graphics. drawLine (pen2, point3, point4);} e. graphics. dispose ();} catch (Exception ex) {MessageBox. show (ex. message) ;}} private void MainTabControl_MouseDown (object sender, MouseEventArgs e) {if (e. button = MouseButtons. left) {www.2cto.com int x = e. x; int y = e. y; Rectangle tabRect = this. mainTabControl. getTabRect (this. mainTabControl. selectedIndex); tabRect. offset (tabRect. width-0x12, 2); tabRect. width = 15; tabRect. height = 15; if (x> tabRect. x) & (x <tabRect. right) & (y> tabRect. y) & (y <tabRect. bottom) {this. mainTabControl. tabPages. remove (this. mainTabControl. selectedTab );}}}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.