Beispiel #1
0
        /// <summary>
        /// Clones the properties from the ToolStripItem wrapped to the real ToolStripItem
        /// </summary>
        /// <param name="item">ToolStripItem target</param>
        /// <param name="wrapper">ToolStripItem wrapped</param>
        /// <returns>A new instance of ToolStripItem</returns>
        private static ToolStripItem CloneProperties(ToolStripItem item, ToolStripItemWrapper wrapper)
        {
            var propertiesTarget = item.GetType().GetProperties();

            foreach (var propertyTarget in propertiesTarget)
            {
                try
                {
                    if (null != propertyTarget && propertyTarget.CanWrite)
                    {
                        //Visible and Enabled properties are handled internally depending on the life cycle events. That's the reason why are not copied.
                        if (propertyTarget.Name.Equals("Visible") || propertyTarget.Name.Equals("Enabled"))
                        {
                            continue;
                        }

                        var propertySrc = wrapper.GetType().GetProperty(propertyTarget.Name, BindingFlags.Public | BindingFlags.Instance);
                        if (null != propertySrc)
                        {
                            propertyTarget.SetValue(item, propertySrc.GetValue(wrapper, null), null);
                        }
                    }
                }
                catch { }
            }

            return(item);
        }
Beispiel #2
0
        /// <summary>
        /// Spread the change of the property in all instances ( tools of toolbar, tools of tools, tools of menu, popup menu etc)
        /// </summary>
        /// <param name="item">ToolStripItem instance</param>
        /// <param name="property">Name of the property to update</param>
        /// <param name="value">Value to update</param>
        /// <param name="excludeChangeInToolList">True to avoid updating in the instance(first parameter) because was updated previously</param>
        public static void SpreadChange(this ToolStripItem item, string property, object value, bool excludeChangeInToolList = false)
        {
            PropertyInfo prop = null;

            //Get the toolbar of the ToolStripItem
            ToolStripWrapper toolbarRoot = (item as ToolStripItemWrapper).ToolbarParent as ToolStripWrapper;

            if (toolbarRoot == null)
            {
                return;                      //No change needed
            }
            //Get the tool in the tools of toolbar
            ToolStripItemWrapper itemInTools = toolbarRoot.Tools[item.Name];

            if (itemInTools == null)
            {
                return;                      //No change needed
            }
            //The property in the tools list of the toolbar is updated
            prop = itemInTools.GetType().GetProperty(property, BindingFlags.Public | BindingFlags.Instance);
            if (null != prop && prop.CanWrite && !excludeChangeInToolList)
            {
                prop.SetValue(itemInTools, value, null);
            }

            //Checks if the popupmenus collection was already reviewed. The popup menus collection is stored in the toolbar root ( the one defined in the designer ). Must be checked 1 time.
            bool popupMenuAlreadyChecked = false;

            //Then we need to get all instances of the ToolStripItem in the toolbars
            foreach (IToolbar toolbar in toolbarRoot.Toolbars)
            {
                //Get the tool in the toolbar items
                var itemInToolbar = (toolbar as ToolStrip).Items[item.Name];
                if (itemInToolbar != null)
                {
                    //The property is updated in the tool
                    prop = itemInToolbar.GetType().GetProperty(property, BindingFlags.Public | BindingFlags.Instance);
                    if (null != prop && prop.CanWrite)
                    {
                        prop.SetValue(itemInToolbar, value, null);
                    }

                    if (toolbar.ToolbarRoot != null && !popupMenuAlreadyChecked)
                    {
                        //Get the popup menus
                        var popupMenus = toolbar.ToolbarRoot.PopupMenus;
                        if (popupMenus.Count > 0)
                        {
                            //Iterate the popup menus of the control
                            foreach (var popupMenu in popupMenus)
                            {
                                if (popupMenu.Value != null)
                                {
                                    //If the key of the tool is present in the popup menus list
                                    if (popupMenu.Value.Items.ContainsKey(item.Name))
                                    {
                                        //Get the Value of the PopupMenu and update its value in the list
                                        var itemInPopupMenu = popupMenu.Value.Items[item.Name];
                                        prop = itemInPopupMenu.GetType().GetProperty(property, BindingFlags.Public | BindingFlags.Instance);
                                        if (null != prop && prop.CanWrite)
                                        {
                                            prop.SetValue(itemInPopupMenu, value, null);
                                        }
                                    }

                                    //Then we must update the GUI. For this we get the item in the toolbar items
                                    var menu = ((toolbar as ToolStrip).Items[popupMenu.Key.ToString()] as ToolStripDropDownButton);
                                    if (menu != null && menu.DropDownItems.ContainsKey(item.Name))
                                    {
                                        //If the key is present in the dropdown items we must update the value as well
                                        var submenu = menu.DropDownItems[item.Name];
                                        prop = submenu.GetType().GetProperty(property, BindingFlags.Public | BindingFlags.Instance);
                                        if (null != prop && prop.CanWrite)
                                        {
                                            prop.SetValue(submenu, value, null);
                                        }
                                        popupMenuAlreadyChecked = true; //The popup menus list was already reviewed
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }