/// <summary> /// Initializes a new instance of the NodeCollectionDialog class. /// </summary> /// <param name="original">Original Nodes to be edited.</param> public CommandCollectionDialog(CommandBaseCollection original) { // Required for Windows Form Designer support InitializeComponent(); // Create a new per command foreach (CommandBase command in original) { // Create a new tree node to hold the command Node newNode = new Node(); // Create a copy to attach to the node CommandBase copy = (CommandBase)command.Clone(); if (copy.GetType() == typeof(SeparatorCommand)) { newNode.Text = "(Separator)"; } else if (copy.GetType() == typeof(ButtonCommand)) { ButtonCommand button = copy as ButtonCommand; // Special case the absense of text if (button.Text.Length == 0) { newNode.Text = "<Empty>"; } else { newNode.Text = button.Text; } // We want to know when the text for the node changes button.TextChanged += new EventHandler(OnTextChanged); } // Attached a copy of the command to the node newNode.Tag = copy; // Append to end of the list treeControl1.Nodes.Add(newNode); } // Set correct initial state of the buttons UpdateButtonState(); }
/// <summary> /// Disposes of the resources (other than memory) used by the class. /// </summary> /// <param name="disposing">true to release both managed and unmanaged; false to release only unmanaged. </param> protected override void Dispose(bool disposing) { if (disposing) { // Remove any event handlers foreach (Node n in treeControl1.Nodes) { ButtonCommand button = n.Tag as ButtonCommand; // Remove any handler from the command if (button != null) { button.TextChanged -= new EventHandler(OnTextChanged); } } } base.Dispose(disposing); }
private void buttonRemove_Click(object sender, System.EventArgs e) { // No longer this object selected propertyGrid.SelectedObject = null; ButtonCommand button = treeControl1.SelectedNode.Tag as ButtonCommand; // Remove any handler from the command if (button != null) { button.TextChanged -= new EventHandler(OnTextChanged); } // Remove the selected item treeControl1.Nodes.Remove(treeControl1.SelectedNode); // Put focus back to the tree control treeControl1.Focus(); UpdateButtonState(); }
private void OnTextChanged(object sender, System.EventArgs e) { // Convert to correct class ButtonCommand button = sender as ButtonCommand; // Find the node the contains this command foreach (Node n in treeControl1.Nodes) { if (n.Tag == button) { // Update node with the new text if (button.Text.Length == 0) { n.Text = "<Empty>"; } else { n.Text = button.Text; } break; } } }