System.Windows.Forms.ToolStripManager.Merge C# (CSharp) Method

Merge() private method

private Merge ( ToolStrip sourceToolStrip, ToolStrip targetToolStrip ) : bool
sourceToolStrip ToolStrip
targetToolStrip ToolStrip
return bool
		public static bool Merge (ToolStrip sourceToolStrip, ToolStrip targetToolStrip)
		{
			// Check for exceptions
			if (sourceToolStrip == null)
				throw new ArgumentNullException ("sourceToolStrip");
				
			if (targetToolStrip == null)
				throw new ArgumentNullException ("targetName");
				
			if (targetToolStrip == sourceToolStrip)
				throw new ArgumentException ("Source and target ToolStrip must be different.");
			
			// If the toolstrips don't allow merging, don't merge them
			if (!sourceToolStrip.AllowMerge || !targetToolStrip.AllowMerge)
				return false;
			
			// We currently can't support merging multiple times
			if (sourceToolStrip.IsCurrentlyMerged || targetToolStrip.IsCurrentlyMerged)
				return false;
				
			// What I wouldn't give to be able to modify a collection
			// while enumerating through it...

			List<ToolStripItem> items_to_move = new List<ToolStripItem> ();
			
			// Create a list of every ToolStripItem we plan on moving
			foreach (ToolStripItem tsi in sourceToolStrip.Items) {
				switch (tsi.MergeAction) {
					case MergeAction.Append:
					default:
						items_to_move.Add (tsi);
						break;
					case MergeAction.Insert:
						if (tsi.MergeIndex >= 0)
							items_to_move.Add (tsi);
						break;
					case MergeAction.Replace:
					case MergeAction.Remove:
					case MergeAction.MatchOnly:
						foreach (ToolStripItem target_tsi in targetToolStrip.Items)
							if (tsi.Text == target_tsi.Text) {
								items_to_move.Add (tsi);
								break;
							}
						break;
				}
			}

			// If there was nothing valid to merge, return false
			if (items_to_move.Count == 0)
				return false;
				
			// Set some state so we can unmerge later
			sourceToolStrip.BeginMerge ();
			targetToolStrip.BeginMerge ();			

			sourceToolStrip.SuspendLayout ();
			targetToolStrip.SuspendLayout ();
	
			while (items_to_move.Count > 0) {
				ToolStripItem tsi = items_to_move[0];
				items_to_move.Remove (tsi);
				
				switch (tsi.MergeAction) {
					case MergeAction.Append:
					default:
						// Just changing the parent will append it to the target
						// and remove it from the source
						ToolStrip.SetItemParent (tsi, targetToolStrip);
						
						break;
					case MergeAction.Insert:
						// Do the same work as Append, except Insert it into the
						// location specified by the MergeIndex
						RemoveItemFromParentToolStrip (tsi);

						if (tsi.MergeIndex == -1)
							continue;
						else if (tsi.MergeIndex >= CountRealToolStripItems (targetToolStrip))
							targetToolStrip.Items.AddNoOwnerOrLayout (tsi);						
						else
							targetToolStrip.Items.InsertNoOwnerOrLayout (AdjustItemMergeIndex (targetToolStrip, tsi), tsi);

						tsi.Parent = targetToolStrip;
						
						break;
					case MergeAction.Replace:
						// Find a target ToolStripItem with the same Text, remove it
						// and replace it with the source one
						foreach (ToolStripItem target_tsi in targetToolStrip.Items)
							if (tsi.Text == target_tsi.Text) {
								RemoveItemFromParentToolStrip (tsi);

								// Insert where the old one is, then remove the old one
								targetToolStrip.Items.InsertNoOwnerOrLayout (targetToolStrip.Items.IndexOf (target_tsi), tsi);
								targetToolStrip.Items.RemoveNoOwnerOrLayout (target_tsi);

								// Store the replaced one so we can get it back in unmerge
								targetToolStrip.HiddenMergedItems.Add (target_tsi);
								break;
							}

						break;
					case MergeAction.Remove:
						// Find a target ToolStripItem with the same Text, and remove
						// it from the target, nothing else
						foreach (ToolStripItem target_tsi in targetToolStrip.Items)
							if (tsi.Text == target_tsi.Text) {
								targetToolStrip.Items.RemoveNoOwnerOrLayout (target_tsi);
								
								// Store the removed one so we can get it back in unmerge
								targetToolStrip.HiddenMergedItems.Add (target_tsi);
								break;
							}

						break;
					case MergeAction.MatchOnly:
						// Ugh, find the target ToolStripItem with the same Text, and take
						// all the subitems from the source one, and append it to the target one
						foreach (ToolStripItem target_tsi in targetToolStrip.Items)
							if (tsi.Text == target_tsi.Text) {
								if (target_tsi is ToolStripMenuItem && tsi is ToolStripMenuItem) {
									ToolStripMenuItem source = (ToolStripMenuItem)tsi;
									ToolStripMenuItem target = (ToolStripMenuItem)target_tsi;
									
									ToolStripManager.Merge (source.DropDown, target.DropDown);
								}
								
								break;
							}
						
						break;
				}
			}
			
			sourceToolStrip.ResumeLayout ();
			targetToolStrip.ResumeLayout ();
			
			// Store who we merged with, so we can unmerge when only given the target toolstrip
			sourceToolStrip.CurrentlyMergedWith = targetToolStrip;
			targetToolStrip.CurrentlyMergedWith = sourceToolStrip;
			
			return true;
		}

