?? mytabcontrol.cs
字號:
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
namespace AdvControls
{
[
ParseChildren(true, "TabPages"),
ToolboxData("<{0}:MyTabControl runat=\"server\" Width=\"125px\" Height=\"50px\"></{0}:MyTabControl>"),
PersistChildren(false),
Designer(typeof(MyTabControlDesigner))
]
public class MyTabControl : CompositeControl
{
#region private fields
private MyTabPageCollection _tabPages;
private int _currentDesignTab;
private int _selectedTab;
#endregion private fields
#region public properties
[
PersistenceMode(PersistenceMode.InnerProperty),
DesignerSerializationVisibility(DesignerSerializationVisibility.Visible),
MergableProperty(false)
]
public MyTabPageCollection TabPages
{
get
{
if (_tabPages == null)
{
_tabPages = new MyTabPageCollection();
}
return _tabPages;
}
}
/// <summary>
/// Get or set the deesign time active tab.
/// </summary>
[Browsable(false),
PersistenceMode(PersistenceMode.InnerProperty),
DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)
]
public int CurrentDesignTab
{
get { return _currentDesignTab; }
set { _currentDesignTab = value; }
}
/// <summary>
/// Get or set the runtime active tab.
/// </summary>
public int SelectedTab
{
get { return _selectedTab; }
set { _selectedTab = value; }
}
#endregion public properties
#region private methods
private void BuildTitles(Table tabControlTable)
{
// Create the titles row
TableRow titlesRow = new TableRow();
titlesRow.HorizontalAlign = HorizontalAlign.Center;
int i = 0;
foreach (MyTabPage tabPage in _tabPages)
{
// Create titles cells
TableCell tabTitleCell = new TableCell();
tabTitleCell.Text = tabPage.Title;
tabTitleCell.Width = new Unit("");
tabTitleCell.BorderStyle = BorderStyle.Outset;
tabTitleCell.BorderWidth = new Unit("2");
tabTitleCell.Style["padding"] = "0px 4px 0px 4px";
tabTitleCell.Style["cursor"] = "hand";
tabTitleCell.Wrap = false;
tabTitleCell.Height = new Unit("20");
if (!DesignMode)
{
//Highlight the selected tab title
if (_selectedTab == i)
{
tabTitleCell.Style["background-color"] = ColorTranslator.ToHtml(Color.DarkGray);
}
}
//Add on-click event on the title cell to switch between tabs
tabTitleCell.Attributes.Add("onclick", "ShowTab(this, " + i.ToString() + ")");
titlesRow.Cells.Add(tabTitleCell);
i++;
}
//Add additional empty cell
TableCell tc1 = new TableCell();
tc1.Width = new Unit("100%");
tc1.Height = new Unit("20");
titlesRow.Cells.Add(tc1);
titlesRow.Height = new Unit("20");
tabControlTable.Rows.Add(titlesRow);
}
private void BuildContentRows(Table tabControlTable)
{
// Create content row(s)
if (DesignMode)
{
TableRow contentRow = new TableRow();
TableCell contentCell = BuildContentCell(contentRow);
_tabPages[_currentDesignTab].TabBody.InstantiateIn(contentCell);
tabControlTable.Rows.Add(contentRow);
}
else
{
int counter = 0;
foreach (MyTabPage tabPage in _tabPages)
{
TableRow contentRow = new TableRow();
TableCell contentCell = BuildContentCell(contentRow);
if (tabPage.TabBody != null)
{
tabPage.TabBody.InstantiateIn(contentCell);
}
//only the selected tab body should be visible
if (_selectedTab == counter)
{
contentRow.Style["display"] = "block";
}
else
{
contentRow.Style["display"] = "none";
}
contentRow.Cells.Add(contentCell);
tabControlTable.Rows.Add(contentRow);
counter++;
}
}
}
private TableCell BuildContentCell(TableRow tableRow)
{
TableCell tc = new TableCell();
tc.ColumnSpan = _tabPages.Count + 1;
tc.BackColor = Color.White;
tc.BorderWidth = new Unit("1");
tc.BorderStyle = BorderStyle.Ridge;
tc.BorderColor = Color.Silver;
tc.Style["padding"] = "5px 5px 5px 5px";
tc.Height = new Unit("100%");
tableRow.Cells.Add(tc);
return tc;
}
#endregion private methods
#region implementations
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
if (DesignMode)
{
_tabPages[_currentDesignTab].TabBody.InstantiateIn(this);
}
}
protected override void CreateChildControls()
{
// Always start with a clean form
Controls.Clear();
// Create a table using the control's declarative properties
Table tabControlTable = new Table();
tabControlTable.CellSpacing = 1;
tabControlTable.CellPadding = 0;
tabControlTable.BorderStyle = BorderStyle;
tabControlTable.Width = this.Width;
tabControlTable.Height = this.Height;
tabControlTable.BackColor = ColorTranslator.FromHtml("inactiveborder");
//keep a the selected tab index in a an attribute
tabControlTable.Attributes.Add("ActiveTabIdx", _selectedTab.ToString());
BuildTitles(tabControlTable);
BuildContentRows(tabControlTable);
// Add the finished tabControlTable to the Controls collection
Controls.Add(tabControlTable);
}
#endregion implementations
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -