//
// imageList1
//
this.imageList1.ColorDepth = System.Windows.Forms.ColorDepth.Depth8Bit;
this.imageList1.ImageSize = new System.Drawing.Size(16, 16);
this.imageList1.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageList1.ImageStream")));
this.imageList1.TransparentColor = System.Drawing.Color.Transparent;
//
// cn
//
this.cn.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Password="""";User ID=Admin;Data Source=db1.mdb;Mode=Share Deny None;Extended Properties="""";Jet OLEDB:System database="""";Jet OLEDB:Registry Path="""";Jet OLEDB:Database Password="""";Jet OLEDB:Engine Type=5;Jet OLEDB:Database Locking Mode=1;Jet OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Global Bulk Transactions=1;Jet OLEDB:New Database Password="""";Jet OLEDB:Create System Database=False;Jet OLEDB:Encrypt Database=False;Jet OLEDB:Don't Copy Locale on Compact=False;Jet OLEDB:Compact Without Replica Repair=False;Jet OLEDB:SFP=False";
//
// button1
//
this.button1.Location = new System.Drawing.Point(64, 256);
this.button1.Name = "button1";
this.button1.TabIndex = 2;
this.button1.Text = "刷新控件";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// panel1
//
this.panel1.Controls.AddRange(new System.Windows.Forms.Control[] {
this.button3,
this.button2,
this.label3,
this.label2,
this.listView1,
this.label1,
this.textBox1});
this.panel1.Location = new System.Drawing.Point(224, 24);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(152, 208);
this.panel1.TabIndex = 3;
this.panel1.Visible = false;
//
// button2
//
this.button2.Location = new System.Drawing.Point(24, 176);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(48, 24);
this.button2.TabIndex = 9;
this.button2.Click += new System.EventHandler(this.button2_Click_1);
//
// label3
//
this.label3.Location = new System.Drawing.Point(40, 72);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(64, 16);
this.label3.TabIndex = 8;
this.label3.Text = "选择图标";
//
// label2
//
this.label2.Location = new System.Drawing.Point(40, 16);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(80, 16);
this.label2.TabIndex = 7;
this.label2.Text = "部门名称";
//
// listView1
//
this.listView1.BorderStyle = System.Windows.Forms.BorderStyle.None;
this.listView1.GridLines = true;
this.listView1.Location = new System.Drawing.Point(48, 120);
this.listView1.MultiSelect = false;
this.listView1.Name = "listView1";
this.listView1.Size = new System.Drawing.Size(56, 48);
this.listView1.SmallImageList = this.imageList1;
this.listView1.TabIndex = 6;
this.listView1.View = System.Windows.Forms.View.SmallIcon;
this.listView1.Visible = false;
this.listView1.Click += new System.EventHandler(this.listView1_Click);
//
// label1
//
this.label1.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
this.label1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.label1.ImageList = this.imageList1;
this.label1.Location = new System.Drawing.Point(48, 96);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(32, 26);
this.label1.TabIndex = 1;
this.label1.TextAlign = System.Drawing.ContentAlignment.TopCenter;
this.label1.Click += new System.EventHandler(this.label1_Click);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(8, 37);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(136, 21);
this.textBox1.TabIndex = 0;
this.textBox1.Text = "";
// imageList1
//
this.imageList1.ColorDepth = System.Windows.Forms.ColorDepth.Depth8Bit;
this.imageList1.ImageSize = new System.Drawing.Size(16, 16);
this.imageList1.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageList1.ImageStream")));
this.imageList1.TransparentColor = System.Drawing.Color.Transparent;
//
// cn
//
this.cn.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Password="""";User ID=Admin;Data Source=db1.mdb;Mode=Share Deny None;Extended Properties="""";Jet OLEDB:System database="""";Jet OLEDB:Registry Path="""";Jet OLEDB:Database Password="""";Jet OLEDB:Engine Type=5;Jet OLEDB:Database Locking Mode=1;Jet OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Global Bulk Transactions=1;Jet OLEDB:New Database Password="""";Jet OLEDB:Create System Database=False;Jet OLEDB:Encrypt Database=False;Jet OLEDB:Don't Copy Locale on Compact=False;Jet OLEDB:Compact Without Replica Repair=False;Jet OLEDB:SFP=False";
//
// button1
//
this.button1.Location = new System.Drawing.Point(64, 256);
this.button1.Name = "button1";
this.button1.TabIndex = 2;
this.button1.Text = "刷新控件";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// panel1
//
this.panel1.Controls.AddRange(new System.Windows.Forms.Control[] {
this.button3,
this.button2,
this.label3,
this.label2,
this.listView1,
this.label1,
this.textBox1});
this.panel1.Location = new System.Drawing.Point(224, 24);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(152, 208);
this.panel1.TabIndex = 3;
this.panel1.Visible = false;
//
// button2
//
this.button2.Location = new System.Drawing.Point(24, 176);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(48, 24);
this.button2.TabIndex = 9;
this.button2.Click += new System.EventHandler(this.button2_Click_1);
//
// label3
//
this.label3.Location = new System.Drawing.Point(40, 72);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(64, 16);
this.label3.TabIndex = 8;
this.label3.Text = "选择图标";
//
// label2
//
this.label2.Location = new System.Drawing.Point(40, 16);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(80, 16);
this.label2.TabIndex = 7;
this.label2.Text = "部门名称";
//
// listView1
//
this.listView1.BorderStyle = System.Windows.Forms.BorderStyle.None;
this.listView1.GridLines = true;
this.listView1.Location = new System.Drawing.Point(48, 120);
this.listView1.MultiSelect = false;
this.listView1.Name = "listView1";
this.listView1.Size = new System.Drawing.Size(56, 48);
this.listView1.SmallImageList = this.imageList1;
this.listView1.TabIndex = 6;
this.listView1.View = System.Windows.Forms.View.SmallIcon;
this.listView1.Visible = false;
this.listView1.Click += new System.EventHandler(this.listView1_Click);
//
// label1
//
this.label1.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
this.label1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.label1.ImageList = this.imageList1;
this.label1.Location = new System.Drawing.Point(48, 96);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(32, 26);
this.label1.TabIndex = 1;
this.label1.TextAlign = System.Drawing.ContentAlignment.TopCenter;
this.label1.Click += new System.EventHandler(this.label1_Click);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(8, 37);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(136, 21);
this.textBox1.TabIndex = 0;
this.textBox1.Text = "";
// contextMenu1
//
this.contextMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem1,
this.menuItem3,
this.menuItem5,
this.menuItem4,
this.menuItem2});
//
// menuItem1
//
this.menuItem1.Index = 0;
this.menuItem1.Text = "添加";
this.menuItem1.Click += new System.EventHandler(this.menuItem1_Click);
//
// menuItem3
//
this.menuItem3.Index = 1;
this.menuItem3.Text = "-";
//
// menuItem5
//
this.menuItem5.Index = 2;
this.menuItem5.Text = "修改";
this.menuItem5.Click += new System.EventHandler(this.menuItem5_Click);
//
// menuItem4
//
this.menuItem4.Index = 3;
this.menuItem4.Text = "-";
//
// menuItem2
//
this.menuItem2.Index = 4;
this.menuItem2.Text = "删除";
this.menuItem2.Click += new System.EventHandler(this.menuItem2_Click);
//
// button3
//
this.button3.Location = new System.Drawing.Point(88, 176);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(48, 24);
this.button3.TabIndex = 10;
this.button3.Text = "取消";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(408, 293);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.panel1,
this.button1,
this.treeView1});
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.panel1.ResumeLayout(false);
this.ResumeLayout(false); }
#endregion /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
} private void button1_Click(object sender, System.EventArgs e)
{
this.treeView1.Nodes.Clear();
string str="";
treenode(0,str);
}
private void treenode(int i,string str)
{
OleDbDataAdapter ap1=new OleDbDataAdapter("select name,id,image from t1 where upid="+i,cn);
DataSet ds1=new DataSet();
ap1.Fill(ds1,"t1");
int j=ds1.Tables["t1"].Rows.Count;
if (j<=0)
return;
for(int f=0;f<j;f++)
{
string path=str;
string name=ds1.Tables["t1"].Rows[f][0].ToString();
int id=int.Parse(ds1.Tables["t1"].Rows[f][1].ToString());
int imageid=int.Parse(ds1.Tables["t1"].Rows[f][2].ToString());
TreeNode n1=new TreeNode(name,imageid,imageid);
n1.Tag=id;
if(treeView1.Nodes.Count<1)
this.treeView1.Nodes.Add(n1);
else
{
foreach(TreeNode node in treeView1.Nodes)
{
TreeNode re=new TreeNode();
re=nodetest(node,path);
if(re.Text!="none")
{
re.Nodes.Add(n1);
break;
}
}
}
if(path.Length>0)
path+="\\"+name;
else
path=name;
treenode(id,path);
}
}
// contextMenu1
//
this.contextMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem1,
this.menuItem3,
this.menuItem5,
this.menuItem4,
this.menuItem2});
//
// menuItem1
//
this.menuItem1.Index = 0;
this.menuItem1.Text = "添加";
this.menuItem1.Click += new System.EventHandler(this.menuItem1_Click);
//
// menuItem3
//
this.menuItem3.Index = 1;
this.menuItem3.Text = "-";
//
// menuItem5
//
this.menuItem5.Index = 2;
this.menuItem5.Text = "修改";
this.menuItem5.Click += new System.EventHandler(this.menuItem5_Click);
//
// menuItem4
//
this.menuItem4.Index = 3;
this.menuItem4.Text = "-";
//
// menuItem2
//
this.menuItem2.Index = 4;
this.menuItem2.Text = "删除";
this.menuItem2.Click += new System.EventHandler(this.menuItem2_Click);
//
// button3
//
this.button3.Location = new System.Drawing.Point(88, 176);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(48, 24);
this.button3.TabIndex = 10;
this.button3.Text = "取消";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(408, 293);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.panel1,
this.button1,
this.treeView1});
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.panel1.ResumeLayout(false);
this.ResumeLayout(false); }
#endregion /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
} private void button1_Click(object sender, System.EventArgs e)
{
this.treeView1.Nodes.Clear();
string str="";
treenode(0,str);
}
private void treenode(int i,string str)
{
OleDbDataAdapter ap1=new OleDbDataAdapter("select name,id,image from t1 where upid="+i,cn);
DataSet ds1=new DataSet();
ap1.Fill(ds1,"t1");
int j=ds1.Tables["t1"].Rows.Count;
if (j<=0)
return;
for(int f=0;f<j;f++)
{
string path=str;
string name=ds1.Tables["t1"].Rows[f][0].ToString();
int id=int.Parse(ds1.Tables["t1"].Rows[f][1].ToString());
int imageid=int.Parse(ds1.Tables["t1"].Rows[f][2].ToString());
TreeNode n1=new TreeNode(name,imageid,imageid);
n1.Tag=id;
if(treeView1.Nodes.Count<1)
this.treeView1.Nodes.Add(n1);
else
{
foreach(TreeNode node in treeView1.Nodes)
{
TreeNode re=new TreeNode();
re=nodetest(node,path);
if(re.Text!="none")
{
re.Nodes.Add(n1);
break;
}
}
}
if(path.Length>0)
path+="\\"+name;
else
path=name;
treenode(id,path);
}
}
{
TreeNode node1=new TreeNode("none");
if(node.FullPath==path)
return node;
if(node.Nodes.Count>0)
{
foreach(TreeNode nodechild in node.Nodes)
{
if (nodechild.FullPath==path)
{
return nodechild;
}
node1=nodetest(nodechild,path);
if(node1.Text!="none")
return node1;
}
}
return node1;
} private void Form1_Load(object sender, System.EventArgs e)
{
} private void treeView1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
if(this.treeView1.Nodes.Count>0)
{
if(e.Button==MouseButtons.Right&&this.treeView1.GetNodeAt(e.X,e.Y) != null)
{
TreeNode node=treeView1.GetNodeAt(e.X,e.Y);
this.treeView1.SelectedNode=node;
this.menuItem1.Visible=true;
this.menuItem2.Visible=true;
this.menuItem3.Visible=true;
this.menuItem4.Visible=true;
this.menuItem5.Visible=true;
}
else
{
this.menuItem1.Visible=false;
this.menuItem2.Visible=false;
this.menuItem3.Visible=false;
this.menuItem4.Visible=false;
this.menuItem5.Visible=false;
}
this.contextMenu1.Show(this.treeView1,new Point(e.X,e.Y));
}
else
{
if(e.Button==MouseButtons.Right)
{
this.menuItem1.Visible=true;
this.menuItem2.Visible=true;
this.menuItem3.Visible=true;
this.menuItem4.Visible=true;
this.menuItem5.Visible=true;
}
else
{
this.menuItem1.Visible=false;
this.menuItem2.Visible=false;
this.menuItem3.Visible=false;
this.menuItem4.Visible=false;
this.menuItem5.Visible=false;
}
this.contextMenu1.Show(this.treeView1,new Point(e.X,e.Y)); }
} private void menuItem1_Click(object sender, System.EventArgs e)
{
this.button2.Text="添加";
this.textBox1.Text="";
this.label1.ImageIndex=0;
this.panel1.Visible=true;
this.isin=true;
} private void label1_Click(object sender, System.EventArgs e)
{
this.listView1.Visible=true;
} private void listView1_Click(object sender, System.EventArgs e)
{
int i=this.listView1.SelectedItems[0].Index;
this.label1.ImageIndex=i;
this.listView1.Visible=false;
}
{
TreeNode node=this.treeView1.SelectedNode;
this.isin=false;
this.button2.Text="修改";
this.panel1.Visible=true;
this.textBox1.Text=node.Text;
this.label1.ImageIndex=node.ImageIndex;
this.textBox1.Focus();
this.textBox1.SelectAll();
} private void menuItem2_Click(object sender, System.EventArgs e)
{
DialogResult end=new DialogResult();
end=MessageBox.Show("将要删除全部子部门","警告",MessageBoxButtons.YesNo,MessageBoxIcon.Exclamation,MessageBoxDefaultButton.Button2);
if(end==DialogResult.Yes)
{
TreeNode node=this.treeView1.SelectedNode;
int id=(int)node.Tag;
delnode(id);
this.treeView1.Nodes.Remove(node);
}
}
private void delnode(int id)
{
int uid=id;
int count;
string sql="delete from t1 where id="+uid;
OleDbCommand ca=new OleDbCommand(sql,cn);
ca.ExecuteNonQuery();
ca.Dispose();
sql="select id from t1 where upid="+uid;
OleDbDataAdapter da=new OleDbDataAdapter(sql,cn);
DataSet dss=new DataSet();
da.Fill(dss,"id");
count=dss.Tables[0].Rows.Count;
if(count>0)
{
for(int i=0;i<count;i++)
{
uid=int.Parse(dss.Tables[0].Rows[i][0].ToString());
delnode(uid);
}
}
else
{
return;
}
}
private void button2_Click_1(object sender, System.EventArgs e)
{
int upid;
int image;
int id;
string name;
if(isin==true)
{
TreeNode node=this.treeView1.SelectedNode;
if(node==null)
{
id=1;
upid=0;
image=this.label1.ImageIndex;
name=this.textBox1.Text.Trim();
}
else
{
upid=(int)node.Tag;
image=this.label1.ImageIndex;
name=this.textBox1.Text.Trim();
OleDbDataAdapter da=new OleDbDataAdapter("select max(id) from t1",cn);
DataSet dss=new DataSet();
da.Fill(dss,"id");
id=int.Parse(dss.Tables["id"].Rows[0][0].ToString());
id+=1;
da.Dispose();
dss.Dispose();
}
if(name.Length<1)
MessageBox.Show("请输入名称","错误",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
else
{
string sql="insert into t1 values("+id+",'"+name+"',"+upid+","+image+");";
OleDbCommand ca=new OleDbCommand(sql,cn);
try
{
ca.ExecuteNonQuery();
}
catch(Exception exx)
{
MessageBox.Show(exx.Message);
}
ca.Dispose();
TreeNode node1=new TreeNode(name,image,image);
node1.Tag=id;
if(node==null)
{
this.treeView1.Nodes.Add(node1);
}
else
{
node.Nodes.Add(node1);
}
this.treeView1.Refresh();
}
}
else
{//更新子项
TreeNode node=this.treeView1.SelectedNode;
id=(int)node.Tag;
name=this.textBox1.Text.Trim();
image=this.label1.ImageIndex;
node=null;
string sql="update t1 set name='"+name+"' , [image]="+image+" where id="+id;
OleDbCommand ca=new OleDbCommand(sql,cn);
try
{
ca.ExecuteNonQuery(); }
catch(Exception exx)
{
MessageBox.Show(exx.Message,"错误");
}
ca.Dispose();
this.treeView1.SelectedNode.Text=this.textBox1.Text;
this.treeView1.SelectedNode.ImageIndex=this.label1.ImageIndex;
this.treeView1.SelectedNode.SelectedImageIndex=this.label1.ImageIndex;
this.treeView1.Refresh();
}
this.panel1.Visible=false;
}
}
}