Adding controls to ToolStrip in C#
2012-07-16 20:36
344 查看
http://someuser77.com/guides/adding-controls-to-toolstrip/
Adding controls to ToolStrip in C#
The ToolStrip control is capable of holding some common controls that comes with the .NET framework such as the ToolStripButton, the ToolStripComboBox and several others.
What about adding a non standard control such as a CheckBox or a MonthCalendar? Using the ToolStripControlHost one can do exactly this.
As it is stated in the MSDN article How to: Wrap a Windows Forms Control with ToolStripControlHost you should derive from the ToolStripControlHost class:
public classToolStripCheckBox : ToolStripControlHost
{
public ToolStripCheckBox()
: base(newCheckBox())
{
}
}
now the CheckBox can be added to the ToolStrip using:
ToolStripCheckBox toolStripCheckBox = newToolStripCheckBox();
toolStrip1.Items.Add((ToolStripItem)toolStripCheckBox);
And the CheckBox properties and events can be accessed using:
(toolStripCheckBox.Control asCheckBox).Text = "CheckBox";
(toolStripCheckBox.Control asCheckBox).CheckedChanged += (sender, e) => MessageBox.Show((sender asCheckBox).Checked.ToString());
To remove the casting from the user code and since C# does not supportmultiple inharitance it is suggested to incapsulate all the commonmethods and properties of CheckBox:
publicCheckBox CheckBoxControl
{
get { return Control asCheckBox; }
}
publicbool Checked
{
get { return CheckBoxControl.Checked; }
set { CheckBoxControl.Checked = value; }
}
so now you can write:
toolStripCheckBox.CheckBoxControl.Text = "CheckBox";
toolStripCheckBox.CheckBoxControl.CheckedChanged += (sender, e) => MessageBox.Show((sender asCheckBox).Checked.ToString());
finally provide the events you wish to access directly from the ToolStripControlHost:
public eventEventHandler CheckedChanged;
public void OnCheckedChanged(object sender, EventArgs e)
{
if (CheckedChanged != null)
{
CheckedChanged(this, e);
}
}
protectedoverridevoid OnSubscribeControlEvents(Control control)
{
base.OnSubscribeControlEvents(control);
(control asCheckBox).CheckedChanged += OnCheckedChanged;
}
protectedoverridevoid OnUnsubscribeControlEvents(Control control)
{
base.OnUnsubscribeControlEvents(control);
(control asCheckBox).CheckedChanged -= OnCheckedChanged;
}
or if you want that the CheckedChanged event will hold the CheckBox as the sender instead of the ToolStripCheckBox replace:
public void OnCheckedChanged(object sender, EventArgs e)
{
if (CheckedChanged != null)
{
CheckedChanged(this, e);
}
}
with:
public void OnCheckedChanged(object sender, EventArgs e)
{
if (CheckedChanged != null)
{
CheckedChanged(sender, e);
}
}
so now you can write:
toolStripCheckBox.CheckedChanged += (sender, e) => MessageBox.Show((sender asToolStripCheckBox).Checked.ToString());
finally if you wish to add the control to the toolstrip in the designer add the ToolStripItemDesignerAvailability Attribute to the ToolStripCheckBox class:
[ToolStripItemDesignerAvailability(ToolStripItemDesignerAvailability.ToolStrip)]
public classToolStripCheckBox : ToolStripControlHost
{
...
now you can select the CheckBox from the designer by right clicking the toolstrip and selecting 'Edit Items...'
and even view the exposed event in the control properties:
The c
Adding controls to ToolStrip in C#
The ToolStrip control is capable of holding some common controls that comes with the .NET framework such as the ToolStripButton, the ToolStripComboBox and several others.
What about adding a non standard control such as a CheckBox or a MonthCalendar? Using the ToolStripControlHost one can do exactly this.
As it is stated in the MSDN article How to: Wrap a Windows Forms Control with ToolStripControlHost you should derive from the ToolStripControlHost class:
public classToolStripCheckBox : ToolStripControlHost
{
public ToolStripCheckBox()
: base(newCheckBox())
{
}
}
now the CheckBox can be added to the ToolStrip using:
ToolStripCheckBox toolStripCheckBox = newToolStripCheckBox();
toolStrip1.Items.Add((ToolStripItem)toolStripCheckBox);
And the CheckBox properties and events can be accessed using:
(toolStripCheckBox.Control asCheckBox).Text = "CheckBox";
(toolStripCheckBox.Control asCheckBox).CheckedChanged += (sender, e) => MessageBox.Show((sender asCheckBox).Checked.ToString());
To remove the casting from the user code and since C# does not supportmultiple inharitance it is suggested to incapsulate all the commonmethods and properties of CheckBox:
publicCheckBox CheckBoxControl
{
get { return Control asCheckBox; }
}
publicbool Checked
{
get { return CheckBoxControl.Checked; }
set { CheckBoxControl.Checked = value; }
}
so now you can write:
toolStripCheckBox.CheckBoxControl.Text = "CheckBox";
toolStripCheckBox.CheckBoxControl.CheckedChanged += (sender, e) => MessageBox.Show((sender asCheckBox).Checked.ToString());
finally provide the events you wish to access directly from the ToolStripControlHost:
public eventEventHandler CheckedChanged;
public void OnCheckedChanged(object sender, EventArgs e)
{
if (CheckedChanged != null)
{
CheckedChanged(this, e);
}
}
protectedoverridevoid OnSubscribeControlEvents(Control control)
{
base.OnSubscribeControlEvents(control);
(control asCheckBox).CheckedChanged += OnCheckedChanged;
}
protectedoverridevoid OnUnsubscribeControlEvents(Control control)
{
base.OnUnsubscribeControlEvents(control);
(control asCheckBox).CheckedChanged -= OnCheckedChanged;
}
or if you want that the CheckedChanged event will hold the CheckBox as the sender instead of the ToolStripCheckBox replace:
public void OnCheckedChanged(object sender, EventArgs e)
{
if (CheckedChanged != null)
{
CheckedChanged(this, e);
}
}
with:
public void OnCheckedChanged(object sender, EventArgs e)
{
if (CheckedChanged != null)
{
CheckedChanged(sender, e);
}
}
so now you can write:
toolStripCheckBox.CheckedChanged += (sender, e) => MessageBox.Show((sender asToolStripCheckBox).Checked.ToString());
finally if you wish to add the control to the toolstrip in the designer add the ToolStripItemDesignerAvailability Attribute to the ToolStripCheckBox class:
[ToolStripItemDesignerAvailability(ToolStripItemDesignerAvailability.ToolStrip)]
public classToolStripCheckBox : ToolStripControlHost
{
...
now you can select the CheckBox from the designer by right clicking the toolstrip and selecting 'Edit Items...'
and even view the exposed event in the control properties:
The c
相关文章推荐
- Adding controls to ToolStrip in C#
- How to read and save Images in a Sql Server Database using ADO.NET and C#
- How to avoid using() mistake in C#?
- 在VMWare 中增加磁盘(Adding a new disk to a VMware Virtual Machine in Linux)
- How to attach to Browser Helper Object (BHO) with C# in two minutes
- How to pass in/out return VB Byte array from a COM Component written in C#
- A tool to generate an NT KMD framework in C- QUICKSYS
- How to find child controls that are located in the template of a parent control
- C# 用ToolStripDropDown实现任意Windows控件弹出显示
- What 's CATT(Computer Aided Test Tool)?How to deal with the CATT in the SAP System?什么是CATT(计算机附加测试工具)
- Part 45 to 47 Talking about Enums in C#
- Part 11 to 20 Basic in C# continue
- xcrun: error: unable to find utility "PackageApplication", not a developer tool or in PATH
- How to write the fast code/ high performance in C#
- Android开发中的代码关联问题:Failed to create the part'sControls(Unmatched braces in the pattern.)
- Sending data to USB printer in C#?
- c#调用c++ dll. unable to find an entry point named function in dll
- Passing Domain Credentials in C# to a WebBrowser Control in .Net WinForms Applications(winform中传递域认)
- How to Execute a Command in C#
- Beginning ASP.NET 3.5 in C# 2008 From Novice to Professional Second Edition(ASP.NET 3.5(C#2008)入门 )翻译-第十章(3)