//=------------------------------------------------------------------= // populateCurrentItemsForStrip //=------------------------------------------------------------------= /// <summary> /// Given a ToolStrip, go and populate the currentItemsListBOx /// list with all the buttons in it. /// </summary> /// /// <param name="in_strip"> /// The ToolStrip to process. /// </param> /// private void populateCurrentItemsForStrip(ToolStrip in_strip) { ToolStripItem[] items; ItemAndName ian; this.currentItemsListBox.Items.Clear(); items = fetchSelectedItemsForStrip(in_strip); // // for each item, go and add it to the list of currently // included items. // for (int x = 0; x < items.Length; x++) { ian = new ItemAndName(); // // replace any separators with our own special separator // item. // if (items[x] is ToolStripSeparator) { ian.Name = SEPARATOR_NAME; ian.Item = null; } else { ian.Name = items[x].Text; ian.Item = items[x]; } this.currentItemsListBox.Items.Add(ian); } // // Finally, add the special Anchor Spearator Item that cannot // be removed. // ian = new ItemAndName(); ian.Name = ANCHOR_SEPARATOR; ian.Item = null; this.currentItemsListBox.Items.Add(ian); }
//=------------------------------------------------------------------= // addButton_Click //=------------------------------------------------------------------= /// <summary> /// The user has clicked on the "Add >>" button, to move the /// currently selected button from the available list box to the /// currently used list box. This code attempts to execute that /// move. /// </summary> /// /// <param name="sender"> /// From whence cometh the event (Add button). /// </param> /// /// <param name="e"> /// Information about the event (in this case, none). /// </param> /// private void addButton_Click(object sender, EventArgs e) { int insertAt, insertFrom; ItemAndName ian; // // get the selected index, making sure it's valid. // insertFrom = this.availableItemsListBox.SelectedIndex; if (insertFrom == -1) { return; } // // add the item to the currentItemsListBox before the currently // selection. If it's a separator, we'll create a new instance // of it. // ian = (ItemAndName)this.availableItemsListBox.Items[insertFrom]; if (ian.Item == null) { ian = new ItemAndName(); ian.Name = SEPARATOR_NAME; ian.Item = null; } insertAt = this.currentItemsListBox.SelectedIndex; if (insertAt == -1) { insertAt = this.currentItemsListBox.Items.Count - 1; } this.currentItemsListBox.Items.Insert(insertAt, ian); // // remove it from the available list, provided it's not the // separator // if (ian.Item != null) { this.availableItemsListBox.Items.Remove(ian); // // make sure some item is still selected. // if (insertFrom < this.availableItemsListBox.Items.Count) { this.availableItemsListBox.SelectedIndex = insertFrom; } else { this.availableItemsListBox.SelectedIndex = insertFrom - 1; } } // // enable and disable the appropriate buttons. // updateButtons(); }
//=------------------------------------------------------------------= // populateAvailableItemsForStrip //=------------------------------------------------------------------= /// <summary> /// Given a list of items that are "available" to be used for a /// given ToolStrip (or MenuStrip), go and populate the /// availableItemsListBox with these items, removing those that /// are already in the strip. /// </summary> /// /// <param name="in_strip"> /// The ToolStrip for which we have a list of available items. /// </param> /// /// <param name="in_items"> /// The list of available items (or derived objects, of course). /// </param> /// /// <remarks> /// There are two minor limitations in this code: /// /// 1. For those cases where there is a ToolStrip Label that is /// clearly never intended to be on its own, such as one /// labelled "Search" next to a drop-down ComboBox, we have /// no way to group those right now. /// /// 2. Since ToolStripItems are parented strongly (each has an /// Owner property), we make sure that any button we have is /// not owned by another ToolStrip. This wouldn't be a 100% /// accurate search (in those cases where we were given items /// from ToolStrips about which we did not know), but it's /// good for the 90% case. /// </remarks> /// private void populateAvailableItemsForStrip ( ToolStrip in_strip, ToolStripItem[] in_items ) { ItemAndName ian; this.availableItemsListBox.Items.Clear(); // // firstly, add the separator item. // ian = new ItemAndName(); ian.Name = SEPARATOR_NAME; ian.Item = null; this.availableItemsListBox.Items.Add(ian); // // loop through the list of "available" items, adding those // which are not already in the list of items on the strip. // for (int x = 0; x < in_items.Length; x++) { // // add non separator items (we handle seps ourselves) // if (!(in_items[x] is ToolStripSeparator)) { if (!stripContainsItem(in_strip, in_items[x]) && !anyStripsContainItem(in_items[x])) { ian = new ItemAndName(); ian.Name = in_items[x].Text; ian.Item = in_items[x]; this.availableItemsListBox.Items.Add(ian); } } } }