同期化されたTreeView編の俺的ライブラリです。
UserControlを継承してSyncTreeViewクラスを作成、そのうえにTreeViewを継承して作成したTreeViewExクラスのオブジェクトを貼り付けています。
1 2 3 4 5 6 7 8 9 10 11 12 13 |
public partial class Form1 : Form { SyncGroup group = new SyncGroup(null); public Form1() { InitializeComponent(); group = new SyncGroup(imageList1); group.SetTreeView(syncTreeView1); group.SetTreeView(syncTreeView2); } } |
あとは基準になるノードを指定して、ラベル名、アイコン、データ(必要ないならnullでOK)を引数にして
1 2 3 4 |
SyncTreeView.AddNodeFirstChild(TreeNodeEx nodeEx, string newText, int imageIndex, int selectedImageIndex, object data); SyncTreeView.AddNodeLastChild(TreeNodeEx nodeEx, string newText, int imageIndex, int selectedImageIndex, object data); SyncTreeView.AddNodeNext(TreeNodeEx nodeEx, string newText, int imageIndex, int selectedImageIndex, object data); SyncTreeView.AddNodePrev(TreeNodeEx nodeEx, string newText, int imageIndex, int selectedImageIndex, object data); |
これで新しいノードを追加できるようになります。
また
1 2 |
SyncTreeView1.MoveToNext(TreeNodeEx dragNodeEx, TreeNodeEx dropNodeEx); SyncTreeView1.MoveToLastChild(TreeNodeEx dragNodeEx, TreeNodeEx dropNodeEx); |
とやれば、ノードの移動もできます。
1 2 |
var nodeEx = syncTreeView1.SelectedNode; nodeEx.Text = "NewText"; |
とやれば、ノードのラベルを変更することもできます。
名前空間名はSyncTreeViewLib、クラスは
SyncTreeView(UserControlを継承)
SyncGroup
TreeViewEx(TreeViewを継承)
TreeNodeEx
があります。
Contents
SyncTreeViewクラス(UserControlを継承)
フィールド変数
1 2 3 4 5 6 7 8 9 10 11 |
public Cursor cursorMoveLast = null; public Cursor cursorMoveFirst = null; public Cursor cursorMoveNext = null; public Cursor cursorMovePrev = null; public Cursor cursorCopyLast = null; public Cursor cursorCopyFirst = null; public Cursor cursorCopyNext = null; public Cursor cursorCopyPrev = null; public SyncGroup SyncGroup = new SyncGroup(null); |
コンストラクタ
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
public SyncTreeView() { InitializeComponent(); Load += SyncTreeView_Load; treeViewEx1.ItemDrag += TreeViewEx1_ItemDrag; treeViewEx1.DragOver += TreeViewEx1_DragOver; treeViewEx1.DragDrop += TreeViewEx1_DragDrop; treeViewEx1.AfterLabelEdit += TreeViewEx1_AfterLabelEdit; treeViewEx1.AfterSelect += TreeViewEx1_AfterSelect; treeViewEx1.BeforeSelect += TreeViewEx1_BeforeSelect; treeViewEx1.KeyDown += (sender, e) => OnKeyDown(e); treeViewEx1.syncTreeView = this; SyncGroup.SetTreeView(this); try { System.Reflection.Assembly asm = System.Reflection.Assembly.GetExecutingAssembly(); cursorMoveLast = new Cursor(asm.GetManifestResourceStream("SyncTreeViewLib.move-last.cur")); cursorMoveFirst = new Cursor(asm.GetManifestResourceStream("SyncTreeViewLib.move-first.cur")); cursorMoveNext = new Cursor(asm.GetManifestResourceStream("SyncTreeViewLib.move-next.cur")); cursorMovePrev = new Cursor(asm.GetManifestResourceStream("SyncTreeViewLib.move-prev.cur")); cursorCopyLast = new Cursor(asm.GetManifestResourceStream("SyncTreeViewLib.copy-last.cur")); cursorCopyFirst = new Cursor(asm.GetManifestResourceStream("SyncTreeViewLib.copy-first.cur")); cursorCopyNext = new Cursor(asm.GetManifestResourceStream("SyncTreeViewLib.copy-next.cur")); cursorCopyPrev = new Cursor(asm.GetManifestResourceStream("SyncTreeViewLib.copy-prev.cur")); } catch { MessageBox.Show("カーソルの読み込みに失敗しました"); } } |
1 2 3 4 |
private void SyncTreeView_Load(object sender, EventArgs e) { treeViewEx1.Dock = DockStyle.Fill; } |
1 2 3 4 5 |
new public bool AllowDrop { set { treeViewEx1.AllowDrop = value; } get { return treeViewEx1.AllowDrop; } } |
1 2 3 4 5 |
public bool CheckBoxes { get { return treeViewEx1.CheckBoxes; } set { treeViewEx1.CheckBoxes = value; } } |
1 2 3 4 5 |
public bool LabelEdit { set { treeViewEx1.LabelEdit = value; } get { return treeViewEx1.LabelEdit; } } |
1 2 3 4 |
public bool IsEmpty { get { return treeViewEx1.Nodes.Count == 0; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
public TreeNodeEx SelectedNode { get { return GetTreeNodeEx(treeViewEx1.SelectedNode); } set { if (value == null) return; TreeNode[] nodes = treeViewEx1.Nodes.Find(value.Key, true); if (nodes.Length == 1) treeViewEx1.SelectedNode = nodes[0]; } } |
1 2 3 4 5 6 7 8 9 10 11 12 |
public TreeNodeEx GetTopLevelTreeNodeEx(int index) { try { TreeNode node = treeViewEx1.Nodes[index]; return this.GetTreeNodeEx(node); } catch { return null; } } |
1 2 3 4 5 6 7 8 9 |
public TreeNodeEx GetTopLevelFirstTreeNodeEx() { int i = treeViewEx1.Nodes.Count; if (i == 0) return null; TreeNode node = treeViewEx1.Nodes[0]; return this.GetTreeNodeEx(node); } |
1 2 3 4 5 6 7 8 9 |
public TreeNodeEx GetTopLevelLastTreeNodeEx() { int i = treeViewEx1.Nodes.Count; if (i == 0) return null; TreeNode node = treeViewEx1.Nodes[i-1]; return this.GetTreeNodeEx(node); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
internal TreeNodeEx GetTreeNodeEx(TreeNode node) { if (node == null) return null; TreeNodeEx ex = new TreeNodeEx(this.SyncGroup); ex._text = node.Text; ex._level = node.Level; ex._key = node.Name; ex._toolTipText = node.ToolTipText; ex._backColorArgb = node.BackColor.ToArgb(); ex._checked = node.Checked; ex._foreColorArgb = node.ForeColor.ToArgb(); ex._imageIndex = node.ImageIndex; ex._isExpanded = node.IsExpanded; ex._selectedImageIndex = node.SelectedImageIndex; ex._data = node.Tag; return ex; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
public TreeNodeEx AddNodeLastChild(TreeNodeEx parentNode, string newText, int imageIndex, int selectedImageIndex, object data) { DateTime dt = DateTime.Now; string newKey = dt.Ticks.ToString(); foreach (TreeViewEx view in SyncGroup.TreeViews) { if (parentNode == null) { view.AddNodeLastChild(null, newKey, newText, imageIndex, selectedImageIndex, data); continue; } TreeNode[] nodes = view.Nodes.Find(parentNode.Key, true); if (nodes.Length > 0) view.AddNodeLastChild(nodes[0], newKey, newText, imageIndex, selectedImageIndex, data); } TreeNode[] nodes1 = treeViewEx1.Nodes.Find(newKey, true); return GetTreeNodeEx(nodes1[0]); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
public TreeNodeEx AddNodeFirstChild(TreeNodeEx parentNode, string newText, int imageIndex, int selectedImageIndex, object data) { DateTime dt = DateTime.Now; string newKey = dt.Ticks.ToString(); foreach (TreeViewEx view in SyncGroup.TreeViews) { if (parentNode == null) { view.AddNodeFirstChild(null, newKey, newText, imageIndex, selectedImageIndex, data); continue; } TreeNode[] nodes = view.Nodes.Find(parentNode.Key, true); if (nodes.Length > 0) view.AddNodeFirstChild(nodes[0], newKey, newText, imageIndex, selectedImageIndex, data); } TreeNode[] nodes1 = treeViewEx1.Nodes.Find(newKey, true); return GetTreeNodeEx(nodes1[0]); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
public TreeNodeEx AddNodeNext(TreeNodeEx targetNode, string newText, int imageIndex, int selectedImageIndex, object data) { if (targetNode == null) return; DateTime dt = DateTime.Now; string newKey = dt.Ticks.ToString(); foreach (TreeViewEx view in SyncGroup.TreeViews) { TreeNode[] nodes = view.Nodes.Find(targetNode.Key, true); if (nodes.Length > 0) view.AddNodeNext(nodes[0], newKey, newText, imageIndex, selectedImageIndex, data); } TreeNode[] nodes1 = treeViewEx1.Nodes.Find(newKey, true); return GetTreeNodeEx(nodes1[0]); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
public TreeNodeEx AddNodePrev(TreeNodeEx targetNode, string newText, int imageIndex, int selectedImageIndex, object data) { if (targetNode == null) return; DateTime dt = DateTime.Now; string newKey = dt.Ticks.ToString(); foreach (TreeViewEx view in SyncGroup.TreeViews) { TreeNode[] nodes = view.Nodes.Find(targetNode.Key, true); if (nodes.Length > 0) view.AddNodePrev(nodes[0], newKey, newText, imageIndex, selectedImageIndex, data); } TreeNode[] nodes1 = treeViewEx1.Nodes.Find(newKey, true); return GetTreeNodeEx(nodes1[0]); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
public void DeleteNode(TreeNodeEx targetNode) { if (targetNode == null) return; foreach (TreeView tree in SyncGroup.TreeViews) { TreeNode[] nodes = tree.Nodes.Find(targetNode.Key, true); if (nodes.Length == 1) { nodes[0].Remove(); } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
public void RestoreNodes(List<TreeNodeEx> nodeExs, TreeNodeEx targetNode, InsertTo insertTo) { List<TreeNode> newNodes = treeViewEx1.RestoreNodes(nodeExs); if (targetNode == null) { foreach (TreeViewEx view in SyncGroup.TreeViews) { view.RestoreTreeView(newNodes, null, insertTo); } } else { if (targetNode._group != null && targetNode._group.TreeViews.Count > 0) { TreeNode[] nodes = targetNode._group.TreeViews[0].Nodes.Find(targetNode.Key, true); if (nodes.Length == 1) { foreach (TreeViewEx view in SyncGroup.TreeViews) { TreeNode[] targets = view.Nodes.Find(targetNode.Key, true); if(targets.Length == 1) view.RestoreTreeView(newNodes, targets[0], insertTo); } } } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
public delegate void BeforeSelectHandler(SyncTreeView sender, BeforeSelectArgs e); public event BeforeSelectHandler BeforeSelect; private void TreeViewEx1_BeforeSelect(object sender, TreeViewCancelEventArgs e) { if (e.Node == null) return; BeforeSelectArgs e1 = new BeforeSelectArgs(e, this); OnBeforeSelect(e1); } protected virtual void OnBeforeSelect(BeforeSelectArgs e) { BeforeSelect?.Invoke(this, e); } |
1 2 3 4 5 6 7 8 9 10 11 12 |
public delegate void AfterSelectHandler(SyncTreeView sender, AfterSelectArgs e); public event AfterSelectHandler AfterSelect; private void TreeViewEx1_AfterSelect(object sender, TreeViewEventArgs e) { AfterSelectArgs e1 = new AfterSelectArgs(e, this); OnAfterSelect(e1); } protected virtual void OnAfterSelect(AfterSelectArgs e) { AfterSelect?.Invoke(this, e); } |
1 2 3 4 5 6 7 8 9 10 11 |
public bool HideSelection { get { return treeViewEx1.HideSelection; } set { treeViewEx1.HideSelection = value; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
public List<TreeNodeEx> GetTreeNodeExs(TreeNodeEx startNode) { if (startNode == null) return treeViewEx1.GetTreeNodeExs(null); else { TreeNode[] nodes = treeViewEx1.Nodes.Find(startNode.Key, true); if (nodes.Length == 1) return treeViewEx1.GetTreeNodeExs(nodes[0]); else return null; foreach (var nodeEx in ret) nodeEx._group = SyncGroup; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
public delegate void AfterLabelEditHandler(SyncTreeView sender, NodeLabelEditEventArgs e); public event AfterLabelEditHandler AfterLabelEdit; private void TreeViewEx1_AfterLabelEdit(object sender, NodeLabelEditEventArgs e) { OnAfterLabelEdit(e); } protected virtual void OnAfterLabelEdit(NodeLabelEditEventArgs e) { AfterLabelEdit?.Invoke(this, e); if (e.Label == null || e.CancelEdit) return; TreeNodeEx nodeEx = GetTreeNodeEx(e.Node); nodeEx.Text = e.Label; } |
1 2 3 4 5 6 7 8 |
protected virtual void OnAfterLabelEdit(NodeLabelEditEventArgs e) { if (e.Label == null || e.CancelEdit) return; TreeNodeEx nodeEx = GetTreeNodeEx(e.Node); nodeEx.Text = e.Label; } |
1 2 3 4 5 6 7 8 9 10 11 |
public delegate void BeforeLabelEditHandler(SyncTreeView sender, NodeLabelEditEventArgs e); public event BeforeLabelEditHandler BeforeLabelEdit; private void TreeViewEx1_BeforeLabelEdit(object sender, NodeLabelEditEventArgs e) { OnBeforeLabelEdit(e); } protected virtual void OnBeforeLabelEdit(NodeLabelEditEventArgs e) { BeforeLabelEdit?.Invoke(this, e); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
public bool CanMove(TreeNodeEx sourceNode, TreeNodeEx targetNode) { if (sourceNode == null || targetNode == null) return false; TreeNode[] sourceNodes = treeViewEx1.Nodes.Find(sourceNode.Key, true); if (sourceNodes.Length != 1) return false; TreeNode[] targetNodes = treeViewEx1.Nodes.Find(targetNode.Key, true); if (targetNodes.Length != 1) return false; return treeViewEx1.CanMove(sourceNodes[0], targetNodes[0]); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
public void MoveToLastChild(TreeNodeEx sourceNode, TreeNodeEx targetNode) { if (!CanMoveToLastChild(sourceNode, targetNode)) return; foreach (TreeViewEx view in treeViewEx1.group.TreeViews) { TreeNode[] sourceNodes = view.Nodes.Find(sourceNode.Key, true); TreeNode[] targetNodes = view.Nodes.Find(targetNode.Key, true); if (sourceNodes.Length == 1 && targetNodes.Length == 1) { view.MoveToLastChild(sourceNodes[0], targetNodes[0]); } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
public bool CanMoveToLastChild(TreeNodeEx sourceNode, TreeNodeEx targetNode) { if (sourceNode == null || targetNode == null) return false; TreeNode[] sourceNodes = treeViewEx1.Nodes.Find(sourceNode.Key, true); if (sourceNodes.Length != 1) return false; TreeNode[] targetNodes = treeViewEx1.Nodes.Find(targetNode.Key, true); if (targetNodes.Length != 1) return false; return treeViewEx1.CanMoveToLastChild(sourceNodes[0], targetNodes[0]); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
public void MoveToFirstChild(TreeNodeEx sourceNode, TreeNodeEx targetNode) { if (!CanMoveToFirstChild(sourceNode, targetNode)) return; foreach (TreeViewEx view in treeViewEx1.group.TreeViews) { TreeNode[] sourceNode0 = view.Nodes.Find(sourceNode.Key, true); TreeNode[] targetNode0 = view.Nodes.Find(targetNode.Key, true); if (sourceNode0.Length == 1 && targetNode0.Length == 1) { view.MoveToFirstChild(sourceNode0[0], targetNode0[0]); } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
public bool CanMoveToFirstChild(TreeNodeEx sourceNode, TreeNodeEx targetNode) { if (sourceNode == null || targetNode == null) return false; TreeNode[] sourceNodes = treeViewEx1.Nodes.Find(sourceNode.Key, true); if (sourceNodes.Length != 1) return false; TreeNode[] targetNodes = treeViewEx1.Nodes.Find(targetNode.Key, true); if (targetNodes.Length != 1) return false; return treeViewEx1.CanMoveToFirstChild(sourceNodes[0], targetNodes[0]); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
public void MoveToNext(TreeNodeEx sourceNode, TreeNodeEx targetNode) { if (!CanMoveToNext(sourceNode, targetNode)) return; foreach (TreeViewEx view in treeViewEx1.group.TreeViews) { TreeNode[] sourceNodes = view.Nodes.Find(sourceNode.Key, true); TreeNode[] targetNodes = view.Nodes.Find(targetNode.Key, true); if (sourceNodes.Length == 1 && targetNodes.Length == 1) { view.MoveToNext(sourceNodes[0], targetNodes[0]); } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
public bool CanMoveToNext(TreeNodeEx sourceNode, TreeNodeEx targetNode) { if (sourceNode == null || targetNode == null) return false; TreeNode[] sourceNodes = treeViewEx1.Nodes.Find(sourceNode.Key, true); if (sourceNodes.Length != 1) return false; TreeNode[] targetNodes = treeViewEx1.Nodes.Find(targetNode.Key, true); if (targetNodes.Length != 1) return false; return treeViewEx1.CanMoveToNext(sourceNodes[0], targetNodes[0]); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
public void MoveToPrev(TreeNodeEx sourceNode, TreeNodeEx targetNode) { if (!CanMoveToPrev(sourceNode, targetNode)) return; foreach (TreeViewEx view in treeViewEx1.group.TreeViews) { TreeNode[] sourceNode0 = view.Nodes.Find(sourceNode.Key, true); TreeNode[] targetNode0 = view.Nodes.Find(targetNode.Key, true); if (sourceNode0.Length == 1 && targetNode0.Length == 1) { view.MoveToPrev(sourceNode0[0], targetNode0[0]); } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
public bool CanMoveToPrev(TreeNodeEx sourceNode, TreeNodeEx targetNode) { if (sourceNode == null || targetNode == null) return false; TreeNode[] sourceNodes = treeViewEx1.Nodes.Find(sourceNode.Key, true); if (sourceNodes.Length != 1) return false; TreeNode[] targetNodes = treeViewEx1.Nodes.Find(targetNode.Key, true); if (targetNodes.Length != 1) return false; return treeViewEx1.CanMoveToPrev(sourceNodes[0], targetNodes[0]); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
public void MoveToNext(TreeNodeEx sourceNode) { if (!CanMoveToNext(sourceNode)) return; foreach (TreeViewEx view in treeViewEx1.group.TreeViews) { TreeNode[] sourceNode0 = view.Nodes.Find(sourceNode.Key, true); if (sourceNode0.Length == 1) { view.MoveToNext(sourceNode0[0]); } } } |
1 2 3 4 5 6 7 8 9 10 11 |
public bool CanMoveToNext(TreeNodeEx sourceNode) { if (sourceNode == null) return false; TreeNode[] sourceNodes = treeViewEx1.Nodes.Find(sourceNode.Key, true); if (sourceNodes.Length != 1) return false; return treeViewEx1.CanMoveToNext(sourceNodes[0]); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
public void MoveToPrev(TreeNodeEx sourceNode) { if (!CanMoveToPrev(sourceNode)) return; foreach (TreeViewEx view in treeViewEx1.group.TreeViews) { TreeNode[] sourceNode0 = view.Nodes.Find(sourceNode.Key, true); if (sourceNode0.Length == 1) { view.MoveToPrev(sourceNode0[0]); } } } |
1 2 3 4 5 6 7 8 9 10 11 |
public bool CanMoveToPrev(TreeNodeEx sourceNode) { if (sourceNode == null) return false; TreeNode[] sourceNodes = treeViewEx1.Nodes.Find(sourceNode.Key, true); if (sourceNodes.Length != 1) return false; return treeViewEx1.CanMoveToPrev(sourceNodes[0]); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
public void LevelUp(TreeNodeEx sourceNode) { if (!CanLevelUp(sourceNode)) return; foreach (TreeViewEx view in treeViewEx1.group.TreeViews) { TreeNode[] sourceNode0 = view.Nodes.Find(sourceNode.Key, true); if (sourceNode0.Length == 1) { view.LevelUp(sourceNode0[0]); } } } |
1 2 3 4 5 6 7 8 9 10 11 |
public bool CanLevelUp(TreeNodeEx sourceNode) { if (sourceNode == null) return false; TreeNode[] sourceNodes = treeViewEx1.Nodes.Find(sourceNode.Key, true); if (sourceNodes.Length != 1) return false; return treeViewEx1.CanLevelUp(sourceNodes[0]); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
public void LevelDown(TreeNodeEx sourceNode) { if (!CanLevelDown(sourceNode)) return; foreach (TreeViewEx view in treeViewEx1.group.TreeViews) { TreeNode[] sourceNode0 = view.Nodes.Find(sourceNode.Key, true); if (sourceNode0.Length == 1) { view.LevelDown(sourceNode0[0]); } } } |
1 2 3 4 5 6 7 8 9 10 11 |
public bool CanLevelDown(TreeNodeEx sourceNode) { if (sourceNode == null) return false; TreeNode[] sourceNodes = treeViewEx1.Nodes.Find(sourceNode.Key, true); if (sourceNodes.Length != 1) return false; return treeViewEx1.CanLevelDown(sourceNodes[0]); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
List<TreeNode> GetTreeNodesForCopy(TreeNodeEx sourceNode, Type[] types) { List<TreeNodeEx> treeNodeExs = GetTreeNodeExs(sourceNode); XmlSerializer xml = null; if (types == null || types.Length == 0) xml = new XmlSerializer(typeof(List<TreeNodeEx>)); else xml = new XmlSerializer(typeof(List<TreeNodeEx>), types); MemoryStream ms = new MemoryStream(); xml.Serialize(ms, treeNodeExs); ms.Position = 0; List<TreeNodeEx> newTreeNodeExs = (List<TreeNodeEx>)xml.Deserialize(ms); return treeViewEx1.RestoreNodes(newTreeNodeExs); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
public void CopyToLastChild(TreeNodeEx sourceNode, TreeNodeEx targetNode, Type[] types) { List<TreeNode> newNodes = GetTreeNodesForCopy(sourceNode, types); if (newNodes == null) return; foreach (TreeViewEx view in treeViewEx1.group.TreeViews) { TreeNode[] targetNodes = view.Nodes.Find(targetNode.Key, true); if (targetNodes.Length == 1) { foreach (TreeNode node in newNodes) targetNodes[0].Nodes.Add((TreeNode)node.Clone()); } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
public void CopyToFirstChild(TreeNodeEx sourceNode, TreeNodeEx targetNode, Type[] types) { List<TreeNode> newNodes = GetTreeNodesForCopy(sourceNode, types); if (newNodes == null) return; foreach (TreeViewEx view in treeViewEx1.group.TreeViews) { TreeNode[] targetNodes = view.Nodes.Find(targetNode.Key, true); if (targetNodes.Length == 1) { foreach (TreeNode node in newNodes) targetNodes[0].Nodes.Insert(0, (TreeNode)node.Clone()); } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
public void CopyToNext(TreeNodeEx sourceNode, TreeNodeEx targetNode, Type[] types) { List<TreeNode> newNodes = GetTreeNodesForCopy(sourceNode, types); if (newNodes == null) return; foreach (TreeViewEx view in treeViewEx1.group.TreeViews) { TreeNode[] targetNodes = view.Nodes.Find(targetNode.Key, true); if (targetNodes.Length == 1) { int index = targetNodes[0].Index; if (targetNodes[0].Parent != null) { foreach (TreeNode node in newNodes) targetNodes[0].Parent.Nodes.Insert(index + 1, (TreeNode)node.Clone()); } else { foreach (TreeNode node in newNodes) view.Nodes.Insert(index + 1, (TreeNode)node.Clone()); } } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
public void CopyToPrev(TreeNodeEx sourceNode, TreeNodeEx targetNode, Type[] types) { List<TreeNode> newNodes = GetTreeNodesForCopy(sourceNode, types); if (newNodes == null) return; foreach (TreeViewEx view in treeViewEx1.group.TreeViews) { TreeNode[] targetNodes = view.Nodes.Find(targetNode.Key, true); if (targetNodes.Length == 1) { int index = targetNodes[0].Index; if (targetNodes[0].Parent != null) { foreach (TreeNode node in newNodes) targetNodes[0].Parent.Nodes.Insert(index, (TreeNode)node.Clone()); } else { foreach (TreeNode node in newNodes) view.Nodes.Insert(index, (TreeNode)node.Clone()); } } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
public void CopyToClipboard(TreeNodeEx sourceNode, Type[] types) { List<TreeNodeEx> treeNodeExs = GetTreeNodeExs(sourceNode); XmlSerializer xml = null; if (types == null || types.Length == 0) xml = new XmlSerializer(typeof(List<TreeNodeEx>)); else xml = new XmlSerializer(typeof(List<TreeNodeEx>), types); MemoryStream ms = new MemoryStream(); xml.Serialize(ms, treeNodeExs); string cf = Application.ExecutablePath; Clipboard.SetData(cf, ms.ToArray()); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
public void PasteFromClipboard(TreeNodeEx targetNode, InsertTo insertTo, Type[] types) { string cf = Application.ExecutablePath; byte[] bytes = (byte[])Clipboard.GetData(cf); MemoryStream ms = new MemoryStream(bytes); XmlSerializer xml = null; if (types == null || types.Length == 0) xml = new XmlSerializer(typeof(List<TreeNodeEx>)); else xml = new XmlSerializer(typeof(List<TreeNodeEx>), types.ToArray()); List<TreeNodeEx> nodeExs = (List<TreeNodeEx>)xml.Deserialize(ms); RestoreNodes(nodeExs, SelectedNode, insertTo); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
private void TreeViewEx1_ItemDrag(object sender, ItemDragEventArgs e) { TreeNodeEx nodeEx = GetTreeNodeEx((TreeNode)e.Item); BeginItemDragArgs args1 = new BeginItemDragArgs(nodeEx); OnBeginItemDrag(args1); if (args1.IsCancel) return; List<TreeNode> oldNodes = new List<TreeNode>(); foreach (var view in treeViewEx1.group.TreeViews) oldNodes.Add(view.SelectedNode); DoDragDrop(nodeEx, DragDropEffects.All); foreach (var node in oldNodes) { if (node != null) node.TreeView.SelectedNode = node; } OnEndItemDrag(new EventArgs()); } |
1 2 3 4 5 6 |
public delegate void BeginItemDragHandler(SyncTreeView sender, BeginItemDragArgs e); public event BeginItemDragHandler BeginItemDrag; protected virtual void OnBeginItemDrag(BeginItemDragArgs e) { BeginItemDrag?.Invoke(this, e); } |
1 2 3 4 5 6 |
public delegate void EndItemDragHandler(SyncTreeView sender, EventArgs e); public event EndItemDragHandler EndItemDrag; protected virtual void OnEndItemDrag(EventArgs e) { EndItemDrag?.Invoke(this, e); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 |
private void TreeViewEx1_DragOver(object sender, System.Windows.Forms.DragEventArgs e) { if (e.Data.GetDataPresent(typeof(TreeNodeEx))) { DragEventExArgs e1 = new DragEventExArgs(e, this); OnDragOverEx(e1); } else { DragEventArgs e1 = new DragEventArgs(e, this); OnDragOver(e1); } } |
1 2 3 4 5 6 |
public delegate void DragOverHandler(SyncTreeView sender, DragEventArgs e); new public event DragOverHandler DragOver; protected virtual void OnDragOver(DragEventArgs e) { DragOver?.Invoke(this, e); } |
1 2 3 4 |
sealed protected override void OnDragOver(System.Windows.Forms.DragEventArgs e) { return; } |
1 2 3 4 5 6 |
public delegate void DragOverExHandler(SyncTreeView sender, DragEventExArgs e); public event DragOverExHandler DragOverEx; protected virtual void OnDragOverEx(DragEventExArgs e) { DragOverEx?.Invoke(this, e); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 |
private void TreeViewEx1_DragDrop(object sender, System.Windows.Forms.DragEventArgs e) { if (e.Data.GetDataPresent(typeof(TreeNodeEx))) { DragEventExArgs e1 = new DragEventExArgs(e, this); OnDragDropEx(e1); } else { DragEventArgs e1 = new DragEventArgs(e, this); OnDragDrop(e1); } } |
1 2 3 4 5 6 |
public delegate void DragDropHandler(SyncTreeView sender, DragEventArgs e); new public event DragDropHandler DragDrop; protected virtual void OnDragDrop(DragEventArgs e) { DragDrop?.Invoke(this, e); } |
1 2 3 4 |
sealed protected override void OnDragDrop(System.Windows.Forms.DragEventArgs e) { return; } |
1 2 3 4 5 6 |
public delegate void DragDropExHandler(SyncTreeView sender, DragEventExArgs e); public event DragDropExHandler DragDropEx; protected virtual void OnDragDropEx(DragEventExArgs e) { DragDropEx?.Invoke(this, e); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
public void SetExpands(TreeNodeEx targetNodeEx, List<bool> isExpands) { List<TreeNode> nodes = treeViewEx1.GetTreeNodes(targetNodeEx); int count = nodes.Count; if(nodes.Count > isExpands.Count) count = isExpands.Count; for (int i = 0; i < count; i++) { if(isExpands[i]) nodes[i].Expand(); else nodes[i].Collapse(); } } |
1 2 3 4 5 6 7 8 9 10 |
public List<bool> GetExpands(TreeNodeEx targetNodeEx) { List<bool> ret = new List<bool>(); List<TreeNode> nodes = treeViewEx1.GetTreeNodes(targetNodeEx); foreach (var node in nodes) ret.Add(node.IsExpanded); return ret; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 |
public void SetChecks(TreeNodeEx targetNodeEx, List<bool> isChecks) { List<TreeNode> nodes = treeViewEx1.GetTreeNodes(targetNodeEx); int count = nodes.Count; if (nodes.Count > isChecks.Count) count = isChecks.Count; for (int i = 0; i < count; i++) { nodes[i].Checked = isChecks[i]; } } |
1 2 3 4 5 6 7 8 9 10 |
public List<bool> GetChecks(TreeNodeEx targetNodeEx) { List<bool> ret = new List<bool>(); List<TreeNode> nodes = treeViewEx1.GetTreeNodes(targetNodeEx); foreach (var node in nodes) ret.Add(node.Checked); return ret; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 |
public void SetImageIndexes(TreeNodeEx targetNodeEx, List<int> imageIndexes) { List<TreeNode> nodes = treeViewEx1.GetTreeNodes(targetNodeEx); int count = nodes.Count; if (nodes.Count > imageIndexes.Count) count = imageIndexes.Count; for (int i = 0; i < count; i++) { nodes[i].ImageIndex = imageIndexes[i]; } } |
1 2 3 4 5 6 7 8 9 10 |
public List<int> GetImageIndexes(TreeNodeEx targetNodeEx) { List<int> ret = new List<int>(); List<TreeNode> nodes = treeViewEx1.GetTreeNodes(targetNodeEx); foreach (var node in nodes) ret.Add(node.ImageIndex); return ret; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 |
public void SetSelectedImageIndexes(TreeNodeEx targetNodeEx, List<int> imageIndexes) { List<TreeNode> nodes = treeViewEx1.GetTreeNodes(targetNodeEx); int count = nodes.Count; if (nodes.Count > imageIndexes.Count) count = imageIndexes.Count; for (int i = 0; i < count; i++) { nodes[i].SelectedImageIndex = imageIndexes[i]; } } |
1 2 3 4 5 6 7 8 9 10 |
public List<int> GetSelectedImageIndexes(TreeNodeEx targetNodeEx) { List<int> ret = new List<int>(); List<TreeNode> nodes = treeViewEx1.GetTreeNodes(targetNodeEx); foreach (var node in nodes) ret.Add(node.SelectedImageIndex); return ret; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
public void SetForeColorArgbs(TreeNodeEx targetNodeEx, List<int> colorArgbs) { List<TreeNode> nodes = treeViewEx1.GetTreeNodes(targetNodeEx); int count = nodes.Count; if (nodes.Count > colorArgbs.Count) count = colorArgbs.Count; for (int i = 0; i < count; i++) { if(colorArgbs[i] == 0) nodes[i].ForeColor = Color.Empty; else nodes[i].ForeColor = Color.FromArgb(colorArgbs[i]); } } |
1 2 3 4 5 6 7 8 9 10 |
public List<int> GetForeColorArgbs(TreeNodeEx targetNodeEx) { List<int> ret = new List<int>(); List<TreeNode> nodes = treeViewEx1.GetTreeNodes(targetNodeEx); foreach (var node in nodes) ret.Add(node.ForeColor.ToArgb()); return ret; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
public void SetBackColorArgbs(TreeNodeEx targetNodeEx, List<int> colorArgbs) { List<TreeNode> nodes = treeViewEx1.GetTreeNodes(targetNodeEx); int count = nodes.Count; if (nodes.Count > colorArgbs.Count) count = colorArgbs.Count; for (int i = 0; i < count; i++) { if (colorArgbs[i] == 0) nodes[i].BackColor = Color.Empty; else nodes[i].BackColor = Color.FromArgb(colorArgbs[i]); } } |
1 2 3 4 5 6 7 8 9 10 |
public List<int> GetBackColorArgbs(TreeNodeEx targetNodeEx) { List<int> ret = new List<int>(); List<TreeNode> nodes = treeViewEx1.GetTreeNodes(targetNodeEx); foreach (var node in nodes) ret.Add(node.BackColor.ToArgb()); return ret; } |
1 2 3 4 |
public int TreeNodeCount() { return treeViewEx1.GetNodeCount(true); } |
SyncGroupクラス
フィールド変数
1 2 |
ImageList _imageList = null; List<TreeViewEx> _treeViews = new List<TreeViewEx>(); |
コンストラクタ
1 2 3 4 |
public SyncGroup(ImageList list) { _imageList = list; } |
1 2 3 4 5 6 7 8 |
internal List<TreeViewEx> TreeViews { get { _treeViews = _treeViews.Where(x => !x.IsDisposed).ToList(); return _treeViews; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
public void SetTreeView(SyncTreeView treeView) { if (!TreeViews.Any(x => x == treeView.treeViewEx1)) { _treeViews.Add(treeView.treeViewEx1); treeView.treeViewEx1.ImageList = _imageList; if (treeView.treeViewEx1.group != null) treeView.treeViewEx1.group.RemoveTreeView(treeView); treeView.treeViewEx1.group = this; treeView.SyncGroup = this; } } |
1 2 3 4 5 6 7 |
public void RemoveTreeView(SyncTreeView treeView) { if (!TreeViews.Any(x => x == treeView.treeViewEx1)) { _treeViews.Remove(treeView.treeViewEx1); } } |
TreeViewExクラス(TreeViewを継承)
フィールド変数
1 2 |
public SyncGroup group = null; internal SyncTreeView syncTreeView = null; |
コンストラクタ
1 2 3 4 |
public TreeViewEx() { this.AllowDrop = true; } |
1 2 3 4 5 6 7 8 9 10 11 |
internal void AddNodeFirstChild(TreeNode parentNode, string newKey, string newText, int imageIndex, int selectedImageIndex, object data) { TreeNode newNode = new TreeNode(newText, imageIndex, selectedImageIndex); newNode.Name = newKey; newNode.Tag = data; if (parentNode != null) parentNode.Nodes.Insert(0, newNode); else this.Nodes.Insert(0, newNode); } |
1 2 3 4 5 6 7 8 9 10 11 |
internal void AddNodeLastChild(TreeNode parentNode, string newKey, string newText, int imageIndex, int selectedImageIndex, object data) { TreeNode newNode = new TreeNode(newText, imageIndex, selectedImageIndex); newNode.Name = newKey; newNode.Tag = data; if (parentNode != null) parentNode.Nodes.Add(newNode); else this.Nodes.Add(newNode); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
internal void AddNodeNext(TreeNode targetNode, string newKey, string newText, int imageIndex, int selectedImageIndex, object data) { if (targetNode == null) return; TreeNode newNode = new TreeNode(newText, imageIndex, selectedImageIndex); newNode.Name = newKey; newNode.Tag = data; TreeNode parentNode = targetNode.Parent; if (parentNode != null) parentNode.Nodes.Insert(targetNode.Index + 1, newNode); else this.Nodes.Insert(targetNode.Index + 1, newNode); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
internal void AddNodePrev(TreeNode targetNode, string newKey, string newText, int imageIndex, int selectedImageIndex, object data) { if (targetNode == null) return; TreeNode newNode = new TreeNode(newText, imageIndex, selectedImageIndex); newNode.Name = newKey; newNode.Tag = data; TreeNode parentNode = targetNode.Parent; if (parentNode != null) parentNode.Nodes.Insert(targetNode.Index, newNode); else this.Nodes.Insert(targetNode.Index, newNode); } |
1 2 3 4 5 6 7 8 9 10 11 12 |
public void MoveToLastChild(TreeNode sourceNode, TreeNode targetNode) { if (CanMoveToLastChild(sourceNode, targetNode)) { TreeNode curNode = SelectedNode; sourceNode.Remove(); targetNode.Nodes.Add(sourceNode); SelectedNode = curNode; } } |
1 2 3 4 5 6 7 8 9 10 |
public bool CanMoveToLastChild(TreeNode sourceNode, TreeNode targetNode) { if (!CanMove(sourceNode, targetNode)) return false; if (targetNode.LastNode == sourceNode) return false; return true; } |
1 2 3 4 5 6 7 8 9 10 11 12 |
public void MoveToFirstChild(TreeNode sourceNode, TreeNode targetNode) { if (CanMove(sourceNode, targetNode)) { TreeNode curNode = SelectedNode; sourceNode.Remove(); targetNode.Nodes.Insert(0, sourceNode); SelectedNode = curNode; } } |
1 2 3 4 5 6 7 8 9 10 |
public bool CanMoveToFirstChild(TreeNode sourceNode, TreeNode targetNode) { if (!CanMove(sourceNode, targetNode)) return false; if (targetNode.FirstNode == sourceNode) return false; return true; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
public void MoveToNext(TreeNode sourceNode, TreeNode targetNode) { if (CanMoveToNext(sourceNode, targetNode)) { TreeNode curNode = SelectedNode; sourceNode.Remove(); int index = targetNode.Index; if (targetNode.Parent != null) targetNode.Parent.Nodes.Insert(index + 1, sourceNode); else Nodes.Insert(index + 1, sourceNode); SelectedNode = curNode; } } |
1 2 3 4 5 6 7 8 9 10 |
public bool CanMoveToNext(TreeNode sourceNode, TreeNode targetNode) { if (!CanMove(sourceNode, targetNode)) return false; if (targetNode.NextNode == sourceNode) return false; return true; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
public void MoveToPrev(TreeNode sourceNode, TreeNode targetNode) { if (CanMoveToPrev(sourceNode, targetNode)) { TreeNode curNode = SelectedNode; sourceNode.Remove(); int index = targetNode.Index; if (targetNode.Parent != null) targetNode.Parent.Nodes.Insert(index, sourceNode); else Nodes.Insert(index, sourceNode); SelectedNode = curNode; } } |
1 2 3 4 5 6 7 8 9 10 |
public bool CanMoveToPrev(TreeNode sourceNode, TreeNode targetNode) { if (!CanMove(sourceNode, targetNode)) return false; if (targetNode.PrevNode == sourceNode) return false; return true; } |
1 2 3 4 5 6 7 |
public void MoveToNext(TreeNode sourceNode) { if (CanMoveToNext(sourceNode)) { MoveToNext(sourceNode, sourceNode.NextNode); } } |
1 2 3 4 5 6 7 |
public bool CanMoveToNext(TreeNode sourceNode) { if (sourceNode.NextNode != null) return true; else return false; } |
1 2 3 4 5 6 7 |
public void MoveToPrev(TreeNode sourceNode) { if (CanMoveToPrev(sourceNode)) { MoveToPrev(sourceNode, sourceNode.PrevNode); } } |
1 2 3 4 5 6 7 |
public bool CanMoveToPrev(TreeNode sourceNode) { if (sourceNode.PrevNode != null) return true; else return false; } |
1 2 3 4 5 6 7 |
public void LevelUp(TreeNode sourceNode) { if (CanLevelUp(sourceNode)) { MoveToNext(sourceNode, sourceNode.Parent); } } |
1 2 3 4 5 6 7 |
public bool CanLevelUp(TreeNode sourceNode) { if (sourceNode.Parent != null) return true; else return false; } |
1 2 3 4 5 6 7 |
public void LevelDown(TreeNode sourceNode) { if (CanLevelDown(sourceNode)) { MoveToLastChild(sourceNode, sourceNode.PrevNode); } } |
1 2 3 4 5 6 7 |
public bool CanLevelDown(TreeNode sourceNode) { if (sourceNode.PrevNode != null) return true; else return false; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
public List<TreeNode> RestoreNodes(List<TreeNodeEx> nodeExs) { if (nodeExs.Count == 0) return null; TreeView view1 = new TreeView(); view1.BeginUpdate(); TreeNode[] vs = new TreeNode[nodeExs.Max(x => x.Level) + 1]; DateTime dt = DateTime.Now; string key0 = dt.Ticks.ToString(); int i = 0; foreach (TreeNodeEx ex in nodeExs) { i++; TreeNode node = new TreeNode(); node.Text = ex.Text; node.ToolTipText = ex.ToolTipText; node.Name = key0 + "-" + i.ToString(); ex.Key = node.Name; if (ex.ForeColorArgb == 0) node.ForeColor = Color.Empty; else node.ForeColor = Color.FromArgb(ex.ForeColorArgb); if (ex.BackColorArgb == 0) node.BackColor = Color.Empty; else node.BackColor = Color.FromArgb(ex.BackColorArgb); node.Checked = ex.Checked; node.ImageIndex = ex.ImageIndex; node.SelectedImageIndex = ex.SelectedImageIndex; node.Tag = ex.Data; if (ex.Level == 0) { view1.Nodes.Add(node); vs[0] = node; } else { vs[ex.Level - 1].Nodes.Add(node); vs[ex.Level] = node; } } List<TreeNode> treeNodes = new List<TreeNode>(); foreach (var node in view1.Nodes) treeNodes.Add((TreeNode)node); view1.Nodes.Clear(); view1.Dispose(); return treeNodes; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
public void RestoreTreeView(List<TreeNode> newNodes, TreeNode targetNode, InsertTo insertTo) { BeginUpdate(); if (targetNode == null) Nodes.Clear(); for (int i = 0; i < newNodes.Count; i++) { TreeNode node = newNodes[i]; if (targetNode == null) { if (insertTo == InsertTo.LastChild) Nodes.Add((TreeNode)node.Clone()); if (insertTo == InsertTo.FirstChild) Nodes.Insert(i, (TreeNode)node.Clone()); } else { TreeNode[] nodes = Nodes.Find(targetNode.Name, true); if (nodes.Length == 1) { if (insertTo == InsertTo.LastChild) nodes[0].Nodes.Add((TreeNode)node.Clone()); else if (insertTo == InsertTo.FirstChild) nodes[0].Nodes.Insert(i, (TreeNode)node.Clone()); else if (insertTo == InsertTo.NextNode) { int index = nodes[0].Index; if (nodes[0].Parent != null) nodes[0].Parent.Nodes.Insert(index + 1 + i, (TreeNode)node.Clone()); else Nodes.Insert(index + 1 + i, (TreeNode)node.Clone()); } else if (insertTo == InsertTo.PrevNode) { int index = nodes[0].Index; if (nodes[0].Parent != null) nodes[0].Parent.Nodes.Insert(index + i, (TreeNode)node.Clone()); else Nodes.Insert(index + i, (TreeNode)node.Clone()); } } } } EndUpdate(); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
internal List<TreeNode> GetTreeNodes(TreeNodeEx startNodeEx) { if (startNodeEx == null) { TreeNode node = null; return GetTreeNodes(node); } TreeNode[] startNodes = Nodes.Find(startNodeEx.Key, true); if (startNodes.Length != 1) return null; return GetTreeNodes(startNodes[0]); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
public List<TreeNode> GetTreeNodes(TreeNode startNode) { List<TreeNode> ret = new List<TreeNode>(); TreeView view = this; if (view.Nodes.Count == 0) return ret; TreeNode curNode = null; TreeNode endNode = null; if (startNode == null) { curNode = view.Nodes[0]; endNode = null; } else { curNode = startNode; endNode = startNode; } ret.Add(curNode); if (startNode != null && startNode.FirstNode == null) return ret; bool b = true; while (true) { if (b && curNode.FirstNode != null) { curNode = curNode.FirstNode; ret.Add(curNode); } else if (curNode.NextNode != null) { curNode = curNode.NextNode; ret.Add(curNode); b = true; } else { curNode = curNode.Parent; b = false; if (curNode == endNode) break; } } return ret; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
public List<TreeNodeEx> GetTreeNodeExs(TreeNode startNode) { List<TreeNodeEx> ret = new List<TreeNodeEx>(); List<TreeNode> nodes = GetTreeNodes(startNode); int startLevel = 0; if (startNode != null) startLevel = startNode.Level; foreach (TreeNode node in nodes) { TreeNodeEx ex = new TreeNodeEx(); ex.Text = node.Text; ex.Level = node.Level - startLevel; ex.Key = node.Name; ex.ToolTipText = node.ToolTipText; ex.BackColorArgb = node.BackColor.ToArgb(); ex.Checked = node.Checked; ex.ForeColorArgb = node.ForeColor.ToArgb(); ex.ImageIndex = node.ImageIndex; ex.IsExpanded = node.IsExpanded; ex.SelectedImageIndex = node.SelectedImageIndex; ex.Data = node.Tag; ret.Add(ex); } return ret; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
internal bool CanMove(TreeNode dragNode, TreeNode dropNode) { if (dragNode == null || dropNode == null) return false; TreeNode node = dropNode; while (true) { if (node == dragNode) return false; if (node == null) return true; node = node.Parent; } } |
TreeNodeExクラス
フィールド変数
1 2 |
[XmlIgnore] internal SyncGroup _group = null; |
コンストラクタ
1 2 3 4 5 6 7 8 |
public TreeNodeEx() { } public TreeNodeEx(SyncGroup group) { _group = group; } |
1 2 3 4 5 6 7 8 9 10 |
public override bool Equals(object obj) { if (obj == null || GetType() != obj.GetType()) return false; if (this.Key == ((TreeNodeEx)obj).Key) return true; return false; } |
1 2 3 4 |
public override int GetHashCode() { return base.GetHashCode(); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
public static bool operator ==(TreeNodeEx a, TreeNodeEx b) { object ao = (object)a; object bo = (object)b; if (ao == null && bo == null) return true; if (ao != null && bo == null) return false; if (ao == null && bo != null) return false; if (a.Key == b.Key) return true; else return false; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
public static bool operator !=(TreeNodeEx a, TreeNodeEx b) { object ao = (object)a; object bo = (object)b; if (ao == null && bo == null) return false; if (ao != null && bo == null) return true; if (ao == null && bo != null) return true; if (a.Key == b.Key) return false; else return true; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
internal string _text = ""; public string Text { get { if (_group != null) { TreeNode[] nodes = _group.TreeViews[0].Nodes.Find(_key, true); if (nodes.Length == 1) { return nodes[0].Text; } } return _text; } set { if (_group == null) _text = value; else { foreach (var view in _group.TreeViews) { TreeNode[] nodes = view.Nodes.Find(_key, true); if (nodes.Length == 1) { nodes[0].Text = value; } } } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
internal string _toolTipText = ""; public string ToolTipText { get { if (_group != null) { TreeNode[] nodes = _group.TreeViews[0].Nodes.Find(_key, true); if (nodes.Length == 1) { return nodes[0].ToolTipText; } } return _toolTipText; } set { if (_group == null) _toolTipText = value; else { foreach (var view in _group.TreeViews) { TreeNode[] nodes = view.Nodes.Find(_key, true); if (nodes.Length == 1) { nodes[0].ToolTipText = value; } } } } } |
1 2 3 4 5 6 7 8 9 10 |
internal string _key = ""; public string Key { get { return _key; } set { if (_group == null) _key = value; } } |
1 2 3 4 5 6 7 8 9 10 |
internal int _level = 0; public int Level { get { return _level; } set { if (_group == null) _level = value; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
internal int _backColorArgb = 0; public int BackColorArgb { get { if (_group != null) { TreeNode[] nodes = _group.TreeViews[0].Nodes.Find(_key, true); if (nodes.Length == 1) { return nodes[0].BackColor.ToArgb(); } } return _backColorArgb; } set { if (_group == null) _backColorArgb = value; else { foreach (var view in _group.TreeViews) { TreeNode[] nodes = view.Nodes.Find(_key, true); if (nodes.Length == 1) { nodes[0].BackColor = Color.FromArgb(value); |