TabPageSwitcher.cs
4.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
namespace RibbonStyle {
/// <summary>
/// The TabPageSwitcher works on Z-Order principals - it Dock.Fills all
/// the contents and uses BringToFront to quickly bring a page to the front
/// </summary>
[Designer(typeof(TabPageSwitcherDesigner))] // specify a custom designer
[System.ComponentModel.DesignerCategory("code")] // when this file is opened, open the editor instead of the designer
[Docking(DockingBehavior.AutoDock)] // when this control is dropped onto the form, try to Dock.Fill
[ToolboxItem(false)] // dont show this control in the toolbox, it will be added by the TabStripToolBoxItem
public class TabPageSwitcher : ContainerControl {
private TabStripPage selectedTabStripPage;
private TabStrip tabStrip;
public TabPageSwitcher() {
ResetBackColor();
}
/// <summary>
/// specify the default size for the control
/// </summary>
protected override Size DefaultSize {
get {
return new Size(60,60);
}
}
/// <summary>
/// specify a default padding to give a border around the control
/// </summary>
protected override Padding DefaultPadding {
get {
return new Padding(4, 0, 4, 2);
}
}
/// <summary>
/// Expose a Load event
/// </summary>
public event EventHandler Load;
/// <summary>
/// The associated TabStrip
/// </summary>
public TabStrip TabStrip {
get {
return tabStrip;
}
set {
tabStrip = value;
}
}
/// <summary>
/// Specify the selected TabStripPage.
/// </summary>
public TabStripPage SelectedTabStripPage {
get { return selectedTabStripPage; }
set {
if (selectedTabStripPage != value) {
selectedTabStripPage = value;
if (selectedTabStripPage != null) {
if (!Controls.Contains(value)) {
Controls.Add(selectedTabStripPage);
}
else {
selectedTabStripPage.BringToFront();
}
}
}
}
}
/// <summary>
/// Occurs when the selected tab has changed
/// </summary>
public event EventHandler SelectedTabStripPageChanged;
/// <summary>
/// Handle the OnHandleCreated event to fire the Load event.
/// </summary>
/// <param name="e"></param>
protected override void OnHandleCreated(EventArgs e) {
base.OnHandleCreated(e);
if (!RecreatingHandle) {
OnLoad(EventArgs.Empty);
}
}
/// <summary>
/// When the control is loaded, if we dont have a SelectedTabStripPage, look for one to activate.
/// </summary>
/// <param name="e"></param>
protected virtual void OnLoad(EventArgs e) {
if (this.Load != null) {
Load(this, e);
}
if (!DesignMode) {
this.TabStrip.SelectedTab = (Tab) this.TabStrip.Items[0];
((Tab)this.TabStrip.Items[0]).b_active = true;
}
}
/// <summary>
/// Override OnControlAdded, all controls added to this control should become Dock.Fill.
/// </summary>
/// <param name="e"></param>
protected override void OnControlAdded(ControlEventArgs e) {
e.Control.Dock = DockStyle.Fill;
base.OnControlAdded(e);
}
/// <summary>
/// occurs when the selected tab has changed
/// </summary>
/// <param name="e"></param>
protected virtual void OnSelectedTabStripPageChanged(EventArgs e) {
if (SelectedTabStripPageChanged != null) {
SelectedTabStripPageChanged(this, EventArgs.Empty);
}
}
#region DesignerSerialization Friendliness
private bool ShouldSerializeBackColor() {
return this.BackColor != Color.FromArgb(191, 219, 255);
}
public override void ResetBackColor() {
this.BackColor = Color.FromArgb(191, 219, 255);
}
#endregion
}
}