using Endv.Tools;
using System;
using System.Data;
using System.Drawing;
using System.IO;
using System.Windows.Forms; namespace Endv
{ public class Form1 : System.Windows.Forms.Form
{ private string DBConStr = "";
private string AppPath = "";
private ContextMenu tvSample1Menu = new ContextMenu();
private ContextMenu tvSample2Menu = new ContextMenu();
private System.ComponentModel.IContainer components;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TreeView TreeView1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.TreeView TreeView2;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.Button button4;
private System.Windows.Forms.Button button5;
private System.Windows.Forms.Button button6;
private DataGridView dataGridView1;
private System.Windows.Forms.ImageList imageList1; #region Form Load
private void Form1_Load(object sender, System.EventArgs e)
{ UI.Hourglass(true);// 获取等待光标,通常是沙漏形状 try
{ AppPath = UI.GetAppPath();
DBConStr = SqlHelper.connectionString;
//DBConStr = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + AppPath + "sample.mdb";
//DBConStr = "Provider= Data Source=(local);Initial Catalog=oittest;User ID=sa;Password=123"; tvSample1Menu.MenuItems.Add("Insert",
new EventHandler(tvSample1RightClickInsert)); tvSample1Menu.MenuItems.Add("Edit",
new EventHandler(tvSample1RightClickEdit)); tvSample1Menu.MenuItems.Add("Nudge Up",
new EventHandler(tvSample1RightClickNudgeUp)); tvSample1Menu.MenuItems.Add("Nudge Down",
new EventHandler(tvSample1RightClickNudgeDown)); tvSample1Menu.MenuItems.Add("Delete",
new EventHandler(tvSample1RightClickDelete)); tvSample2Menu.MenuItems.Add("Insert",
new EventHandler(tvSample2RightClickInsert)); tvSample2Menu.MenuItems.Add("Edit",
new EventHandler(tvSample2RightClickEdit)); tvSample2Menu.MenuItems.Add("Nudge Up",
new EventHandler(tvSample2RightClickNudgeUp)); tvSample2Menu.MenuItems.Add("Nudge Down",
new EventHandler(tvSample2RightClickNudgeDown)); tvSample2Menu.MenuItems.Add("Delete",
new EventHandler(tvSample2RightClickDelete)); LoadAllTrees(); TreeView1.AllowDrop = true;
TreeView2.AllowDrop = true; }
catch (Exception err) { UI.Hourglass(false); UI.ShowError(err.Message); }
finally { UI.Hourglass(false); }
}
#endregion #region Load All Trees
private void LoadAllTrees()
{ try
{
LoadTree(TreeView1, Datas.emp.GetHierarchy());
LoadTree(TreeView2, Datas.emp.GetHierarchy( ));
//Datas.emp.GetHierarchy(DBConStr, "A01")
}
catch (Exception) //
{ throw; }
}
#endregion #region Load Tree
private void LoadTree(TreeView tv, DataSet ds)
{ UI.Hourglass(true); try
{ TreeViewUtil.LoadFromDataSet(tv, ds, "Description");//描述 if (tv.Nodes.Count > )
{
tv.Nodes[].Expand();
} }
catch (Exception) { throw; }
finally
{
UI.Hourglass(false);
}
}
#endregion #region TreeView1 右键删除 Right Click Delete
private void tvSample1RightClickDelete(object sender, System.EventArgs e)
{ UI.Hourglass(true); try
{
TreeViewUtil.DeleteNode(TreeView1, true);
}
catch (Exception err) { UI.ShowError(err.Message); }
finally { UI.Hourglass(false); }
}
#endregion #region TreeView2 右键删除 Right Click Delete
private void tvSample2RightClickDelete(object sender, System.EventArgs e)
{ UI.Hourglass(true); try
{
TreeViewUtil.DeleteNode(TreeView2, true);
}
catch (Exception err) { UI.ShowError(err.Message); }
finally { UI.Hourglass(false); }
}
#endregion #region tvSample1 Right Click Edit
private void tvSample1RightClickEdit(object sender, System.EventArgs e)
{ UI.Hourglass(true); try
{ TreeNode node = TreeView1.SelectedNode; if (node == null) { return; } node.TreeView.LabelEdit = true; node.BeginEdit(); }
catch (Exception err) { UI.ShowError(err.Message); }
finally { UI.Hourglass(false); }
}
#endregion #region tvSample2 Right Click Edit
private void tvSample2RightClickEdit(object sender, System.EventArgs e)
{ UI.Hourglass(true); try
{ TreeNode node = TreeView2.SelectedNode; if (node == null) { return; } node.TreeView.LabelEdit = true; node.BeginEdit(); }
catch (Exception err) { UI.ShowError(err.Message); }
finally { UI.Hourglass(false); }
}
#endregion #region tvSample1 Right Click Nudge Up
private void tvSample1RightClickNudgeUp(object sender, System.EventArgs e)
{ UI.Hourglass(true); try
{
TreeViewUtil.NudgeUp(TreeView1.SelectedNode);
}
catch (Exception err) { UI.ShowError(err.Message); }
finally { UI.Hourglass(false); }
}
#endregion #region tvSample1 Right Click Nudge Down
private void tvSample1RightClickNudgeDown(object sender, System.EventArgs e)
{ UI.Hourglass(true); try
{
TreeViewUtil.NudgeDown(TreeView1.SelectedNode);
}
catch (Exception err) { UI.ShowError(err.Message); }
finally { UI.Hourglass(false); }
}
#endregion #region tvSample2 Right Click Nudge Up
private void tvSample2RightClickNudgeUp(object sender, System.EventArgs e)
{ UI.Hourglass(true); try
{
TreeViewUtil.NudgeUp(TreeView2.SelectedNode);
}
catch (Exception err) { UI.ShowError(err.Message); }
finally { UI.Hourglass(false); }
}
#endregion #region tvSample2 Right Click Nudge Down
private void tvSample2RightClickNudgeDown(object sender, System.EventArgs e)
{ UI.Hourglass(true); try
{
TreeViewUtil.NudgeDown(TreeView2.SelectedNode);
}
catch (Exception err) { UI.ShowError(err.Message); }
finally { UI.Hourglass(false); }
}
#endregion #region tvSample1 Right Click Insert
private void tvSample1RightClickInsert(object sender, System.EventArgs e)
{ UI.Hourglass(true); try
{ TreeNode node = TreeView1.SelectedNode; if (node == null) { return; } InsertNewNode(node); }
catch (Exception err) { UI.ShowError(err.Message); }
finally { UI.Hourglass(false); }
}
#endregion #region tvSample2 Right Click Insert
private void tvSample2RightClickInsert(object sender, System.EventArgs e)
{ UI.Hourglass(true); try
{ TreeNode node = TreeView2.SelectedNode; if (node == null) { return; } InsertNewNode(node); }
catch (Exception err) { UI.ShowError(err.Message); }
finally { UI.Hourglass(false); }
}
#endregion #region Insert New Node
private void InsertNewNode(TreeNode node)
{ DataRow row = null;
DataRow ParentRow = null;
DataTable dt = null;
int newindex = ; try
{ ParentRow = (DataRow)node.Tag; if (ParentRow == null) { return; } newindex = int.Parse(ParentRow["SortOrder"].ToString()) + ; dt = ParentRow.Table; row = dt.NewRow(); row["ObjectID"] = Guid.NewGuid().ToString();
row["ObjectTypeID"] = ;
row["ModelID"] = int.Parse(ParentRow["ModelID"].ToString());
row["NodeID"] = Guid.NewGuid().ToString();
row["ParentNodeID"] = ParentRow[dt.PrimaryKey[].ColumnName].ToString();
row["Description"] = "New Node";
row["ForeColor"] = "#000000";
row["BackColor"] = "#FFFFFF";
row["ImageIndex"] = ;
row["SelectedImageIndex"] = ;
row["Checked"] = true;
row["ActiveID"] = ;
row["NamedRange"] = "";
row["NodeValue"] = "";
row["LastUpdateTime"] = DateTime.Now;
row["SortOrder"] = newindex; dt.Rows.Add(row); node.Nodes.Add(TreeViewUtil.GetTreeNodeFromDataRow(row, "Description")); }
catch (Exception)
{
throw;
} }
#endregion #region Edit Node
private void EditNode(TreeNode node, string newText)
{
DataRow row = null; try
{ if (node == null) { return; } row = (DataRow)node.Tag; if (row == null) { return; } row["Description"] = newText; }
catch (Exception) { throw; } }
#endregion #region 重载测试数据 Button Reload Test Data
private void button1_Click(object sender, System.EventArgs e)
{
LoadAllTrees();
}
#endregion #region 导出树 Button Export Trees To Xml
private void button2_Click(object sender, System.EventArgs e)
{ string filename = "";
DataSet ds;
DataRow row;
DataSet compareds; try
{ UI.Hourglass(true); // Write out the contents of tvSample to disk filename = Path.Combine(AppPath, "treeview1.xml"); if (File.Exists(filename)) { File.Delete(filename); } if (TreeView1.Nodes.Count == ) { return; } row = (DataRow)TreeView1.Nodes[].Tag; ds = row.Table.DataSet; compareds = ds.GetChanges(); if (compareds != null)
{
compareds.WriteXml(filename, XmlWriteMode.DiffGram);
} // Write out the contents of tvSample2 to disk filename = Path.Combine(AppPath, "treeview2.xml"); if (File.Exists(filename)) { File.Delete(filename); } if (TreeView2.Nodes.Count == ) { return; } row = (DataRow)TreeView2.Nodes[].Tag; ds = row.Table.DataSet; compareds = ds.GetChanges(); if (compareds != null)
{
compareds.WriteXml(filename, XmlWriteMode.DiffGram);
} }
catch (Exception err) { UI.ShowError(err.Message); }
finally { UI.Hourglass(false); }
}
#endregion #region tvSample Mouse Down
private void tvSample_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{ TreeViewUtil.SetSelectedNodeByPosition(TreeView1, e.X, e.Y); if (TreeView1.SelectedNode == null) { return; } if (e.Button == MouseButtons.Right) { return; } }
#endregion #region tvSample MouseUp
private void tvSample_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{ switch (e.Button)
{
case MouseButtons.Right: tvSample1Menu.Show(TreeView1, new Point(e.X, e.Y));
return; default:
break;
} }
#endregion #region tvSample2 Mouse Down
private void tvSample2_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{ TreeViewUtil.SetSelectedNodeByPosition(TreeView2, e.X, e.Y); if (TreeView2.SelectedNode == null) { return; } if (e.Button == MouseButtons.Right) { return; } }
#endregion #region tvSample2 MouseUp
private void tvSample2_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{ switch (e.Button)
{
case MouseButtons.Right: tvSample2Menu.Show(TreeView2, new Point(e.X, e.Y));
break; default:
break;
}
}
#endregion #region tvSample Drag And Drop Events
private void tvSample_ItemDrag(object sender, System.Windows.Forms.ItemDragEventArgs e)
{
DoDragDrop(e.Item, DragDropEffects.Move);
} private void tvSample_DragEnter(object sender, System.Windows.Forms.DragEventArgs e)
{
TreeViewUtil.DragEnter((TreeView)sender, e);
} private void tvSample_DragOver(object sender, System.Windows.Forms.DragEventArgs e)
{
TreeViewUtil.DragOver((TreeView)sender, e);
} private void tvSample_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
{
DataRow row;
bool dropOnNewControl = false; try
{ UI.Hourglass(true); TreeViewUtil.DragDrop((TreeView)sender, e, ref dropOnNewControl); if (dropOnNewControl)
{
row = (DataRow)TreeView2.Nodes[].Tag;
Datas.emp.CommitHierarchy(DBConStr, row.Table.DataSet);
row = (DataRow)TreeView1.Nodes[].Tag;
Datas.emp.CommitHierarchy(DBConStr, row.Table.DataSet);
} // this.LoadAllTrees(); UI.Hourglass(false);
}
catch (Exception err) { UI.ShowError(err.Message); }
finally { UI.Hourglass(false); }
}
#endregion #region tvSample2 Drag And Drop Events
private void tvSample2_ItemDrag(object sender, System.Windows.Forms.ItemDragEventArgs e)
{
DoDragDrop(e.Item, DragDropEffects.Move);
} private void tvSample2_DragEnter(object sender, System.Windows.Forms.DragEventArgs e)
{
TreeViewUtil.DragEnter((TreeView)sender, e);
} private void tvSample2_DragOver(object sender, System.Windows.Forms.DragEventArgs e)
{
TreeViewUtil.DragOver((TreeView)sender, e);
} private void tvSample2_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
{
DataRow row;
bool dropOnNewControl = false; try
{ UI.Hourglass(true); TreeViewUtil.DragDrop((TreeView)sender, e, ref dropOnNewControl); if (dropOnNewControl)
{
row = (DataRow)TreeView1.Nodes[].Tag;
Datas.emp.CommitHierarchy(DBConStr, row.Table.DataSet);
row = (DataRow)TreeView2.Nodes[].Tag;
Datas.emp.CommitHierarchy(DBConStr, row.Table.DataSet);
} UI.Hourglass(false);
}
catch (Exception err) { UI.ShowError(err.Message); }
finally { UI.Hourglass(false); }
}
#endregion #region tvSample1 After Label Edit
private void tvSample_AfterLabelEdit(object sender, System.Windows.Forms.NodeLabelEditEventArgs e)
{
try
{
if (e.Label.Trim().Length < ) { e.CancelEdit = true; }
EditNode(TreeView1.SelectedNode, e.Label);
TreeView1.SelectedNode.EndEdit(false);
TreeView1.LabelEdit = false;
}
catch (Exception err) { UI.ShowError(err.Message); }
}
#endregion #region tvSample2 After Label Edit
private void tvSample2_AfterLabelEdit(object sender, System.Windows.Forms.NodeLabelEditEventArgs e)
{
try
{
if (e.Label.Trim().Length < ) { e.CancelEdit = true; }
EditNode(TreeView2.SelectedNode, e.Label);
TreeView2.SelectedNode.EndEdit(false);
TreeView2.LabelEdit = false;
}
catch (Exception err) { UI.ShowError(err.Message); }
}
#endregion #region tvSample1 Accept Changes
private void button3_Click(object sender, System.EventArgs e)
{ DataRow row = null;
UI.Hourglass(true); try
{ if (TreeView1.Nodes.Count == ) { return; } row = (DataRow)TreeView1.Nodes[].Tag; Datas.emp.CommitHierarchy(DBConStr, row.Table.DataSet); }
catch (Exception err) { UI.ShowError(err.Message); }
finally { UI.Hourglass(false); } }
#endregion #region tvSample1 拒绝更改 Reject Changes
private void button4_Click(object sender, System.EventArgs e)
{ DataRow row = null;
UI.Hourglass(true); try
{ if (TreeView1.Nodes.Count < ) { return; } row = (DataRow)TreeView1.Nodes[].Tag; row.Table.DataSet.RejectChanges(); LoadTree(TreeView1, row.Table.DataSet); }
catch (Exception err) { UI.ShowError(err.Message); }
finally { UI.Hourglass(false); }
}
#endregion #region tvSample2 接受变化 Accept Changes
private void button6_Click(object sender, System.EventArgs e)
{ DataRow row = null;
UI.Hourglass(true); try
{ if (TreeView2.Nodes.Count == ) { return; } row = (DataRow)TreeView2.Nodes[].Tag; Datas.emp.CommitHierarchy(DBConStr, row.Table.DataSet); }
catch (Exception err) { UI.ShowError(err.Message); }
finally { UI.Hourglass(false); }
}
#endregion #region tvSample2 Reject Changes
private void button5_Click(object sender, System.EventArgs e)
{ DataRow row = null;
UI.Hourglass(true); try
{ if (TreeView2.Nodes.Count < ) { return; } row = (DataRow)TreeView2.Nodes[].Tag; row.Table.DataSet.RejectChanges(); LoadTree(TreeView2, row.Table.DataSet); }
catch (Exception err) { UI.ShowError(err.Message); }
finally { UI.Hourglass(false); }
}
#endregion #region Form Closed
private void Form1_Closed(object sender, System.EventArgs e)
{ }
#endregion #region Exit
private void cmdExit_Click(object sender, System.EventArgs e)
{
this.Close();
Application.Exit();
}
#endregion #region Constructor [STAThread]
static void Main()
{
Application.Run(new Form1());
} public Form1()
{
InitializeComponent();
} protected override void Dispose(bool disposing)
{
if (disposing)
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose(disposing);
}
#endregion #region Windows Form Designer generated code
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.label1 = new System.Windows.Forms.Label();
this.TreeView1 = new System.Windows.Forms.TreeView();
this.imageList1 = new System.Windows.Forms.ImageList(this.components);
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.TreeView2 = new System.Windows.Forms.TreeView();
this.label2 = new System.Windows.Forms.Label();
this.button3 = new System.Windows.Forms.Button();
this.button4 = new System.Windows.Forms.Button();
this.button5 = new System.Windows.Forms.Button();
this.button6 = new System.Windows.Forms.Button();
this.dataGridView1 = new System.Windows.Forms.DataGridView();
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
this.SuspendLayout();
//
// label1
//
this.label1.Location = new System.Drawing.Point(, );
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(, );
this.label1.TabIndex = ;
this.label1.Text = "Tree 1";
//
// TreeView1
//
this.TreeView1.AllowDrop = true;
this.TreeView1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.TreeView1.HideSelection = false;
this.TreeView1.ImageIndex = ;
this.TreeView1.ImageList = this.imageList1;
this.TreeView1.Location = new System.Drawing.Point(, );
this.TreeView1.Name = "TreeView1";
this.TreeView1.SelectedImageIndex = ;
this.TreeView1.Size = new System.Drawing.Size(, );
this.TreeView1.TabIndex = ;
this.TreeView1.AfterLabelEdit += new System.Windows.Forms.NodeLabelEditEventHandler(this.tvSample_AfterLabelEdit);
this.TreeView1.ItemDrag += new System.Windows.Forms.ItemDragEventHandler(this.tvSample_ItemDrag);
this.TreeView1.DragDrop += new System.Windows.Forms.DragEventHandler(this.tvSample_DragDrop);
this.TreeView1.DragEnter += new System.Windows.Forms.DragEventHandler(this.tvSample_DragEnter);
this.TreeView1.DragOver += new System.Windows.Forms.DragEventHandler(this.tvSample_DragOver);
this.TreeView1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.tvSample_MouseDown);
this.TreeView1.MouseUp += new System.Windows.Forms.MouseEventHandler(this.tvSample_MouseUp);
//
// imageList1
//
this.imageList1.ColorDepth = System.Windows.Forms.ColorDepth.Depth8Bit;
this.imageList1.ImageSize = new System.Drawing.Size(, );
this.imageList1.TransparentColor = System.Drawing.Color.Transparent;
//
// button1
//
this.button1.Location = new System.Drawing.Point(, );
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(, );
this.button1.TabIndex = ;
this.button1.Text = "Reload Test Data";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(, );
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(, );
this.button2.TabIndex = ;
this.button2.Text = "Save Xml To Root Folder";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// TreeView2
//
this.TreeView2.AllowDrop = true;
this.TreeView2.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.TreeView2.ImageIndex = ;
this.TreeView2.ImageList = this.imageList1;
this.TreeView2.Location = new System.Drawing.Point(, );
this.TreeView2.Name = "TreeView2";
this.TreeView2.SelectedImageIndex = ;
this.TreeView2.Size = new System.Drawing.Size(, );
this.TreeView2.TabIndex = ;
this.TreeView2.AfterLabelEdit += new System.Windows.Forms.NodeLabelEditEventHandler(this.tvSample2_AfterLabelEdit);
this.TreeView2.ItemDrag += new System.Windows.Forms.ItemDragEventHandler(this.tvSample2_ItemDrag);
this.TreeView2.DragDrop += new System.Windows.Forms.DragEventHandler(this.tvSample2_DragDrop);
this.TreeView2.DragEnter += new System.Windows.Forms.DragEventHandler(this.tvSample2_DragEnter);
this.TreeView2.DragOver += new System.Windows.Forms.DragEventHandler(this.tvSample2_DragOver);
this.TreeView2.MouseDown += new System.Windows.Forms.MouseEventHandler(this.tvSample2_MouseDown);
this.TreeView2.MouseUp += new System.Windows.Forms.MouseEventHandler(this.tvSample2_MouseUp);
//
// label2
//
this.label2.Location = new System.Drawing.Point(, );
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(, );
this.label2.TabIndex = ;
this.label2.Text = "Tree 2";
//
// button3
//
this.button3.Location = new System.Drawing.Point(, );
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(, );
this.button3.TabIndex = ;
this.button3.Text = "Accept Changes";
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// button4
//
this.button4.Location = new System.Drawing.Point(, );
this.button4.Name = "button4";
this.button4.Size = new System.Drawing.Size(, );
this.button4.TabIndex = ;
this.button4.Text = "Reject Changes";
this.button4.Click += new System.EventHandler(this.button4_Click);
//
// button5
//
this.button5.Location = new System.Drawing.Point(, );
this.button5.Name = "button5";
this.button5.Size = new System.Drawing.Size(, );
this.button5.TabIndex = ;
this.button5.Text = "Reject Changes";
this.button5.Click += new System.EventHandler(this.button5_Click);
//
// button6
//
this.button6.Location = new System.Drawing.Point(, );
this.button6.Name = "button6";
this.button6.Size = new System.Drawing.Size(, );
this.button6.TabIndex = ;
this.button6.Text = "Accept Changes";
this.button6.Click += new System.EventHandler(this.button6_Click);
//
// dataGridView1
//
this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.dataGridView1.Location = new System.Drawing.Point(, );
this.dataGridView1.Name = "dataGridView1";
this.dataGridView1.RowTemplate.Height = ;
this.dataGridView1.Size = new System.Drawing.Size(, );
this.dataGridView1.TabIndex = ;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(, );
this.ClientSize = new System.Drawing.Size(, );
this.Controls.Add(this.dataGridView1);
this.Controls.Add(this.button5);
this.Controls.Add(this.button6);
this.Controls.Add(this.button4);
this.Controls.Add(this.button3);
this.Controls.Add(this.label2);
this.Controls.Add(this.TreeView2);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.TreeView1);
this.Controls.Add(this.label1);
this.MaximizeBox = false;
this.Name = "Form1";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Tree Sample";
this.Closed += new System.EventHandler(this.Form1_Closed);
this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
this.ResumeLayout(false); }
#endregion }
}