Code:
/ DotNET / DotNET / 8.0 / untmp / whidbey / REDBITS / ndp / fx / src / Designer / WinForms / System / WinForms / Design / ChangeToolStripParentVerb.cs / 1 / ChangeToolStripParentVerb.cs
//------------------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//-----------------------------------------------------------------------------
/*
*/
namespace System.Windows.Forms.Design {
using System.Design;
using Accessibility;
using System.ComponentModel;
using System.Diagnostics;
using System;
using System.Security;
using System.Security.Permissions;
using System.Collections;
using System.ComponentModel.Design;
using System.Windows.Forms;
using System.Runtime.Serialization;
using System.ComponentModel.Design.Serialization;
using System.Drawing;
using System.Windows.Forms.Design.Behavior;
using System.Drawing.Design;
using System.Diagnostics.CodeAnalysis;
///
/// Internal class to provide 'Embed in ToolStripContainer" verb for ToolStrips & MenuStrips.
///
internal class ChangeToolStripParentVerb {
private ToolStripDesigner _designer;
private IDesignerHost _host;
private IComponentChangeService componentChangeSvc;
private IServiceProvider _provider;
///
/// Create one of these things...
///
internal ChangeToolStripParentVerb(string text, ToolStripDesigner designer) {
Debug.Assert(designer != null, "Can't have a StandardMenuStripVerb without an associated designer");
this._designer = designer;
this._provider = designer.Component.Site;
this._host = (IDesignerHost)_provider.GetService(typeof(IDesignerHost));
componentChangeSvc = (IComponentChangeService)_provider.GetService(typeof(IComponentChangeService));
}
///
/// When the verb is invoked, change the parent of the ToolStrip.
///
[SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")]
[SuppressMessage("Microsoft.Security", "CA2102:CatchNonClsCompliantExceptionsInGeneralHandlers")]
// This is actually called...
[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
public void ChangeParent() {
Cursor current = Cursor.Current;
// create a transaction so this happens as an atomic unit.
DesignerTransaction changeParent = _host.CreateTransaction("Add ToolStripContainer Transaction");
try
{
Cursor.Current = Cursors.WaitCursor;
//Add a New ToolStripContainer to the RootComponent ...
Control root = _host.RootComponent as Control;
ParentControlDesigner rootDesigner = _host.GetDesigner(root) as ParentControlDesigner;
if (rootDesigner != null)
{
// close the DAP first - this is so that the autoshown panel on drag drop here is not conflicting with the currently opened panel
// if the verb was called from the panel
ToolStrip toolStrip = _designer.Component as ToolStrip;
if(toolStrip != null && _designer != null && _designer.Component != null && _provider != null) {
DesignerActionUIService dapuisvc = _provider.GetService(typeof(DesignerActionUIService)) as DesignerActionUIService;
dapuisvc.HideUI(toolStrip);
}
// Get OleDragHandler ...
ToolboxItem tbi = new ToolboxItem(typeof(System.Windows.Forms.ToolStripContainer));
OleDragDropHandler ddh = rootDesigner.GetOleDragHandler();
if (ddh != null)
{
IComponent[] newComp = ddh.CreateTool(tbi, root, 0, 0, 0, 0, false, false);
ToolStripContainer tsc = newComp[0] as ToolStripContainer;
if (tsc != null)
{
if(toolStrip != null)
{
IComponentChangeService changeSvc = _provider.GetService(typeof(IComponentChangeService)) as IComponentChangeService;
Control newParent = GetParent(tsc, toolStrip);
PropertyDescriptor controlsProp = TypeDescriptor.GetProperties(newParent)["Controls"];
Control oldParent = toolStrip.Parent;
if (oldParent != null) {
changeSvc.OnComponentChanging(oldParent, controlsProp);
//remove control from the old parent
oldParent.Controls.Remove(toolStrip);
}
if (newParent != null)
{
changeSvc.OnComponentChanging(newParent, controlsProp);
//finally add & relocate the control with the new parent
newParent.Controls.Add(toolStrip);
}
//fire our comp changed events
if (changeSvc != null && oldParent != null && newParent != null) {
changeSvc.OnComponentChanged(oldParent, controlsProp, null, null);
changeSvc.OnComponentChanged(newParent, controlsProp, null, null);
}
//Set the Selection on the new Parent ... so that the selection is restored to the new item,
ISelectionService selSvc = _provider.GetService(typeof(ISelectionService)) as ISelectionService;
if (selSvc != null)
{
selSvc.SetSelectedComponents(new IComponent[] { tsc });
}
}
}
}
}
}
catch (Exception e){
if (e is System.InvalidOperationException) {
IUIService uiService = (IUIService)_provider.GetService(typeof(IUIService));
uiService.ShowError(e.Message);
}
if (changeParent != null)
{
changeParent.Cancel();
changeParent = null;
}
}
finally {
if (changeParent != null)
{
changeParent.Commit();
changeParent = null;
}
Cursor.Current = current;
}
}
private Control GetParent(ToolStripContainer container, Control c)
{
Control newParent = container.ContentPanel;
DockStyle dock = c.Dock;
if (c.Parent is ToolStripPanel)
{
dock = c.Parent.Dock;
}
foreach(Control panel in container.Controls)
{
if (panel is ToolStripPanel)
{
if (panel.Dock == dock)
{
newParent = panel;
break;
}
}
}
return newParent;
}
}
}
// 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
- VersionedStream.cs
- RadialGradientBrush.cs
- XmlQuerySequence.cs
- StrokeNode.cs
- TypeHelper.cs
- ExternalDataExchangeService.cs
- TitleStyle.cs
- FtpWebResponse.cs
- ProfileSettings.cs
- ProgressBar.cs
- DesignerCalendarAdapter.cs
- DataGridViewRowErrorTextNeededEventArgs.cs
- DashStyles.cs
- XsdSchemaFileEditor.cs
- PartialCachingAttribute.cs
- HandleCollector.cs
- PlatformCulture.cs
- XmlILCommand.cs
- InlineObject.cs
- ScriptResourceInfo.cs
- DrawingBrush.cs
- WindowsGraphics.cs
- EdmEntityTypeAttribute.cs
- RelationshipEndMember.cs
- RemotingClientProxy.cs
- SecureConversationServiceCredential.cs
- DeviceContexts.cs
- InstanceKeyCompleteException.cs
- QueryOptionExpression.cs
- ServiceModelStringsVersion1.cs
- AliasGenerator.cs
- BaseParaClient.cs
- StylusPlugInCollection.cs
- XPathNodeInfoAtom.cs
- _DisconnectOverlappedAsyncResult.cs
- IProducerConsumerCollection.cs
- TextWriterEngine.cs
- Form.cs
- EventTask.cs
- DataGridViewCellStyleChangedEventArgs.cs
- CryptoApi.cs
- AsyncOperation.cs
- SpeakInfo.cs
- EntityParameterCollection.cs
- OperandQuery.cs
- IChannel.cs
- FloaterBaseParagraph.cs
- WindowExtensionMethods.cs
- WebPartConnectionsConnectVerb.cs
- ClientBuildManager.cs
- FindRequestContext.cs
- AdditionalEntityFunctions.cs
- Quad.cs
- JournalNavigationScope.cs
- baseshape.cs
- WindowsSpinner.cs
- DesignerActionHeaderItem.cs
- DataServicePagingProviderWrapper.cs
- TypedTableBase.cs
- BindingElementCollection.cs
- OleDbTransaction.cs
- SqlTypesSchemaImporter.cs
- ExpressionContext.cs
- ServiceDescriptionReflector.cs
- SspiHelper.cs
- XmlNamespaceMapping.cs
- AutoGeneratedFieldProperties.cs
- arc.cs
- WebPartZone.cs
- ChannelSinkStacks.cs
- DoubleCollection.cs
- SafeEventLogWriteHandle.cs
- MetadataHelper.cs
- SafeWaitHandle.cs
- MimeAnyImporter.cs
- OleDbDataReader.cs
- WindowsScrollBar.cs
- UntrustedRecipientException.cs
- XmlSchemaAnyAttribute.cs
- XmlSchemaAnnotated.cs
- ContentDisposition.cs
- MembershipPasswordException.cs
- TemplateNameScope.cs
- ListBoxAutomationPeer.cs
- SiteMap.cs
- OleDbPropertySetGuid.cs
- FirstMatchCodeGroup.cs
- FocusTracker.cs
- ValueQuery.cs
- MachineKeySection.cs
- MemberProjectionIndex.cs
- TypefaceCollection.cs
- CodeTypeReferenceCollection.cs
- CustomLineCap.cs
- ObjectKeyFrameCollection.cs
- MetadataPropertyCollection.cs
- NavigationCommands.cs
- externdll.cs
- RawStylusSystemGestureInputReport.cs
- ColorConverter.cs