Same methods

ToolStripManager::Merge ( ToolStrip sourceToolStrip, string targetName ) : bool

Usage Example

Example #1
0
        internal bool SetWindowStates(MdiWindowManager wm)
        {
            /*
             *      MDI WindowState behaviour:
             *      - If the active window is maximized, all other maximized windows are normalized.
             *      - If a normal window gets focus and the original active window was maximized,
             *        the normal window gets maximized and the original window gets normalized.
             *      - If a minimized window gets focus and the original window was maximized,
             *        the minimzed window gets maximized and the original window gets normalized.
             *        If the ex-minimized window gets deactivated, it will be normalized.
             */
            Form form = wm.form;

            if (setting_windowstates)
            {
                return(false);
            }

            if (!form.Visible)
            {
                return(false);
            }

            bool is_active     = wm.IsActive;
            bool maximize_this = false;

            if (!is_active)
            {
                return(false);
            }

            ArrayList minimize_these  = new ArrayList();
            ArrayList normalize_these = new ArrayList();

            setting_windowstates = true;
            foreach (Form frm in mdi_child_list)
            {
                if (frm == form)
                {
                    continue;
                }
                else if (!frm.Visible)
                {
                    continue;
                }
                if (frm.WindowState == FormWindowState.Maximized && is_active)
                {
                    maximize_this = true;
                    if (((MdiWindowManager)frm.window_manager).was_minimized)
                    {
                        minimize_these.Add(frm);
                    }
                    else
                    {
                        normalize_these.Add(frm);
                    }
                }
            }

            if (maximize_this && form.WindowState != FormWindowState.Maximized)
            {
                wm.was_minimized = form.window_state == FormWindowState.Minimized;
                form.WindowState = FormWindowState.Maximized;
            }

            foreach (Form frm in minimize_these)
            {
                frm.WindowState = FormWindowState.Minimized;
            }

            foreach (Form frm in normalize_these)
            {
                frm.WindowState = FormWindowState.Normal;
            }


            SetParentText(false);

            XplatUI.RequestNCRecalc(ParentForm.Handle);
            XplatUI.RequestNCRecalc(Handle);

            SizeScrollBars();

            setting_windowstates = false;

            if (form.MdiParent.MainMenuStrip != null)
            {
                form.MdiParent.MainMenuStrip.RefreshMdiItems();
            }

            // Implicit menu strip merging
            // - When child is activated
            // - Parent form must have a MainMenuStrip
            // - Find the first menustrip on the child
            // - Merge
            MenuStrip parent_menu = form.MdiParent.MainMenuStrip;

            if (parent_menu != null)
            {
                if (parent_menu.IsCurrentlyMerged)
                {
                    ToolStripManager.RevertMerge(parent_menu);
                }

                MenuStrip child_menu = LookForChildMenu(form);

                if (form.WindowState != FormWindowState.Maximized)
                {
                    RemoveControlMenuItems(wm);
                }

                if (form.WindowState == FormWindowState.Maximized)
                {
                    bool found = false;

                    foreach (ToolStripItem tsi in parent_menu.Items)
                    {
                        if (tsi is MdiControlStrip.SystemMenuItem)
                        {
                            (tsi as MdiControlStrip.SystemMenuItem).MdiForm = form;
                            found = true;
                        }
                        else if (tsi is MdiControlStrip.ControlBoxMenuItem)
                        {
                            (tsi as MdiControlStrip.ControlBoxMenuItem).MdiForm = form;
                            found = true;
                        }
                    }

                    if (!found)
                    {
                        parent_menu.SuspendLayout();
                        parent_menu.Items.Insert(0, new MdiControlStrip.SystemMenuItem(form));
                        parent_menu.Items.Add(new MdiControlStrip.ControlBoxMenuItem(form, MdiControlStrip.ControlBoxType.Close));
                        parent_menu.Items.Add(new MdiControlStrip.ControlBoxMenuItem(form, MdiControlStrip.ControlBoxType.Max));
                        parent_menu.Items.Add(new MdiControlStrip.ControlBoxMenuItem(form, MdiControlStrip.ControlBoxType.Min));
                        parent_menu.ResumeLayout();
                    }
                }

                if (child_menu != null)
                {
                    ToolStripManager.Merge(child_menu, parent_menu);
                }
            }

            return(maximize_this);
        }
All Usage Examples Of System.Windows.Forms.ToolStripManager::Merge