Add a HideTabs property to turn on/off the Tabs

From: http://www.dotnetrix.co.uk/tabcontrols.html

 1using System.ComponentModel;
 2using System.Drawing;
 3using System.Windows.Forms;
 4
 5namespace Dotnetrix.Samples.CSharp
 6{
 7    [ToolboxBitmap(typeof(System.Windows.Forms.TabControl))]
 8    public class TabControl : System.Windows.Forms.TabControl
 9    {
10        private bool m_HideTabs = false;
11        
12        [DefaultValue(false)]
13        [RefreshProperties(RefreshProperties.All)]
14        public bool HideTabs
15        {
16            get{return m_HideTabs;}
17            set
18            {
19                if (m_HideTabs == value) return;
20                m_HideTabs = value;
21                if (value == truethis.Multiline = true;
22                this.UpdateStyles();
23            }

24        }

25        
26        [RefreshProperties(RefreshProperties.All)]
27        public new bool Multiline 
28        {
29            get
30            {
31                if (this.HideTabs) return true;
32                return base.Multiline;
33            }

34            set
35            {
36               if (this.HideTabs)
37                    base.Multiline = true;
38                else
39                    base.Multiline = value;
40            }

41        }

42        
43        public override System.Drawing.Rectangle DisplayRectangle
44        {
45            get
46            {
47                if (this.HideTabs)
48                    return new Rectangle(00, Width, Height);
49                else
50                {
51                    int tabStripHeight, itemHeight;
52
53                    if (this.Alignment <= TabAlignment.Bottom)
54                        itemHeight = this.ItemSize.Height;
55                    else
56                        itemHeight = this.ItemSize.Width;
57
58                    if (this.Appearance == TabAppearance.Normal)
59                        tabStripHeight = 5 + (itemHeight * this.RowCount);
60                    else
61                        tabStripHeight = (3 + itemHeight) * this.RowCount;
62
63                    switch (this.Alignment)
64                    {
65                        case TabAlignment.Bottom:
66                            return new Rectangle(44, Width - 8, Height - tabStripHeight - 4);
67                        case TabAlignment.Left:
68                            return new Rectangle(tabStripHeight, 4, Width - tabStripHeight - 4, Height - 8);
69                        case TabAlignment.Right:
70                            return new Rectangle(44, Width - tabStripHeight - 4, Height - 8);
71                        default:
72                            return new Rectangle(4, tabStripHeight, Width - 8, Height - tabStripHeight - 4);
73                    }

74                }

75            }

76        }

77    }

78}

 

还有另外的方法对一个标准的TabControl控件实现隐藏TabPages的Tabs。只要将TabControl控件的四个属性作如下设置就可以了:

TabControl.Appearance = TabAppearance.FlatButtons
TabControl.Multiline = False
TabControl.SizeMode = TabSizeMode.Fixed
TabControl.ItemSize = New Size(0, 1)

不过,这时有一种情况可能是你不想看到的。那就在运行时刻,用户可以通过Ctrl+Tab和Ctrl+Shift+Tab来切换TabPage,即使Tabs没有显示也是如此的。这种情况我们可以通过下面的代码来避免。

 1protected override bool ProcessCmdKey(ref System.Windows.Forms.Message msg, System.Windows.Forms.Keys keyData)
 2        {
 3            if ((keyData == (Keys.Control | Keys.Tab)) || (keyData == (Keys.Control | Keys.Shift | Keys.Tab)))
 4            {
 5                //Indicate that we've handled this keypress
 6                return true;
 7            }

 8            
 9            return base.ProcessCmdKey(ref msg, keyData);
10        }

 补充:ProcessCmdKey的另外一种写法。

1protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
2{
3    if (ActiveControl is TabControl)
4    {
5        if (System.Convert.ToBoolean(keyData & Keys.Tab | Keys.Control))
6            return true;
7    }

8    return base.ProcessCmdKey (ref msg, keyData);
9}

posted on 2006-09-18 23:36  Voidclass  阅读(1403)  评论(7编辑  收藏  举报

导航