Code:
/ FX-1434 / FX-1434 / 1.0 / untmp / whidbey / REDBITS / ndp / fx / src / Designer / WinForms / System / WinForms / Design / EventHandlerService.cs / 1 / EventHandlerService.cs
//------------------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//-----------------------------------------------------------------------------
/*
*/
namespace System.Windows.Forms.Design {
using System.Diagnostics;
using System;
using System.Windows.Forms;
///
///
///
///
public sealed class EventHandlerService : IEventHandlerService {
// We cache the last requested handler for speed.
//
private object lastHandler;
private Type lastHandlerType;
// The handler stack
//
private HandlerEntry handlerHead;
// Our change event
//
private EventHandler changedEvent;
private readonly Control focusWnd;
///
///
/// [To be supplied.]
///
public EventHandlerService(Control focusWnd) {
this.focusWnd = focusWnd;
}
///
///
/// [To be supplied.]
///
public event EventHandler EventHandlerChanged {
add {
changedEvent += value;
}
remove {
changedEvent -= value;
}
}
///
///
/// [To be supplied.]
///
public Control FocusWindow {
get {
return focusWnd;
}
}
///
///
///
/// Gets the currently active event handler of the specified type.
///
public object GetHandler(Type handlerType) {
if (handlerType == lastHandlerType) {
return lastHandler;
}
for (HandlerEntry entry = handlerHead; entry != null; entry = entry.next) {
if (entry.handler != null && handlerType.IsInstanceOfType(entry.handler)) {
lastHandlerType = handlerType;
lastHandler = entry.handler;
return entry.handler;
}
}
return null;
}
///
///
/// Fires an OnEventHandlerChanged event.
///
private void OnEventHandlerChanged(EventArgs e) {
if (changedEvent != null) {
((EventHandler)changedEvent)(this, e);
}
}
///
///
///
/// Pops
/// the given handler off of the stack.
///
public void PopHandler(object handler) {
for (HandlerEntry entry = handlerHead; entry != null; entry = entry.next) {
if (entry.handler == handler) {
handlerHead = entry.next;
lastHandler = null;
lastHandlerType = null;
OnEventHandlerChanged(EventArgs.Empty);
return;
}
}
Debug.Assert(handler == null || handlerHead == null, "Failed to locate handler to remove from list.");
}
///
///
/// Pushes a new event handler on the stack.
///
public void PushHandler(object handler) {
handlerHead = new HandlerEntry(handler, handlerHead);
// Update the handlerType if the Handler pushed is the same type as the last one ....
// This is true when SplitContainer is on the form and Edit Properties pushed another handler.
lastHandlerType = handler.GetType();
lastHandler = handlerHead.handler;
OnEventHandlerChanged(EventArgs.Empty);
}
///
///
/// Contains a single node of our handler stack. We typically
/// have very few handlers, and the handlers are long-living, so
/// I just implemented this as a linked list.
///
private sealed class HandlerEntry {
public object handler;
public HandlerEntry next;
///
///
/// Creates a new handler entry objet.
///
public HandlerEntry(object handler, HandlerEntry next) {
this.handler = handler;
this.next = next;
}
}
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- FormViewUpdatedEventArgs.cs
- PropertyReferenceExtension.cs
- QualifiedCellIdBoolean.cs
- HwndAppCommandInputProvider.cs
- EntityDataSourceState.cs
- DataViewSetting.cs
- XslNumber.cs
- OleDbEnumerator.cs
- InvariantComparer.cs
- PerspectiveCamera.cs
- StyleBamlTreeBuilder.cs
- InvalidProgramException.cs
- DispatcherHookEventArgs.cs
- ResourceSetExpression.cs
- SemanticKeyElement.cs
- ProcessProtocolHandler.cs
- EventSourceCreationData.cs
- GZipStream.cs
- UniqueIdentifierService.cs
- Stroke.cs
- DataGridViewRowConverter.cs
- BitmapEffectrendercontext.cs
- RelationshipFixer.cs
- SettingsPropertyCollection.cs
- ByteKeyFrameCollection.cs
- UntypedNullExpression.cs
- CatalogPartChrome.cs
- TreeNode.cs
- HttpListenerTimeoutManager.cs
- TypeConvertions.cs
- DataMember.cs
- MembershipValidatePasswordEventArgs.cs
- PerformanceCounterCategory.cs
- XmlAttributeProperties.cs
- URLIdentityPermission.cs
- PropertyTabChangedEvent.cs
- HttpValueCollection.cs
- ColorAnimationUsingKeyFrames.cs
- HtmlElementErrorEventArgs.cs
- EventlogProvider.cs
- PageClientProxyGenerator.cs
- Attributes.cs
- WebPartsPersonalizationAuthorization.cs
- SizeF.cs
- Environment.cs
- SqlDataSourceSelectingEventArgs.cs
- ContentPathSegment.cs
- Vector3dCollection.cs
- XmlWrappingReader.cs
- TimeSpanSecondsOrInfiniteConverter.cs
- ImplicitInputBrush.cs
- XmlDigitalSignatureProcessor.cs
- MouseOverProperty.cs
- FunctionCommandText.cs
- HttpCacheVary.cs
- TextSelectionProcessor.cs
- TreeSet.cs
- ECDiffieHellmanPublicKey.cs
- ResourceLoader.cs
- MsmqInputMessagePool.cs
- SystemColors.cs
- GestureRecognitionResult.cs
- Model3D.cs
- PrintDialog.cs
- StylusDownEventArgs.cs
- ExitEventArgs.cs
- DataGridAddNewRow.cs
- DataGridViewCellValueEventArgs.cs
- WorkflowViewStateService.cs
- DeclarativeCatalogPart.cs
- PlainXmlSerializer.cs
- TreeViewDesigner.cs
- X509CertificateCollection.cs
- ConfigurationFileMap.cs
- ActivityWithResult.cs
- WSDualHttpSecurityMode.cs
- XPathNodeIterator.cs
- TypeBuilder.cs
- InfiniteIntConverter.cs
- CreateUserWizard.cs
- RC2.cs
- XPathItem.cs
- SizeIndependentAnimationStorage.cs
- Unit.cs
- Label.cs
- AttachedAnnotationChangedEventArgs.cs
- DataKeyCollection.cs
- ProcessProtocolHandler.cs
- Light.cs
- CodeIndexerExpression.cs
- TcpProcessProtocolHandler.cs
- TcpClientSocketManager.cs
- BindingSourceDesigner.cs
- ButtonBaseDesigner.cs
- ToolboxItemAttribute.cs
- RegexGroupCollection.cs
- ViewgenContext.cs
- ListItemCollection.cs
- JsonByteArrayDataContract.cs
- MetricEntry.cs