C # override the TabControl control to implement the close button Method

Source: Internet
Author: User

1. c # The TabControl control in it does not have a close button, and it is ugly.

2. There are some third-party controls that are ready for use, but they are charged.

3. Because my fault tree inference and diagnosis project allows multiple documents to be opened at the same time during plotting, it is necessary to implement the multi-tag function similar to the browser and disable it.

4. Write a class to inherit the TabControl class, and then rewrite some methods to implement it.

5. features: the button is closed, and the tag has a background color. The selected tag is different from the unselected color, enabling the shortcut and shortcut buttons.

Check out the complete code in my project. Many codes are required by my project. You can delete them according to your project requirements. The core code will be explained in detail below:

Copy codeThe Code is as follows: // <summary>
/// Overwrite the TabControl with the close button
/// </Summary>
Public class MyTabControl: TabControl
{
Private int iconWidth = 16;
Private int iconHeight = 16;
Private Image icon = null;
Private Brush biaocolor = Brushes. Silver; // the background color of the tab
Private Form_paint father; // The parent window, that is, the drawing interface, in order to call the dispose event of the parent window to close the parent window after the tab is fully closed
Private AxMicrosoft. Office. Interop. VisOcx. AxDrawingControl axDrawingControl1;
Public MyTabControl (AxMicrosoft. Office. Interop. VisOcx. AxDrawingControl axDrawingControl)
: Base ()
{
This. axDrawingControl1 = axDrawingControl;
This. ItemSize = new Size (50, 25); // you can set the Size of the tab label to change the height. The width cannot be changed.
// This. Appearance = TabAppearance. Buttons; // display mode of the tab
This. DrawMode = TabDrawMode. OwnerDrawFixed;
Icon = Properties. Resources. close. ToBitmap ();
IconWidth = icon. Width; iconHeight = icon. Height;
}
/// <Summary>
/// Set the parent window
/// </Summary>
/// <Param name = "fp"> drawing window </param>
Public void setFather (Form_paint fp)
{
This. father = fp;
}
/// <Summary>
/// Rewrite the draw event
/// </Summary>
/// <Param name = "e"> </param>
Protected override void OnDrawItem (DrawItemEventArgs e) // rewrite the draw event.
{
Graphics g = e. Graphics;
Rectangle r = GetTabRect (e. Index );
If (e. Index = this. SelectedIndex) // The Selected Tab page. set different styles to show the Selected Tab page.
{
Brush selected_color = Brushes. Gold; // the background color of the selected item
G. FillRectangle (selected_color, r); // you can change the background color of a tab.
String title = this. TabPages [e. Index]. Text + "";
G. drawString (title, this. font, new SolidBrush (Color. black), new PointF (r. X + 3, r. Y + 6); // position of the PointF tab title
R. Offset (r. Width-iconWidth-3, 2 );
G. drawImage (icon, new Point (r. x-2, r. Y + 2); // The Position of the icon on the tab. fntTab = new System. drawing. font (e. font, FontStyle. bold );
}
Else // unselected
{
G. FillRectangle (biaocolor, r); // you can change the background color of a tab.
String title = this. TabPages [e. Index]. Text + "";
G. drawString (title, this. font, new SolidBrush (Color. black), new PointF (r. X + 3, r. Y + 6); // position of the PointF tab title
R. Offset (r. Width-iconWidth-3, 2 );
G. DrawImage (icon, new Point (r. X-2, r. Y + 2); // The Position of the icon on the tab
}
}

Protected override void OnMouseClick (MouseEventArgs e)
{
# Left-click region to determine whether the region is closed
If (e. Button = MouseButtons. Left)
{
Point p = e. Location;
Rectangle r = GetTabRect (this. SelectedIndex );
R. Offset (r. Width-iconWidth-3, 2 );
R. Width = iconWidth;
R. Height = iconHeight;
If (r. Contains (p) // click a specific region.
{
String temp = this. SelectedTab. Text;
If (temp [temp. Length-5] = '*') // Save the changes
{
// Confirm whether to save the sealing file to ft_doc_Path
DialogResult response = MessageBox. Show ("whether to save Fault Tree" + this. SelectedTab. Name + "to graphic file", "Please confirm", MessageBoxButtons. YesNoCancel,
MessageBoxIcon. Question, MessageBoxDefaultButton. Button2 );
If (response = System. Windows. Forms. DialogResult. Yes) // confirm to save
{
AxDrawingControl1.Document. SaveAs (GlobalVariables. ft_doc_Path + axDrawingControl1.Document. Title + ". Sealing"); // Save the current document to the folder
String datetime = DateTime. Now. ToString (); // obtain the current time
HelpTool. saveVsdDB (axDrawingControl1.Document. Title, datetime); // Save the HPA document to the database
HelpTool. setDatetimeToXml (axDrawingControl1.Document. Title, datetime); // if the information already exists, update the date in xml. If the information does not exist, insert it directly.

This. SelectedTab. Text = temp. Substring (0, temp. Length-5) + ""; // cancel the asterisk mark after saving and restore it to the style when there is no change
}
Else if (response = System. Windows. Forms. DialogResult. Cancel) // click Cancel or close
{
Return; // exit directly to cancel the program Close event.
}
}
If (this. TabCount = 1) // is the last tab. Close the parent interface, that is, the drawing interface.
{
Father. DisposeForTabControl (true );
}
Else // not the last one
{
This. TabPages. Remove (this. SelectedTab );
}
}
}
# Endregion
# Right-click region and select
Else if (e. Button = MouseButtons. Right) // Right-click to select
{
For (int I = 0; I <this. TabPages. Count; I ++)
{
TabPage tp = this. TabPages [I];
If (this. GetTabRect (I). Contains (new Point (e. X, e. Y )))
{
This. SelectedTab = tp;
Break;
}
}
}
# Endregion
# Disable the key in region
Else if (e. Button = MouseButtons. Middle) // close the mouse
{
For (int I = 0; I <this. TabPages. Count; I ++)
{
TabPage tp = this. TabPages [I];
If (this. GetTabRect (I). Contains (new Point (e. X, e. Y) // close
{
This. SelectedTab = tp;
String temp = tp. Text;
If (temp [temp. Length-5] = '*') // Save the changes
{
// Confirm whether to save the sealing file to ft_doc_Path
DialogResult response = MessageBox. Show ("whether to save Fault Tree" + this. SelectedTab. Name + "to graphic file", "Please confirm", MessageBoxButtons. YesNoCancel,
MessageBoxIcon. Question, MessageBoxDefaultButton. Button2 );
If (response = System. Windows. Forms. DialogResult. Yes) // confirm to save
{
AxDrawingControl1.Document. SaveAs (GlobalVariables. ft_doc_Path + axDrawingControl1.Document. Title + ". Sealing"); // Save the current document to the folder
String datetime = DateTime. Now. ToString (); // obtain the current time
HelpTool. saveVsdDB (axDrawingControl1.Document. Title, datetime); // Save the HPA document to the database
HelpTool. setDatetimeToXml (axDrawingControl1.Document. Title, datetime); // if the information already exists, update the date in xml. If the information does not exist, insert it directly.

This. SelectedTab. Text = temp. Substring (0, temp. Length-5) + ""; // cancel the asterisk mark after saving and restore it to the style when there is no change
}
Else if (response = System. Windows. Forms. DialogResult. Cancel) // click Cancel or close
{
Return; // exit directly to cancel the program Close event.
}
}
If (this. TabCount = 1) // is the last tab. Close the parent interface, that is, the drawing interface.
{
Father. DisposeForTabControl (true );
}
Else // not the last one
{
This. TabPages. Remove (this. SelectedTab );
}

Break;
}
}
}
# Endregion
}
}

The key code for implementing the close button is to override the OnDrawItem (DrawItemEventArgs e) method:

Copy codeThe Code is as follows: protected override void OnDrawItem (DrawItemEventArgs e) // rewrite the draw event.
{
Graphics g = e. Graphics;
Rectangle r = GetTabRect (e. Index );
If (e. Index = this. SelectedIndex) // The Selected Tab page. set different styles to show the Selected Tab page.
{
Brush selected_color = Brushes. Gold; // the background color of the selected item
G. FillRectangle (selected_color, r); // you can change the background color of a tab.
String title = this. TabPages [e. Index]. Text + "";
G. drawString (title, this. font, new SolidBrush (Color. black), new PointF (r. X + 3, r. Y + 6); // position of the PointF tab title
R. Offset (r. Width-iconWidth-3, 2 );
G. drawImage (icon, new Point (r. x-2, r. Y + 2); // The Position of the icon on the tab. fntTab = new System. drawing. font (e. font, FontStyle. bold );
}
Else // unselected
{
G. FillRectangle (biaocolor, r); // you can change the background color of a tab.
String title = this. TabPages [e. Index]. Text + "";
G. drawString (title, this. font, new SolidBrush (Color. black), new PointF (r. X + 3, r. Y + 6); // position of the PointF tab title
R. Offset (r. Width-iconWidth-3, 2 );
G. DrawImage (icon, new Point (r. X-2, r. Y + 2); // The Position of the icon on the tab
}
}

The if-else is used to determine whether the current tab is selected, so that the selected color is different from that of the unselected tab, and the items that are not required in the project can be removed.


The specific principle of implementing the disable function is to override the protected override void OnMouseClick (MouseEventArgs e) method, and the left-click operation triggers the corresponding event to determine whether the clicked region is in the area of the close button, disable the function. In addition, you can process the middle and right-click buttons. Based on your project, modify the code under the corresponding button event.

Copy codeThe Code is as follows: protected override void OnMouseClick (MouseEventArgs e)
{
# Left-click region to determine whether the region is closed
If (e. Button = MouseButtons. Left)
{
Point p = e. Location;
Rectangle r = GetTabRect (this. SelectedIndex );
R. Offset (r. Width-iconWidth-3, 2 );
R. Width = iconWidth;
R. Height = iconHeight;
If (r. Contains (p) // click a specific region.
{
String temp = this. SelectedTab. Text;
If (temp [temp. Length-5] = '*') // Save the changes
{
// Confirm whether to save the sealing file to ft_doc_Path
DialogResult response = MessageBox. Show ("whether to save Fault Tree" + this. SelectedTab. Name + "to graphic file", "Please confirm", MessageBoxButtons. YesNoCancel,
MessageBoxIcon. Question, MessageBoxDefaultButton. Button2 );
If (response = System. Windows. Forms. DialogResult. Yes) // confirm to save
{
AxDrawingControl1.Document. SaveAs (GlobalVariables. ft_doc_Path + axDrawingControl1.Document. Title + ". Sealing"); // Save the current document to the folder
String datetime = DateTime. Now. ToString (); // obtain the current time
HelpTool. saveVsdDB (axDrawingControl1.Document. Title, datetime); // Save the HPA document to the database
HelpTool. setDatetimeToXml (axDrawingControl1.Document. Title, datetime); // if the information already exists, update the date in xml. If the information does not exist, insert it directly.

This. SelectedTab. Text = temp. Substring (0, temp. Length-5) + ""; // cancel the asterisk mark after saving and restore it to the style when there is no change
}
Else if (response = System. Windows. Forms. DialogResult. Cancel) // click Cancel or close
{
Return; // exit directly to cancel the program Close event.
}
}
If (this. TabCount = 1) // is the last tab. Close the parent interface, that is, the drawing interface.
{
Father. DisposeForTabControl (true );
}
Else // not the last one
{
This. TabPages. Remove (this. SelectedTab );
}
}
}
# Endregion
# Right-click region and select
Else if (e. Button = MouseButtons. Right) // Right-click to select
{
For (int I = 0; I <this. TabPages. Count; I ++)
{
TabPage tp = this. TabPages [I];
If (this. GetTabRect (I). Contains (new Point (e. X, e. Y )))
{
This. SelectedTab = tp;
Break;
}
}
}
# Endregion
# Disable the key in region
Else if (e. Button = MouseButtons. Middle) // close the mouse
{
For (int I = 0; I <this. TabPages. Count; I ++)
{
TabPage tp = this. TabPages [I];
If (this. GetTabRect (I). Contains (new Point (e. X, e. Y) // close
{
This. SelectedTab = tp;
String temp = tp. Text;
If (temp [temp. Length-5] = '*') // Save the changes
{
// Confirm whether to save the sealing file to ft_doc_Path
DialogResult response = MessageBox. Show ("whether to save Fault Tree" + this. SelectedTab. Name + "to graphic file", "Please confirm", MessageBoxButtons. YesNoCancel,
MessageBoxIcon. Question, MessageBoxDefaultButton. Button2 );
If (response = System. Windows. Forms. DialogResult. Yes) // confirm to save
{
AxDrawingControl1.Document. SaveAs (GlobalVariables. ft_doc_Path + axDrawingControl1.Document. Title + ". Sealing"); // Save the current document to the folder
String datetime = DateTime. Now. ToString (); // obtain the current time
HelpTool. saveVsdDB (axDrawingControl1.Document. Title, datetime); // Save the HPA document to the database
HelpTool. setDatetimeToXml (axDrawingControl1.Document. Title, datetime); // if the information already exists, update the date in xml. If the information does not exist, insert it directly.

This. SelectedTab. Text = temp. Substring (0, temp. Length-5) + ""; // cancel the asterisk mark after saving and restore it to the style when there is no change
}
Else if (response = System. Windows. Forms. DialogResult. Cancel) // click Cancel or close
{
Return; // exit directly to cancel the program Close event.
}
}
If (this. TabCount = 1) // is the last tab. Close the parent interface, that is, the drawing interface.
{
Father. DisposeForTabControl (true );
}
Else // not the last one
{
This. TabPages. Remove (this. SelectedTab );
}

Break;
}
}
}
# Endregion
}

How can I use it after writing it ???

Drag a TabControl on your form and open the corresponding form code file. designer. find the private void InitializeComponent () method in the cs file, and find the corresponding TabControl Definition Statement, that is, this. tabControl = .... Change this. TabControl = new MyTabControl (); if you want to pass the parameter, add the constructor with the parameter when rewriting MyTabControl (my parameters are included ).

It is worth mentioning that. designer. the private void InitializeComponent () method found in the cs file is automatically generated by the program according to your visual interface design, so every time you re-edit it in the visual design environment, this will be generated again, so you have to manually change this. tabControl = new MyTabControl ();

My program works as follows:

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.