/// <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); }
/// <summary> /// Changes a specific ToolStripItem property in all its instances /// </summary> /// <param name="item">ToolStripItem instance</param> /// <param name="change">Property to change</param> /// <param name="newValue">New value to change</param> public static void ChangeAll(this ToolStripItem item, ToolbarHelper.ConstantsChangeAll change, object newValue) { ToolStripItemWrapper itemWrapper = (item as ToolStripItemWrapper); switch (change) { case ToolbarHelper.ConstantsChangeAll.ssChangeAllName: itemWrapper.Text = Convert.ToString(newValue); // Text in .NET is the equivalent for Name in VB6 break; case ToolbarHelper.ConstantsChangeAll.ssChangeAllVisible: itemWrapper.Visible = Convert.ToBoolean(newValue); break; case ToolbarHelper.ConstantsChangeAll.ssChangeAllDisplayStyle: //pending break; case ToolbarHelper.ConstantsChangeAll.ssChangeAllCtlWidth: //no uses in the code break; case ToolbarHelper.ConstantsChangeAll.ssChangeAllForeColor: //no uses in the code break; } }
/// <summary> /// Adds a new ToolStripItem /// </summary> /// <param name="toolList">List of Tools of the Toolbar</param> /// <param name="toolId">Id of the tool</param> /// <param name="style">Type of ToolStripItem</param> /// <param name="index">Index to insert</param> /// <returns>A new ToolStrip Item</returns> private static ToolStripItem Add(ToolsList toolList, string toolId = "", ToolbarHelper.ConstantsType style = ToolbarHelper.ConstantsType.ssTypeButton, int index = 0) { //First we check if we are inserting in the Tools of the Toolbar or in the Tools of a ToolStripItem if (InsertionInToolsOfToolStripItem(toolList)) { //Let's create the real ToolStripItem which is the one that will be displayed in the Form var tuple = AddToolStripItem(toolList, toolId, index); if (tuple != null) { //If index is greater that List Count assign back the count to the index to keep consistency if (index > toolList.Count) { index = toolList.Count; } //Now the ToolStripItem is included as part of the tools of the ToolStripItem toolList.Insert(index, tuple.Item2); //Return the ToolStripItem created return(tuple.Item1); } } else { //If the code jumps here means we are inserting ToolStripItems in the Tools of the Toolbar //Create the wrapper for ToolStripItem ToolStripItemWrapper itemWrapper = new ToolStripItemWrapper(); itemWrapper.ToolbarParent = toolList.ToolbarParent; //Assign its Toolbar parent itemWrapper.Name = toolId; //Assign its id itemWrapper.Text = SetText(itemWrapper.Text, toolId); //Assign its text itemWrapper.ConstantsType = style; //Assign the type //If the Type of the ToolStripItem will be Combobox, then create the combobox instance for its handling if (style == ToolbarHelper.ConstantsType.ssTypeComboBox) { itemWrapper.ComboBox = new ComboBoxWrapper(itemWrapper.ToolbarParent, itemWrapper.Name); } //If the Type of the ToolStripItem will be Menu, then create the menu instance for its handling else if (style == ToolbarHelper.ConstantsType.ssTypeMenu) { itemWrapper.Menu = new ToolStripItemWrapperMenu(toolId, toolList.ToolbarParent); //It's important to mention this: When a ToolStripItem of type Menu is created it can be work as a popup menu into any other control. //To support this we add a new ToolStripDown and we include the id of the tool in the list of popup menus toolList.ToolbarParent.PopupMenus.Add(toolId, CreateToolStripDown(itemWrapper, toolId)); } //Finally the new ToolStripItem is added to the list of tools of the toolbar toolList.Add(itemWrapper); //Return the ToolStripItem created return(itemWrapper); } return(null); }
/// <summary> /// Creates a new instance of ToolStripDropDown used to handle the popup menus /// </summary> /// <param name="itemWrapper">ToolStripItem wrapped</param> /// <param name="toolId">ID of the tool</param> /// <returns>A new instance of ToolStripItem</returns> private static ToolStripDropDown CreateToolStripDown(ToolStripItemWrapper itemWrapper, string toolId) { //Create a new instance of ToolStripDropDown ToolStripDropDown _toolStripDropDown = new ToolStripDropDown { CanOverflow = true, AutoClose = true, DropShadowEnabled = true, Name = toolId }; //Assign the ItemClick and a delegate is defined to invoke the click logic (behind the hoods) AssignClickToToolStripDropDown(itemWrapper, ref _toolStripDropDown); //Return the instance of ToolStripDropDown return(_toolStripDropDown); }
/// <summary> /// Extension method created to define the state of a ToolStripItem /// </summary> /// <param name="item">Instance of ToolStripItem</param> /// <param name="state">New State</param> public static void setState(this ToolStripItemWrapper item, ToolbarHelper.ConstantsState state) { //Change the state in the tools of each toolbar and also in the items of each Toolbar foreach (IToolbar toolbar in item.ToolbarParent.Toolbars) { var toolStripButtonState = (toolbar as ToolStrip).Items[item.Name] as ToolStripButton; if (toolStripButtonState != null) { toolStripButtonState.CheckState = (state == ToolbarHelper.ConstantsState.ssChecked) ? CheckState.Checked : CheckState.Unchecked; } var toolStripItem = toolbar.Tools[item.Name]; if (toolStripItem != null) { toolStripItem.State = state; } } }
/// <summary> /// Assigns the itemclick event to the ToolStripDropDown /// </summary> /// <param name="itemWrapper">ToolStripItem of type Menu</param> /// <param name="_toolStripDropDown">Instance of the ToolStripDropDown</param> private static void AssignClickToToolStripDropDown(ToolStripItemWrapper itemWrapper, ref ToolStripDropDown _toolStripDropDown) { //By reflection the method info and create the delegate which will invoke the logic when the items of the ToolStripDropDown are clicked Type parentType = (itemWrapper.ToolbarParent as ToolStripWrapper).Parent.GetType(); MethodInfo methodInfo = parentType.GetMethod((itemWrapper.ToolbarParent as ToolStripWrapper).Name + ToolbarHelper.HANDLER_PREFIX, BindingFlags.NonPublic | BindingFlags.Instance); if (methodInfo != null) { _toolStripDropDown.ItemClicked += (sender, args) => { methodInfo.Invoke((itemWrapper.ToolbarParent as ToolStripWrapper).Parent, new object[] { itemWrapper.ToolbarParent, args }); }; } }
public static object GetRealOldValue(this ToolStripItem item, string property, object value) { object valueToReturn = value; if (property.Equals("Visible") || property.Equals("Enabled")) { ToolStripWrapper toolbarRoot = (item as ToolStripItemWrapper).ToolbarParent as ToolStripWrapper; if (toolbarRoot == null) { return(valueToReturn); } ToolStripItemWrapper itemInTools = toolbarRoot.Tools[item.Name]; if (itemInTools == null) { return(valueToReturn); } foreach (ToolStripWrapper toolbar in toolbarRoot.Toolbars) { if (toolbar.Items.ContainsKey(item.Name)) { if (property.Equals("Visible")) { valueToReturn = toolbar.Items[item.Name].Visible; break; } if (property.Equals("Enabled")) { valueToReturn = toolbar.Items[item.Name].Enabled; break; } } } } return(valueToReturn); }
/// <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 } } } } } } } }
/// <summary> /// Creates a new instance of ToolStripItem /// </summary> /// <param name="type">ToolStripItem type</param> /// <param name="wrapper">ToolStripItem wrapped</param> /// <returns>A new instance of ToolStripItem</returns> private static ToolStripItem CreateToolStripItem(ToolbarHelper.ConstantsType type, ToolStripItemWrapper wrapper) { ToolStripItem item = null; switch (type) { case ToolbarHelper.ConstantsType.ssTypeButton: item = new ToolStripButton(); break; case ToolbarHelper.ConstantsType.ssTypeStateButton: item = new ToolStripButton(); break; case ToolbarHelper.ConstantsType.ssTypeMenu: item = new ToolStripDropDownButton(); break; case ToolbarHelper.ConstantsType.ssTypeComboBox: item = new ToolStripComboBox(); break; case ToolbarHelper.ConstantsType.ssTypeSeparator: item = new ToolStripSeparator(); break; default: item = new ToolStripButton(); break; } if (wrapper != null) { //The properties of the wrapped object are cloned in the real ToolStripItem item = CloneProperties(item, wrapper); } return(item); }