using System; using System.Windows.Forms; namespace RyzStudio.Windows.Forms { public class MovableTreeViewSelectedNode { protected MovableTreeView Treeview = null; public MovableTreeViewSelectedNode(MovableTreeView treeview) { Treeview = treeview; } protected TreeNode SelectedNode { get => this.Treeview.SelectedNode; set => this.Treeview.SelectedNode = value; } protected string encodeName(string name) => Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(name)); public TreeNode AddFolder() { //return this.AddFolder("New Folder " + (new Random()).Next(10001, 99999).ToString()); return this.AddFolder("New Folder (" + DateTime.Now.ToString("yyyy-MM-dd-HHmmss") + ")"); } public TreeNode AddFolder(string name) { if (this.SelectedNode == null) { return null; } if (this.SelectedNode.Tag != null) { return null; } return this.SelectedNode.Nodes.Add(encodeName(name), name, (int)MovableTreeView.IconSet.Folder1, (int)MovableTreeView.IconSet.Folder2); } public TreeNode AddItem(string name, object tag) { if (this.SelectedNode == null) { return null; } if (this.SelectedNode.Tag != null) { return null; } TreeNode tn = new TreeNode(name, (int)MovableTreeView.IconSet.Default, (int)MovableTreeView.IconSet.Default); tn.Tag = tag; this.SelectedNode.Nodes.Add(tn); return tn; } //public TreeNode AddPage() => this.AddPage("New Page (" + DateTime.Now.ToString("yyyy-MM-dd-HHmmss") + ")"); //public TreeNode AddPage(string name) //{ // if (this.SelectedNode == null) // { // return null; // } // if (this.SelectedNode.Tag != null) // { // return null; // } // TreeNode tn = this.SelectedNode.Nodes.Add(encodeName(name), name, (int)MovableTreeView.IconSet.Default, (int)MovableTreeView.IconSet.Default); // tn.Tag = new object(); // tn.ToolTipText = name; // nodeCount++; // NodeCountUpdate(nodeCount); // this.SelectedNode = tn; // OnAddItemNode(tn); // return tn; //} public void Delete() { if (this.SelectedNode == null) { return; } if (this.Treeview.Nodes.Count <= 0) { return; } if (this.SelectedNode.Equals(this.Treeview.Nodes[0])) { return; } this.SelectedNode.Remove(); } public void MoveDown() { if (this.SelectedNode == null) { return; } TreeNode tn = this.SelectedNode; if (tn.Parent == null) { return; } TreeNode tn1 = tn.Parent; if (tn.Index >= (tn1.Nodes.Count - 1)) { return; } int n = tn.Index + 1; tn1.Nodes.Remove(tn); tn1.Nodes.Insert(n, tn); this.SelectedNode = tn; } public void MoveUp() { if (this.SelectedNode == null) { return; } TreeNode tn = this.SelectedNode; if (tn.Parent == null) { return; } if (tn.Index <= 0) { return; } int n = tn.Index - 1; TreeNode tn1 = tn.Parent; tn1.Nodes.Remove(tn); tn1.Nodes.Insert(n, tn); this.SelectedNode = tn; } public void Sort() { if (this.SelectedNode == null) { return; } string[] tnv = new string[0]; TreeNode[] tna = new TreeNode[0]; foreach (TreeNode tn2 in this.SelectedNode.Nodes) { Array.Resize(ref tna, (tna.Length + 1)); tna[(tna.Length - 1)] = tn2; Array.Resize(ref tnv, (tnv.Length + 1)); tnv[(tnv.Length - 1)] = tn2.Text; } Array.Sort(tnv, tna); this.SelectedNode.Nodes.Clear(); foreach (TreeNode tn2 in tna) { this.SelectedNode.Nodes.Add(tn2); } } } }