Hide "C #" for the TabControl control's label

Source: Internet
Author: User

"Method One"

As a result of recent project needs, a database Distribution Synchronization Wizard was developed using the TabControl control, in which the final encapsulation found that the TabControl control did not have the methods and properties of the existing hidden tags; it's a real emergency! Find a way to solve it. The function of the Internet is strong, although there is no ready-made solution, but I also understand the almost. An attempt can be made to implement TabControl tag hiding in the following ways:

In the form Load event, add:

[C-sharp]View Plaincopy
    1. This.tabControl1.Region = New Region (new RectangleF (this.tabPage1.Left, This.tabPage1.Top,   This.tabPage1.Width, this.tabPage1.Height));

The above code completes the hiding of the tag, but there is also a problem, that is, Ctrl +tab can toggle the page in TabControl, can be masked by capturing key messages:

[C-sharp]View Plaincopy
  1. Protected override bool processCmdKey (ref Message msg, Keys keyData)
  2. {
  3. switch (keyData)
  4. {
  5. Case (Keys.tab | Keys.control):
  6. return true;
  7. Default:
  8. Break ;
  9. }
  10. return base.  processCmdKey (ref msg, keyData);
  11. }

The above two steps can realize the hidden TabControl label, if you want to effect ideal, you can adjust the TabControl Appearance property settings TabControl the way to draw.

"Method Two"

Tags that hide TabControl must be implemented by inheriting TabControl and redrawing themselves. Here is a complete example of redrawing TabControl:

[C-sharp]View Plaincopy
  1. Public class ftabcontrol:system.windows.forms.tabcontrol{
  2. Private System.ComponentModel.Container components = null;
  3. Private static Color Backcolor=color.fromargb (227,237,251);
  4. Private static Color Buttoncolor;
  5. Private static Color Buttonhighlightcolor;
  6. Private static Color Bordercolor=color.black;
  7. Public bool userchangetab=true;
  8. Public Ftabcontrol () {
  9. InitializeComponent ();
  10. Setdafaultstyle ();
  11. SetStyle (Controlstyles.userpaint,true);
  12. }
  13. #region Component Designer generated code
  14. Private void InitializeComponent () {
  15. components = new System.ComponentModel.Container ();
  16. }
  17. #endregion
  18. Public void Setdafaultstyle () {
  19. appearance = System.Windows.Forms.TabAppearance.FlatButtons;
  20. Buttoncolor = Color.FromArgb (184,210,250);
  21. Buttonhighlightcolor = Color.FromArgb (144,187,252);
  22. }
  23. Protected override void OnPaint (PaintEventArgs e) {
  24. E.graphics.fillrectangle (new SolidBrush (BackColor), e.cliprectangle);
  25. for (int i=0;i< this. tabcount;i++) {
  26. DrawItem (E.graphics,i);
  27. }
  28. }
  29. protected void DrawItem (Graphics g,int index) {
  30. Rectangle r = gettabrect (index);
  31. R.inflate ( -2,-2);
  32. if (Selectedindex==index)
  33. G.fillrectangle (new SolidBrush (Buttonhighlightcolor), R);
  34. Else
  35. G.fillrectangle (new SolidBrush (Buttoncolor), R);
  36. G.drawrectangle (NewPen (new SolidBrush (bordercolor)), R);
  37. R.inflate ( -3,-3);
  38. g.DrawString (Tabpages[index].   Text,font,new SolidBrush (bordercolor), R);
  39. }
  40. Protected override void WndProc (ref System.Windows.Forms.Message m) {
  41. if (m.msg = = 513 &&! This. Userchangetab) {
  42. Return //trap Wm_lbuttondown
  43. }
  44. else{
  45. Base.   WndProc (ref m);
  46. }
  47. }
  48. Protected override void OnKeyDown (KeyEventArgs e) {
  49. if (e.control==true && e.keycode==system.windows.forms.keys.tab &&! ) This. Userchangetab) {
  50. Return //trap Ctrl+tab and Ctrl+shift+tab
  51. }
  52. else{
  53. Base. OnKeyDown (e);
  54. }
  55. }
  56. }

Here is a little explanation of the above example code:1) The role of the public bool Userchangetab member is to specify whether the user is allowed to change the current tab by clicking on the Mouse tab on the interface and pressing "Ctrl+tab".2) The purpose of calling SetStyle () in the constructor is to tell the system that the control will repaint itself instead of using the system default display. 3) OnPaint () completed the work of self-redrawing, which called the DrawItem function to redraw all the labels. 4) The function of Setdafaultstyle () is to set some default color and interface style. 5) The most important is the overloaded WndProc () and onkeydown () functions. Mouse click events and keyboard input events are captured in these two overloaded functions. Once this. The value of Userchangetab is false (that is, the user is not allowed to change the tab), the captured event is destroyed and is no longer passed to the event handler of the base class.
When using this Ftabcontrol, 1) If you need to prevent the user from changing the tab by mouse or ctrl+tab, you need to set the Userchangetab to False. 2) If you need to hide the label, you need to set the ItemSize property of the Ftabcontrol to () and adjust the color settings appropriately to achieve the visually invisible effect.
It should be explained that, through the above example, not only can hide tab tags, prohibit users to change the label and other functions, through the expansion of the DrawItem function can also achieve a variety of tab label complex visual effects.

"Method Three"

about how to hide the tab header (page tab) of TabControl

Many netizens ask this question, actually can't do it. I found an alternative approach that basically satisfies the effect.

That is: Set TabControl itemsize (the first), we have a hobby, you can try.

< user reply > that's what TabControl do?

You can use a panel to load it up and set the location's Y value to negative.

< user reply > When you want to hide

[C-sharp]View Plaincopy
    1. if (this.tabmain.tabpages[ "Tabpagethepage"]! = null)
    2. {
    3. This.tabMain.TabPages.Remove (Tabpagethepage);
    4. }
    5. When you want to show it,
    6. if (this.tabmain.tabpages[ "tabpagethepage"] = = null)
    7. {
    8. THIS.TABMAIN.TABPAGES.ADD (Tabpagethepage);
    9. }

Hide "C #" for the TabControl control's label

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.