⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 treenodecollectioneditor.cs

📁 浏览器端看到树型目录结构,用户可以完整地看到像windows资源管理器一样的效果
💻 CS
📖 第 1 页 / 共 2 页
字号:
                upButton.Image = new Icon(typeof(CollectionEditor), "SortUp.ico").ToBitmap();
                upButton.Click += new EventHandler(this.UpButton_click);

                downButton.Anchor = AnchorStyles.Top | AnchorStyles.Right;
                downButton.Location = new Point(286, 59);
                downButton.Size = new Size(23, 23);
                downButton.TabIndex = 3;
                downButton.Image = new Icon(typeof(CollectionEditor), "SortDown.ico").ToBitmap();
                downButton.Click += new EventHandler(this.DownButton_click);

                leftButton.Anchor = AnchorStyles.Top | AnchorStyles.Right;
                leftButton.Location = new Point(286, 88);
                leftButton.Size = new Size(23, 23);
                leftButton.TabIndex = 4;
                leftButton.Image = new Icon(typeof(CollectionEditor), "SortDown.ico").ToBitmap();
                leftButton.Image.RotateFlip(RotateFlipType.Rotate90FlipNone);
                leftButton.Click += new EventHandler(this.LeftButton_click);

                rightButton.Anchor = AnchorStyles.Top | AnchorStyles.Right;
                rightButton.Location = new Point(286, 117);
                rightButton.Size = new Size(23, 23);
                rightButton.TabIndex = 5;
                rightButton.Image = new Icon(typeof(CollectionEditor), "SortDown.ico").ToBitmap();
                rightButton.Image.RotateFlip(RotateFlipType.Rotate270FlipNone);
                rightButton.Click += new EventHandler(this.RightButton_click);

                this.Text = "TreeNodeEditor";
                this.AcceptButton = btnOK;
                this.AutoScaleBaseSize = new Size(5, 13);
                this.CancelButton = btnCancel;
                this.ClientSize = new Size(539, 380);
                this.MinimumSize = new Size(544, 383);
                this.MaximizeBox = false;
                this.MinimizeBox = false;
                this.ShowInTaskbar = false;
                this.StartPosition = FormStartPosition.CenterScreen;

                btnAddRoot.Location = new Point(14, 302);
                btnAddRoot.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
                btnAddRoot.Size = new Size(80, 23);
                btnAddRoot.TabIndex = 6;
                btnAddRoot.Text = "Add Root";
                btnAddRoot.ImageAlign = ContentAlignment.MiddleLeft;
                //                btnAddRoot.Image = new Icon(typeof(TreeNodeCollectionEditor), "Folder.ico").ToBitmap();
                btnAddRoot.Click += new EventHandler(this.BtnAddRoot_click);

                btnAddChild.Enabled = false;
                btnAddChild.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
                btnAddChild.Location = new Point(107, 302);
                btnAddChild.Size = new Size(80, 23);
                btnAddChild.TabIndex = 7;
                btnAddChild.Text = "Add Child";
                btnAddChild.ImageAlign = ContentAlignment.MiddleLeft;
                //                btnAddChild.Image = new Icon(typeof(TreeNodeCollectionEditor), "ChildFolder.ico").ToBitmap();
                btnAddChild.Click += new EventHandler(this.BtnAddChild_click);

                btnDelete.Enabled = false;
                btnDelete.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
                btnDelete.Location = new Point(200, 302);
                btnDelete.Size = new Size(80, 23);
                btnDelete.TabIndex = 8;
                btnDelete.Text = "Delete";
                btnDelete.ImageAlign = ContentAlignment.MiddleLeft;
                //                btnDelete.Image = new Icon(typeof(TreeNodeCollectionEditor), "Delete.ico").ToBitmap();
                btnDelete.Click += new EventHandler(this.BtnDelete_click);

                groupBox1.Anchor = AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;
                groupBox1.Location = new Point(8, 340);
                groupBox1.Size = new Size(524, 8);
                groupBox1.TabIndex = 9;
                groupBox1.TabStop = false;
                groupBox1.Text = "";

                lblNodes.Location = new Point(10, 10);
                lblNodes.Size = new Size(150, 13);
                lblNodes.TabIndex = 0;
                lblNodes.TabStop = false;
                lblNodes.Text = "Select Node To Edit:";

                lblProp.Location = new Point(314, 10);
                lblProp.Size = new Size(220, 13);
                lblProp.TabIndex = 0;
                lblProp.TabStop = false;
                lblProp.Text = "Properties:";
                lblProp.Anchor = AnchorStyles.Bottom | AnchorStyles.Top | AnchorStyles.Right;

                this.Controls.Clear();                     
                this.Controls.AddRange(new Control[] {
                                                         btnDelete,
                                                         propGrid,
                                                         lblNodes,
                                                         lblProp,
                                                         btnAddRoot,
                                                         btnCancel,
                                                         btnOK,
                                                         btnAddChild,
                                                         btnApply,
                                                         groupBox1,
                                                         upButton,
                                                         downButton,
                                                         leftButton,
                                                         rightButton,
                                                         tvNodes});
            }
            
            /// <summary>
            /// This is called when the value property in the CollectionForm has changed.
            /// In it you should update your user interface to reflect the current value.
            /// </summary>
            protected override void OnEditValueChanged() 
            {
                if (EditValue != null)
                {
                    Microsoft.Web.UI.WebControls.TreeNodeCollection editCol = (Microsoft.Web.UI.WebControls.TreeNodeCollection)EditValue;
                    tvWebNodes.Site = ((Microsoft.Web.UI.WebControls.TreeView)editCol.Parent).Site;

                    object[] items = Items;
                    System.Windows.Forms.TreeNode[] nodes = new System.Windows.Forms.TreeNode[items.Length];

                    tvWebNodes.Nodes.Clear();
                    for (int i = 0; i < items.Length; i++) 
                    {
                        // We need to copy the nodes into our editor TreeView, not move them.
                        // We overwrite the passed-in array with the new roots.
                        //
                        Microsoft.Web.UI.WebControls.TreeNode webnode = (Microsoft.Web.UI.WebControls.TreeNode)((ICloneable)((Microsoft.Web.UI.WebControls.TreeNode)items[i])).Clone();
                        nodes[i] = WebNodeToFormNode(webnode);
                        tvWebNodes.Nodes.Add(webnode);
                    }

                    tvWebNodes.TreeNodeTypes.Clear();
                    foreach (TreeNodeType nodeType in ((Microsoft.Web.UI.WebControls.TreeView)editCol.Parent).TreeNodeTypes)
                    {
                        tvWebNodes.TreeNodeTypes.Add((TreeNodeType)nodeType.Clone());
                    }

                    tvNodes.Nodes.Clear();
                    tvNodes.Nodes.AddRange(nodes);
                    
                    // Update current node related UI
                    //
                    if (tvNodes.Nodes.Count > 0)
                    {
                        SelectedNode = tvNodes.Nodes[0];
                    }
                    else
                    {
                        SelectedNode = null;
                    }
                    btnApply.Enabled = false;
                }
            }

            private void TvNodes_afterSelect(object sender, TreeViewEventArgs e) 
            {
                SelectedNode = e.Node;
            }

            private void TvNodes_afterLabelEdit(object sender, NodeLabelEditEventArgs e)
            {
                ((Microsoft.Web.UI.WebControls.TreeNode)e.Node.Tag).Text = e.Label;
                propGrid.Refresh();
                btnApply.Enabled = true;
            }

            private System.Windows.Forms.TreeNode WebNodeToFormNode(Microsoft.Web.UI.WebControls.TreeNode webNode)
            {
                System.Windows.Forms.TreeNode node = new System.Windows.Forms.TreeNode();
                node.Text = webNode.Text;
                node.Tag = webNode;
                node.Expand();
                for (int i = 0; i < webNode.Nodes.Count; i++)
                {
                    node.Nodes.Add(WebNodeToFormNode(webNode.Nodes[i]));
                }
                return node;
            }

            private void PropertyChanged(Object sender, PropertyValueChangedEventArgs e)
            {
                if (e.ChangedItem.Label == "Text")
                    SelectedNode.Text = (String)e.ChangedItem.Value;
                btnApply.Enabled = true;
            }

            /// <summary>
            /// Moves the selected item down one.
            /// </summary>
            /// <param name="sender">Source object</param>
            /// <param name="e">Event arguments</param>
            private void DownButton_click(object sender, EventArgs e) 
            {
                System.Windows.Forms.TreeNodeCollection col;
                Microsoft.Web.UI.WebControls.TreeNodeCollection webCol;
                System.Windows.Forms.TreeNode movingNode = SelectedNode;
                System.Windows.Forms.TreeNode nextnode = movingNode.NextNode;

                if (movingNode.Parent != null)
                {
                    col = movingNode.Parent.Nodes;
                    webCol = ((Microsoft.Web.UI.WebControls.TreeNode)((Microsoft.Web.UI.WebControls.TreeNode)movingNode.Tag).Parent).Nodes;
                }
                else
                {
                    col = tvNodes.Nodes;
                    webCol = tvWebNodes.Nodes;
                }

                ((Microsoft.Web.UI.WebControls.TreeNode)movingNode.Tag).Remove();
                movingNode.Remove();
                col.Insert(nextnode.Index + 1, movingNode);
                webCol.AddAt(movingNode.Index, ((Microsoft.Web.UI.WebControls.TreeNode)movingNode.Tag));

                SelectedNode = movingNode;
                btnApply.Enabled = true;
            }

            /// <summary>
            /// Moves the selected item up one.
            /// </summary>
            /// <param name="sender">Source object</param>
            /// <param name="e">Event arguments</param>
            private void UpButton_click(object sender, EventArgs e) 
            {
                System.Windows.Forms.TreeNodeCollection col;
                Microsoft.Web.UI.WebControls.TreeNodeCollection webCol;
                System.Windows.Forms.TreeNode movingNode = SelectedNode;
                System.Windows.Forms.TreeNode prevnode = movingNode.PrevNode;

                if (movingNode.Parent != null)
                {
                    col = movingNode.Parent.Nodes;
                    webCol = ((Microsoft.Web.UI.WebControls.TreeNode)movingNode.Tag).GetSiblingNodeCollection();
                }
                else
                {
                    col = tvNodes.Nodes;
                    webCol = tvWebNodes.Nodes;
                }
                ((Microsoft.Web.UI.WebControls.TreeNode)(movingNode.Tag)).Remove();
                movingNode.Remove();
                col.Insert(prevnode.Index, movingNode);
                webCol.AddAt(movingNode.Index, ((Microsoft.Web.UI.WebControls.TreeNode)movingNode.Tag));

                SelectedNode = movingNode;
                btnApply.Enabled = true;
            }

            /// <summary>
            /// Moves the selected item one level to the left.
            /// </summary>
            /// <param name="sender">Source object</param>
            /// <param name="e">Event arguments</param>
            private void LeftButton_click(object sender, EventArgs e) 
            {
                System.Windows.Forms.TreeNodeCollection col;
                Microsoft.Web.UI.WebControls.TreeNodeCollection webCol;
                System.Windows.Forms.TreeNode movingNode = SelectedNode;
                System.Windows.Forms.TreeNode parentnode = movingNode.Parent;

                if (parentnode.Parent != null)
                {
                    col = parentnode.Parent.Nodes;
                    webCol = ((Microsoft.Web.UI.WebControls.TreeNode)((Microsoft.Web.UI.WebControls.TreeNode)parentnode.Tag).Parent).Nodes;
                }
                else
                {
                    col = tvNodes.Nodes;
                    webCol = tvWebNodes.Nodes;
                }
                ((Microsoft.Web.UI.WebControls.TreeNode)movingNode.Tag).Remove();
                movingNode.Remove();
                col.Insert(parentnode.Index + 1, movingNode);
                webCol.AddAt(movingNode.Index, ((Microsoft.Web.UI.WebControls.TreeNode)movingNode.Tag));

                SelectedNode = movingNode;
                btnApply.Enabled = true;
            }

            /// <summary>
            /// Moves the selected item one level to the right.
            /// </summary>
            /// <param name="sender">Source object</param>
            /// <param name="e">Event arguments</param>
            private void RightButton_click(object sender, EventArgs e) 
            {
                System.Windows.Forms.TreeNodeCollection col;
                Microsoft.Web.UI.WebControls.TreeNodeCollection webCol;
                System.Windows.Forms.TreeNode movingNode = SelectedNode;

                System.Windows.Forms.TreeNode prevnode = movingNode.PrevNode;
                ((Microsoft.Web.UI.WebControls.TreeNode)movingNode.Tag).Remove();
                col = prevnode.Nodes;
                webCol = ((Microsoft.Web.UI.WebControls.TreeNode)prevnode.Tag).Nodes;

                movingNode.Remove();
                col.Add(movingNode);
                webCol.Add(((Microsoft.Web.UI.WebControls.TreeNode)movingNode.Tag));
                
                SelectedNode = movingNode;
                btnApply.Enabled = true;
            }
        }
    }
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -