📄 treenodecollectioneditor.cs
字号:
//------------------------------------------------------------------------------
// Copyright (c) 2000-2003 Microsoft Corporation. All Rights Reserved.
//------------------------------------------------------------------------------
namespace Microsoft.Web.UI.WebControls.Design
{
using System.Runtime.InteropServices;
using System.ComponentModel;
using System;
using System.Design;
using System.Collections;
using Microsoft.Win32;
using System.ComponentModel.Design;
using System.Diagnostics;
using System.Drawing;
using System.Windows.Forms;
using Microsoft.Web.UI.WebControls;
/// <summary>
/// The TreeNodeCollectionEditor is a collection editor that is
/// specifically designed to edit a TreeNodeCollection.
/// </summary>
internal class TreeNodeCollectionEditor : CollectionEditor
{
/// <summary>
/// Constructs a new instance of a TreeNodeCollectionEditor.
/// </summary>
public TreeNodeCollectionEditor() : base(typeof(Microsoft.Web.UI.WebControls.TreeNodeCollection))
{
}
/// <summary>
/// Creates a new form to show the current collection.
/// You may inherit from CollectionForm to provide your own form.
/// </summary>
/// <returns>The form to show the currect collection.</returns>
protected override CollectionForm CreateCollectionForm()
{
return new TreeNodeCollectionForm(this);
}
private class TreeNodeCollectionForm : CollectionForm
{
private int nextNode = 0;
private Container components = new Container();
private System.Windows.Forms.TreeView tvNodes = new System.Windows.Forms.TreeView();
private Microsoft.Web.UI.WebControls.TreeView tvWebNodes = new Microsoft.Web.UI.WebControls.TreeView();
private Button btnAddChild = new Button();
private Button btnOK = new Button();
private Button btnApply = new Button();
private Button btnCancel = new Button();
private Button btnDelete = new Button();
private Button btnAddRoot = new Button();
private Button upButton = new Button();
private Button downButton = new Button();
private Button leftButton = new Button();
private Button rightButton = new Button();
private Label lblNodes = new Label();
private Label lblProp = new Label();
private GroupBox groupBox1 = new GroupBox();
private PropertyGrid propGrid = new PropertyGrid();
private object NextNodeKey = new object();
public TreeNodeCollectionForm(CollectionEditor editor) : base(editor)
{
InitializeComponent();
}
private Microsoft.Web.UI.WebControls.TreeView TreeView
{
get
{
if (Context != null && Context.Instance is Microsoft.Web.UI.WebControls.TreeView)
{
return (Microsoft.Web.UI.WebControls.TreeView)Context.Instance;
}
else
{
Debug.Assert(false, "TreeNodeCollectionEditor couldn't find the TreeView being designed");
return null;
}
}
}
private System.Windows.Forms.TreeNode SelectedNode
{
get
{
return tvNodes.SelectedNode;
}
set
{
tvNodes.SelectedNode = value;
if (value == null)
{
propGrid.SelectedObject = null;
lblProp.Text = "Properties:";
btnAddChild.Enabled = false;
btnDelete.Enabled = false;
upButton.Enabled = false;
downButton.Enabled = false;
leftButton.Enabled = false;
rightButton.Enabled = false;
}
else
{
propGrid.SelectedObject = value.Tag;
lblProp.Text = value.Text + "'s Properties:";
btnAddChild.Enabled = true;
btnDelete.Enabled = true;
upButton.Enabled = (value.PrevNode != null);
downButton.Enabled = (value.NextNode != null);
leftButton.Enabled = (value.Parent != null);
rightButton.Enabled = (value.PrevNode != null);
}
}
}
private int NextNode
{
get
{
if (TreeView != null && TreeView.Site != null)
{
IDictionaryService ds = (IDictionaryService)TreeView.Site.GetService(typeof(IDictionaryService));
Debug.Assert(ds != null, "TreeNodeCollectionEditor relies on IDictionaryService, which is not available.");
if (ds != null)
{
object dictionaryValue = ds.GetValue(NextNodeKey);
if (dictionaryValue != null)
{
nextNode = (int)dictionaryValue;
}
else
{
nextNode = 0;
ds.SetValue(NextNodeKey, 0);
}
}
}
return nextNode;
}
set
{
nextNode = value;
if (TreeView != null && TreeView.Site != null)
{
IDictionaryService ds = (IDictionaryService)TreeView.Site.GetService(typeof(IDictionaryService));
Debug.Assert(ds != null, "TreeNodeCollectionEditor relies on IDictionaryService, which is not available.");
if (ds != null)
{
ds.SetValue(NextNodeKey, nextNode);
}
}
}
}
private System.Windows.Forms.TreeNode Add(System.Windows.Forms.TreeNode parent)
{
string strText = "Node" + NextNode++.ToString();
System.Windows.Forms.TreeNode newnode;
Microsoft.Web.UI.WebControls.TreeNodeCollection col;
Microsoft.Web.UI.WebControls.TreeNode webNode;
if (parent == null)
{
newnode = tvNodes.Nodes.Add(strText);
col = tvWebNodes.Nodes;
}
else
{
newnode = parent.Nodes.Add(strText);
parent.Expand();
col = ((Microsoft.Web.UI.WebControls.TreeNode)parent.Tag).Nodes;
}
webNode = new Microsoft.Web.UI.WebControls.TreeNode();
webNode.Text = strText;
col.Add(webNode);
newnode.Tag = webNode;
btnApply.Enabled = true;
return newnode;
}
private void BtnAddChild_click(object sender, EventArgs e)
{
Add(SelectedNode);
// Refresh arrow states
SelectedNode = SelectedNode;
}
private void BtnAddRoot_click(object sender, EventArgs e)
{
System.Windows.Forms.TreeNode rootNode = Add(null);
if (SelectedNode == null)
{
SelectedNode = rootNode;
}
else
{
// Refresh arrow states
SelectedNode = SelectedNode;
}
}
private void BtnDelete_click(object sender, EventArgs e)
{
if (SelectedNode == null)
{
return;
}
// Determine the new node to select
System.Windows.Forms.TreeNode selNode = null;
if (SelectedNode.NextNode != null)
{
selNode = SelectedNode.NextNode;
}
else if (SelectedNode.PrevNode != null)
{
selNode = SelectedNode.PrevNode;
}
else if (SelectedNode.Parent != null)
{
selNode = SelectedNode.Parent;
}
((Microsoft.Web.UI.WebControls.TreeNode)(SelectedNode.Tag)).Remove();
SelectedNode.Remove();
btnApply.Enabled = true;
SelectedNode = selNode;
}
private void BtnOK_click(object sender, EventArgs e)
{
object[] values = new object[tvWebNodes.Nodes.Count];
for (int i = 0; i < values.Length; i++)
{
values[i] = ((ICloneable)tvWebNodes.Nodes[i]).Clone();
}
Items = values;
btnApply.Enabled = false;
}
/// <summary>
/// NOTE: The following code is required by the form
/// designer. It can be modified using the form editor. Do not
/// modify it using the code editor.
/// </summary>
private void InitializeComponent()
{
tvNodes.Location = new Point(10, 30);
tvNodes.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Bottom | AnchorStyles.Right;
tvNodes.Size = new Size(271, 260);
tvNodes.TabIndex = 1;
tvNodes.Text = "tvNodes";
tvNodes.HideSelection = false;
tvNodes.ImageIndex = 0;
tvNodes.Indent = 19;
tvNodes.LabelEdit = true;
tvNodes.AfterLabelEdit += new NodeLabelEditEventHandler(this.TvNodes_afterLabelEdit);
tvNodes.AfterSelect += new TreeViewEventHandler(this.TvNodes_afterSelect);
propGrid.CommandsVisibleIfAvailable = true;
propGrid.Location = new Point(316, 30);
propGrid.Size = new System.Drawing.Size(215, 260);
propGrid.TabIndex = 10;
propGrid.Anchor = AnchorStyles.Bottom | AnchorStyles.Top | AnchorStyles.Right;
propGrid.PropertyValueChanged += new PropertyValueChangedEventHandler(this.PropertyChanged);
btnOK.Location = new Point(256, 352);
btnOK.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
btnOK.Size = new Size(75, 23);
btnOK.TabIndex = 11;
btnOK.Text = "OK";
btnOK.DialogResult = DialogResult.OK;
btnOK.Click += new EventHandler(this.BtnOK_click);
btnCancel.Location = new Point(350, 352);
btnCancel.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
btnCancel.Size = new Size(75, 23);
btnCancel.TabIndex = 12;
btnCancel.Text = "Cancel";
btnCancel.DialogResult = DialogResult.Cancel;
btnApply.Enabled = false;
btnApply.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
btnApply.Location = new Point(444, 352);
btnApply.Size = new Size(80, 23);
btnApply.TabIndex = 13;
btnApply.Text = "Apply";
btnApply.Click += new EventHandler(this.BtnOK_click);
upButton.Anchor = AnchorStyles.Top | AnchorStyles.Right;
upButton.Location = new Point(286, 30);
upButton.Size = new Size(23, 23);
upButton.TabIndex = 2;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -