Code:
/ Dotnetfx_Vista_SP2 / Dotnetfx_Vista_SP2 / 8.0.50727.4016 / DEVDIV / depot / DevDiv / releases / whidbey / NetFxQFE / ndp / fx / src / WinForms / Managed / System / WinForms / ToolStripButton.cs / 1 / ToolStripButton.cs
//------------------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//-----------------------------------------------------------------------------
namespace System.Windows.Forms {
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Imaging;
using System.ComponentModel;
using System.Windows.Forms.Design;
///
///
[ToolStripItemDesignerAvailability(ToolStripItemDesignerAvailability.ToolStrip)]
public class ToolStripButton : ToolStripItem {
private CheckState checkState = CheckState.Unchecked;
private bool checkOnClick = false;
private const int StandardButtonWidth = 23;
private static readonly object EventCheckedChanged = new object();
private static readonly object EventCheckStateChanged = new object();
///
///
/// Summary of ToolStripButton.
///
public ToolStripButton() {
Initialize();
}
public ToolStripButton(string text):base(text,null,null) {
Initialize();
}
public ToolStripButton(Image image):base(null,image,null) {
Initialize();
}
public ToolStripButton(string text, Image image):base(text,image,null) {
Initialize();
}
public ToolStripButton(string text, Image image, EventHandler onClick):base(text,image,onClick) {
Initialize();
}
public ToolStripButton(string text, Image image, EventHandler onClick, string name):base(text,image,onClick,name) {
Initialize();
}
[DefaultValue(true)]
public new bool AutoToolTip {
get {
return base.AutoToolTip;
}
set {
base.AutoToolTip = value;
}
}
///
///
/// Summary of CanSelect.
///
public override bool CanSelect {
get {
return true;
}
}
///
[
DefaultValue(false),
SRCategory(SR.CatBehavior),
SRDescription(SR.ToolStripButtonCheckOnClickDescr)
]
public bool CheckOnClick {
get {
return checkOnClick;
}
set {
checkOnClick = value;
}
}
///
///
///
/// Gets or sets a value indicating whether the item is checked.
///
///
[
DefaultValue(false),
SRCategory(SR.CatAppearance),
SRDescription(SR.ToolStripButtonCheckedDescr)
]
public bool Checked {
get {
return checkState != CheckState.Unchecked;
}
set {
if (value != Checked) {
CheckState = value ? CheckState.Checked : CheckState.Unchecked;
InvokePaint();
}
}
}
///
///
/// Gets
/// or sets a value indicating whether the check box is checked.
///
[
SRCategory(SR.CatAppearance),
DefaultValue(CheckState.Unchecked),
SRDescription(SR.CheckBoxCheckStateDescr)
]
public CheckState CheckState {
get {
return checkState;
}
set {
//valid values are 0x0 to 0x2
if (!ClientUtils.IsEnumValid(value, (int)value, (int)CheckState.Unchecked, (int)CheckState.Indeterminate))
{
throw new InvalidEnumArgumentException("value", (int)value, typeof(CheckState));
}
if (value != checkState) {
checkState = value;
Invalidate();
OnCheckedChanged(EventArgs.Empty);
OnCheckStateChanged(EventArgs.Empty);
}
}
}
///
///
/// Occurs when the
/// value of the
/// property changes.
///
[SRDescription(SR.CheckBoxOnCheckedChangedDescr)]
public event EventHandler CheckedChanged {
add {
Events.AddHandler(EventCheckedChanged, value);
}
remove {
Events.RemoveHandler(EventCheckedChanged, value);
}
}
///
///
/// Occurs when the
/// value of the
/// property changes.
///
[SRDescription(SR.CheckBoxOnCheckStateChangedDescr)]
public event EventHandler CheckStateChanged {
add {
Events.AddHandler(EventCheckStateChanged, value);
}
remove {
Events.RemoveHandler(EventCheckStateChanged, value);
}
}
protected override bool DefaultAutoToolTip {
get {
return true;
}
}
///
///
/// constructs the new instance of the accessibility object for this ToolStripItem. Subclasses
/// should not call base.CreateAccessibilityObject.
///
[EditorBrowsable(EditorBrowsableState.Advanced)]
protected override AccessibleObject CreateAccessibilityInstance() {
return new ToolStripButtonAccessibleObject(this);
}
public override Size GetPreferredSize(Size constrainingSize) {
Size prefSize = base.GetPreferredSize(constrainingSize);
prefSize.Width = Math.Max(prefSize.Width, StandardButtonWidth);
return prefSize;
}
///
/// Called by all constructors of ToolStripButton.
///
private void Initialize() {
SupportsSpaceKey = true;
}
///
///
/// Raises the
/// event.
///
protected virtual void OnCheckedChanged(EventArgs e) {
EventHandler handler = (EventHandler)Events[EventCheckedChanged];
if (handler != null) handler(this,e);
}
///
///
/// Raises the event.
///
protected virtual void OnCheckStateChanged(EventArgs e) {
AccessibilityNotifyClients(AccessibleEvents.StateChange);
EventHandler handler = (EventHandler)Events[EventCheckStateChanged];
if (handler != null) handler(this,e);
}
///
///
/// Inheriting classes should override this method to handle this event.
///
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e) {
if (this.Owner != null) {
ToolStripRenderer renderer = this.Renderer;
renderer.DrawButtonBackground(new ToolStripItemRenderEventArgs(e.Graphics, this));
if ((DisplayStyle & ToolStripItemDisplayStyle.Image) == ToolStripItemDisplayStyle.Image) {
ToolStripItemImageRenderEventArgs rea = new ToolStripItemImageRenderEventArgs(e.Graphics, this, InternalLayout.ImageRectangle);
rea.ShiftOnPress = true;
renderer.DrawItemImage(rea);
}
if ((DisplayStyle & ToolStripItemDisplayStyle.Text) == ToolStripItemDisplayStyle.Text) {
renderer.DrawItemText(new ToolStripItemTextRenderEventArgs(e.Graphics, this, this.Text, InternalLayout.TextRectangle, this.ForeColor, this.Font, InternalLayout.TextFormat));
}
}
}
///
protected override void OnClick(EventArgs e) {
if (checkOnClick) {
this.Checked = !this.Checked;
}
base.OnClick(e);
}
///
/// An implementation of AccessibleChild for use with ToolStripItems
///
[System.Runtime.InteropServices.ComVisible(true)]
internal class ToolStripButtonAccessibleObject : ToolStripItemAccessibleObject {
private ToolStripButton ownerItem = null;
public ToolStripButtonAccessibleObject(ToolStripButton ownerItem): base(ownerItem) {
this.ownerItem = ownerItem;
}
public override AccessibleStates State {
get {
if (ownerItem.Enabled && ownerItem.Checked) {
return base.State | AccessibleStates.Checked;
}
return base.State;
}
}
}
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
//------------------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//-----------------------------------------------------------------------------
namespace System.Windows.Forms {
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Imaging;
using System.ComponentModel;
using System.Windows.Forms.Design;
///
///
[ToolStripItemDesignerAvailability(ToolStripItemDesignerAvailability.ToolStrip)]
public class ToolStripButton : ToolStripItem {
private CheckState checkState = CheckState.Unchecked;
private bool checkOnClick = false;
private const int StandardButtonWidth = 23;
private static readonly object EventCheckedChanged = new object();
private static readonly object EventCheckStateChanged = new object();
///
///
/// Summary of ToolStripButton.
///
public ToolStripButton() {
Initialize();
}
public ToolStripButton(string text):base(text,null,null) {
Initialize();
}
public ToolStripButton(Image image):base(null,image,null) {
Initialize();
}
public ToolStripButton(string text, Image image):base(text,image,null) {
Initialize();
}
public ToolStripButton(string text, Image image, EventHandler onClick):base(text,image,onClick) {
Initialize();
}
public ToolStripButton(string text, Image image, EventHandler onClick, string name):base(text,image,onClick,name) {
Initialize();
}
[DefaultValue(true)]
public new bool AutoToolTip {
get {
return base.AutoToolTip;
}
set {
base.AutoToolTip = value;
}
}
///
///
/// Summary of CanSelect.
///
public override bool CanSelect {
get {
return true;
}
}
///
[
DefaultValue(false),
SRCategory(SR.CatBehavior),
SRDescription(SR.ToolStripButtonCheckOnClickDescr)
]
public bool CheckOnClick {
get {
return checkOnClick;
}
set {
checkOnClick = value;
}
}
///
///
///
/// Gets or sets a value indicating whether the item is checked.
///
///
[
DefaultValue(false),
SRCategory(SR.CatAppearance),
SRDescription(SR.ToolStripButtonCheckedDescr)
]
public bool Checked {
get {
return checkState != CheckState.Unchecked;
}
set {
if (value != Checked) {
CheckState = value ? CheckState.Checked : CheckState.Unchecked;
InvokePaint();
}
}
}
///
///
/// Gets
/// or sets a value indicating whether the check box is checked.
///
[
SRCategory(SR.CatAppearance),
DefaultValue(CheckState.Unchecked),
SRDescription(SR.CheckBoxCheckStateDescr)
]
public CheckState CheckState {
get {
return checkState;
}
set {
//valid values are 0x0 to 0x2
if (!ClientUtils.IsEnumValid(value, (int)value, (int)CheckState.Unchecked, (int)CheckState.Indeterminate))
{
throw new InvalidEnumArgumentException("value", (int)value, typeof(CheckState));
}
if (value != checkState) {
checkState = value;
Invalidate();
OnCheckedChanged(EventArgs.Empty);
OnCheckStateChanged(EventArgs.Empty);
}
}
}
///
///
/// Occurs when the
/// value of the
/// property changes.
///
[SRDescription(SR.CheckBoxOnCheckedChangedDescr)]
public event EventHandler CheckedChanged {
add {
Events.AddHandler(EventCheckedChanged, value);
}
remove {
Events.RemoveHandler(EventCheckedChanged, value);
}
}
///
///
/// Occurs when the
/// value of the
/// property changes.
///
[SRDescription(SR.CheckBoxOnCheckStateChangedDescr)]
public event EventHandler CheckStateChanged {
add {
Events.AddHandler(EventCheckStateChanged, value);
}
remove {
Events.RemoveHandler(EventCheckStateChanged, value);
}
}
protected override bool DefaultAutoToolTip {
get {
return true;
}
}
///
///
/// constructs the new instance of the accessibility object for this ToolStripItem. Subclasses
/// should not call base.CreateAccessibilityObject.
///
[EditorBrowsable(EditorBrowsableState.Advanced)]
protected override AccessibleObject CreateAccessibilityInstance() {
return new ToolStripButtonAccessibleObject(this);
}
public override Size GetPreferredSize(Size constrainingSize) {
Size prefSize = base.GetPreferredSize(constrainingSize);
prefSize.Width = Math.Max(prefSize.Width, StandardButtonWidth);
return prefSize;
}
///
/// Called by all constructors of ToolStripButton.
///
private void Initialize() {
SupportsSpaceKey = true;
}
///
///
/// Raises the
/// event.
///
protected virtual void OnCheckedChanged(EventArgs e) {
EventHandler handler = (EventHandler)Events[EventCheckedChanged];
if (handler != null) handler(this,e);
}
///
///
/// Raises the event.
///
protected virtual void OnCheckStateChanged(EventArgs e) {
AccessibilityNotifyClients(AccessibleEvents.StateChange);
EventHandler handler = (EventHandler)Events[EventCheckStateChanged];
if (handler != null) handler(this,e);
}
///
///
/// Inheriting classes should override this method to handle this event.
///
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e) {
if (this.Owner != null) {
ToolStripRenderer renderer = this.Renderer;
renderer.DrawButtonBackground(new ToolStripItemRenderEventArgs(e.Graphics, this));
if ((DisplayStyle & ToolStripItemDisplayStyle.Image) == ToolStripItemDisplayStyle.Image) {
ToolStripItemImageRenderEventArgs rea = new ToolStripItemImageRenderEventArgs(e.Graphics, this, InternalLayout.ImageRectangle);
rea.ShiftOnPress = true;
renderer.DrawItemImage(rea);
}
if ((DisplayStyle & ToolStripItemDisplayStyle.Text) == ToolStripItemDisplayStyle.Text) {
renderer.DrawItemText(new ToolStripItemTextRenderEventArgs(e.Graphics, this, this.Text, InternalLayout.TextRectangle, this.ForeColor, this.Font, InternalLayout.TextFormat));
}
}
}
///
protected override void OnClick(EventArgs e) {
if (checkOnClick) {
this.Checked = !this.Checked;
}
base.OnClick(e);
}
///
/// An implementation of AccessibleChild for use with ToolStripItems
///
[System.Runtime.InteropServices.ComVisible(true)]
internal class ToolStripButtonAccessibleObject : ToolStripItemAccessibleObject {
private ToolStripButton ownerItem = null;
public ToolStripButtonAccessibleObject(ToolStripButton ownerItem): base(ownerItem) {
this.ownerItem = ownerItem;
}
public override AccessibleStates State {
get {
if (ownerItem.Enabled && ownerItem.Checked) {
return base.State | AccessibleStates.Checked;
}
return base.State;
}
}
}
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- SymmetricKey.cs
- CheckBoxStandardAdapter.cs
- EndpointPerformanceCounters.cs
- MtomMessageEncodingBindingElement.cs
- BindingList.cs
- LineSegment.cs
- Rectangle.cs
- WsrmMessageInfo.cs
- WorkflowApplication.cs
- ToolStripDesignerUtils.cs
- WebCategoryAttribute.cs
- DatatypeImplementation.cs
- Pkcs9Attribute.cs
- StateMachineDesignerPaint.cs
- SafeNativeMethodsCLR.cs
- CharacterBuffer.cs
- AppearanceEditorPart.cs
- DataServiceHost.cs
- XmlBinaryReader.cs
- ClientTargetCollection.cs
- DetailsView.cs
- SymbolMethod.cs
- SettingsProviderCollection.cs
- SafeNativeMethods.cs
- InputScope.cs
- SoapObjectReader.cs
- TransferRequestHandler.cs
- ImageCodecInfo.cs
- ASCIIEncoding.cs
- SchemaObjectWriter.cs
- UnsafeNativeMethodsPenimc.cs
- SessionPageStateSection.cs
- PatternMatcher.cs
- ArrayList.cs
- ContextMenu.cs
- WeakEventManager.cs
- WebEventCodes.cs
- MethodToken.cs
- PointConverter.cs
- ObjectDataProvider.cs
- VectorConverter.cs
- FormsAuthenticationCredentials.cs
- XPathDocumentIterator.cs
- SecurityPolicySection.cs
- TemplateControl.cs
- LocationSectionRecord.cs
- SiteMapNodeItem.cs
- Point4DConverter.cs
- DynamicDataResources.Designer.cs
- LinearGradientBrush.cs
- MessageQueuePermission.cs
- XmlNamespaceManager.cs
- RtfNavigator.cs
- GridItemCollection.cs
- DateTimeFormatInfo.cs
- PreviewPrintController.cs
- WorkflowClientDeliverMessageWrapper.cs
- RichTextBoxConstants.cs
- Typeface.cs
- RegionInfo.cs
- IgnoreSection.cs
- AtomPub10CategoriesDocumentFormatter.cs
- HttpPostedFile.cs
- CollectionsUtil.cs
- SimpleBitVector32.cs
- SmtpFailedRecipientException.cs
- SecurityCookieModeValidator.cs
- SeekStoryboard.cs
- DataGridTable.cs
- InlineCollection.cs
- EventEntry.cs
- StringBuilder.cs
- NavigationPropertyEmitter.cs
- SystemTcpConnection.cs
- TextLineResult.cs
- RijndaelManaged.cs
- Style.cs
- NativeMethods.cs
- UnmanagedMarshal.cs
- CqlWriter.cs
- CheckBox.cs
- SmtpSpecifiedPickupDirectoryElement.cs
- PrintingPermission.cs
- Proxy.cs
- SqlVersion.cs
- RemoteWebConfigurationHost.cs
- ResXResourceReader.cs
- PropertyMap.cs
- FormViewUpdatedEventArgs.cs
- DecoderExceptionFallback.cs
- DynamicControl.cs
- Trigger.cs
- ToolStripLocationCancelEventArgs.cs
- PeerCollaborationPermission.cs
- TextHidden.cs
- WeakReadOnlyCollection.cs
- TranslateTransform3D.cs
- XmlTextReaderImpl.cs
- CapabilitiesAssignment.cs
- AuthorizationRuleCollection.cs