用TreeView构建树型目录(C#版)

用TreeView构建树型目录(C#版)
引言:

参与了一个大型社区程序的开发,现在将相关开发经验陆续总结出来,和大家探讨一下。本节内容主要主要讨论的是用TreeView生成无限级分类的树型目录的问题,相关代码摘录自社区程序中的一个文章发布管理系统中的目录管理部分。

一、数据库设计

BBS_BestArticleFolder 精华区目录表

Key: FolderID

Field name
Type
Length
Default
Description

FolderID
int
4

ID(自动编号)

ParentFolderID
int
4
0
父目录ID

FolderName
Nvarchar
80
 
目录名

BoardID
int
4
0
版块ID

CreaterUserID
int
4
0
创建者ID

AddTime
DateTime
8
GetDate()
创建时间


二、树型目录构建过程

1、安装TreeView控件,我想大多数人都是会安装的,不会安装的去找相关文章一下下。

2、具体实现:

a 数据生成

#region 树的数据生成事件 

private DataView CreateDateView(int boardID)

{

BBSBestArticleFolderCollection bafc=new BBSBestArticleFolderCollection();

bafc.GetInfoByParentFolderID(boardID,-1);

int bafcCount=bafc.Count;

DataTable dt = new DataTable("table");

dt.Columns.Add("FolderID",System.Type.GetType("System.String"));

dt.Columns.Add("FolderName",System.Type.GetType("System.String"));

dt.Columns.Add("ParentFolderID",System.Type.GetType("System.String"));

for(int i=0;i<bafcCount;i++)

{

DataRow dr=dt.NewRow();

dr[0]=bafc[i].ID;

dr[1]=bafc[i].FolderName;

dr[2]=bafc[i].ParentFolderID;

dt.Rows.Add(dr);

}

 

dv=dt.DefaultView;

return dv;

}

 

#endregion

一点说明:BBSBestArticleFolderCollection对象是个集合类,里面存储了所有符合条件的数据集合,GetInfoByParentFolderID是其中的一个方法,根据一定条件填充集合类的,此文主要讲TreeView构建树型目录,此处会在另外的文章中详述。

 

b

#region TreeView节点生成方法

private void CreateTree(TreeNodeCollection folderTreeNodeCollection ,int parentID)

{

dv.RowFilter= "ParentFolderID="+parentID;

int tmpParentFolderID=0;

foreach(DataRowView drv in dv)

{

TreeNode tn=new TreeNode();

tn.ID=drv.Row["FolderID"].ToString();

tn.Text=drv.Row["FolderName"].ToString().Trim();

tmpParentFolderID=Int32.Parse(drv.Row["ParentFolderID"].ToString().Trim());

//tn.CheckBox=true;

folderTreeNodeCollection.Add(tn);

CreateTree(folderTreeNodeCollection[folderTreeNodeCollection.Count - 1].Nodes,Int32.Parse(tn.ID));

}

}

一点解释:递归构建树的各级节点,其中的dv就是上一个方法生成的DataView


c 取得数据的方法有了,生节节点的方法也有了,那么剩下的就是调用了


CreateTree(TreeView1.Nodes,0);


一点解释:TreeView1是控件ID,这个大家都知道吧,这里的0其实就是表示顶级目录,从顶级目录递归调用下去DI

 

C#中TreeView类操作全攻略(一)
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using com.prm.client.tools;
using System.Data.OracleClient;
using com.prm.client.common;
using com.prm.client.sysmanager.popedom;

namespace com.prm.client.forms
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class SM_FunctionMaintenance : System.Windows.Forms.Form
{
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.GroupBox groupBox2;
private System.Windows.Forms.TreeView trv_Function;
private System.Windows.Forms.Label lbl_Detail;
private System.Windows.Forms.Label lbl_BelongTo;
private System.Windows.Forms.Label lbl_FunEntity;
private System.Windows.Forms.Label lbl_Type;
private System.Windows.Forms.Label lbl_IsInterface;
private System.Windows.Forms.Label lbl_Tag;
private System.Windows.Forms.Label lbl_Name;
private System.Windows.Forms.CheckBox ckb_IsInterface;
private System.Windows.Forms.ComboBox cbo_Type;
private System.Windows.Forms.TextBox txt_Detail;
private System.Windows.Forms.TextBox txt_BelongTo;
private System.Windows.Forms.TextBox txt_FunEntity;
private System.Windows.Forms.TextBox txt_Tag;
private System.Windows.Forms.TextBox txt_Name;
private System.Data.DataSet dataSet1;
private System.Data.DataColumn dataColumn1;
private System.Data.DataColumn dataColumn2;
private System.Data.DataColumn dataColumn3;
private System.Data.DataColumn dataColumn4;
private System.Data.DataColumn dataColumn5;
private System.Data.DataColumn dataColumn6;
private System.Data.DataColumn dataColumn7;
private System.Data.DataColumn dataColumn8;
private System.Data.DataColumn dataColumn9;
private System.Data.DataColumn dataColumn10;
private System.Data.DataColumn dataColumn11;
//用来保存功能表中所有的数据
private System.Data.DataTable funcDataTable;

//用来保存Connection属性
private OracleConnection funcTableConn;
//用来保存数字字典功能类别的数据
ClientDictionary[] DIC_funcType;
//用来保存树总的虚拟根节点的ID,默认为0
public const string ROOT_NODE_VALUE = "0";
private System.Windows.Forms.Button btn_Help;
private System.Windows.Forms.Button btn_Remove;
private System.Windows.Forms.Button btn_Modify;
private System.Windows.Forms.Button btn_AddSon;
private System.Windows.Forms.Button btn_AddRoot;
private System.Windows.Forms.Button btn_Close;

 

//树结点的临时信息
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;

public SM_FunctionMaintenance(SM_Popedom popedom)
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();

//生成类DataAccessObject的实例,并保存连接属性
DataAccessObject funcTableAccessObject=new DataAccessObject();
funcTableConn=funcTableAccessObject.Connection;
//调用QueryAllFuncInfo过程来对变量funcDataTable进行赋值
QueryAllFuncInfo();
const string VALUEMEMBER="valueMember",DISPLAYMEMBER="displayMember",NULLTEXT="";
//调用QueryFuncTypeDictionary过程来对变量DIC_funcType进行赋值
QueryFuncTypeDictionary();
cbo_Type.DataSource = DIC_funcType;
cbo_Type.ValueMember = VALUEMEMBER;
cbo_Type.DisplayMember = DISPLAYMEMBER;
}

/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(SM_FunctionMaintenance));
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.trv_Function = new System.Windows.Forms.TreeView();
this.groupBox2 = new System.Windows.Forms.GroupBox();
this.lbl_Detail = new System.Windows.Forms.Label();
this.lbl_BelongTo = new System.Windows.Forms.Label();
this.lbl_FunEntity = new System.Windows.Forms.Label();
this.lbl_Type = new System.Windows.Forms.Label();
this.lbl_IsInterface = new System.Windows.Forms.Label();
this.lbl_Tag = new System.Windows.Forms.Label();
this.lbl_Name = new System.Windows.Forms.Label();
this.cbo_Type = new System.Windows.Forms.ComboBox();
this.txt_Detail = new System.Windows.Forms.TextBox();
this.txt_BelongTo = new System.Windows.Forms.TextBox();
this.txt_FunEntity = new System.Windows.Forms.TextBox();
this.ckb_IsInterface = new System.Windows.Forms.CheckBox();
this.txt_Tag = new System.Windows.Forms.TextBox();
this.txt_Name = new System.Windows.Forms.TextBox();
this.dataSet1 = new System.Data.DataSet();
this.funcDataTable = new System.Data.DataTable();
this.dataColumn1 = new System.Data.DataColumn();
this.dataColumn2 = new System.Data.DataColumn();
this.dataColumn3 = new System.Data.DataColumn();
this.dataColumn4 = new System.Data.DataColumn();
this.dataColumn5 = new System.Data.DataColumn();
this.dataColumn6 = new System.Data.DataColumn();
this.dataColumn7 = new System.Data.DataColumn();
this.dataColumn8 = new System.Data.DataColumn();
this.dataColumn9 = new System.Data.DataColumn();
this.dataColumn10 = new System.Data.DataColumn();
this.dataColumn11 = new System.Data.DataColumn();
this.btn_Help = new System.Windows.Forms.Button();
this.btn_Remove = new System.Windows.Forms.Button();
this.btn_Modify = new System.Windows.Forms.Button();
this.btn_AddSon = new System.Windows.Forms.Button();
this.btn_AddRoot = new System.Windows.Forms.Button();
this.btn_Close = new System.Windows.Forms.Button();
this.groupBox1.SuspendLayout();
this.groupBox2.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.dataSet1)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.funcDataTable)).BeginInit();
this.SuspendLayout();
//
// groupBox1
//
this.groupBox1.Controls.Add(this.trv_Function);
this.groupBox1.Location = new System.Drawing.Point(24, 24);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(440, 496);
this.groupBox1.TabIndex = 0;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "功能列表:";
//
// trv_Function
//
this.trv_Function.AllowDrop = true;
this.trv_Function.ForeColor = System.Drawing.Color.FromArgb(((System.Byte)(30)), ((System.Byte)(66)), ((System.Byte)(94)));
this.trv_Function.ImageIndex = -1;
this.trv_Function.Indent = 40;
this.trv_Function.Location = new System.Drawing.Point(8, 16);
this.trv_Function.Name = "trv_Function";
this.trv_Function.SelectedImageIndex = -1;
this.trv_Function.Size = new System.Drawing.Size(424, 472);
this.trv_Function.TabIndex = 0;
this.trv_Function.DragOver += new System.Windows.Forms.DragEventHandler(this.trv_Function_DragOver);
this.trv_Function.AfterSelect += new System.Windows.Forms.TreeViewEventHandler(this.trv_Function_AfterSelect);
this.trv_Function.DragEnter += new System.Windows.Forms.DragEventHandler(this.trv_Function_DragEnter);
this.trv_Function.ItemDrag += new System.Windows.Forms.ItemDragEventHandler(this.trv_Function_ItemDrag);
this.trv_Function.DragDrop += new System.Windows.Forms.DragEventHandler(this.trv_Function_DragDrop);
//
// groupBox2
//
this.groupBox2.Controls.Add(this.lbl_Detail);
this.groupBox2.Controls.Add(this.lbl_BelongTo);
this.groupBox2.Controls.Add(this.lbl_FunEntity);
this.groupBox2.Controls.Add(this.lbl_Type);
this.groupBox2.Controls.Add(this.lbl_IsInterface);
this.groupBox2.Controls.Add(this.lbl_Tag);
this.groupBox2.Controls.Add(this.lbl_Name);
this.groupBox2.Controls.Add(this.cbo_Type);
this.groupBox2.Controls.Add(this.txt_Detail);
this.groupBox2.Controls.Add(this.txt_BelongTo);
this.groupBox2.Controls.Add(this.txt_FunEntity);
this.groupBox2.Controls.Add(this.ckb_IsInterface);
this.groupBox2.Controls.Add(this.txt_Tag);
this.groupBox2.Controls.Add(this.txt_Name);
this.groupBox2.Location = new System.Drawing.Point(472, 24);
this.groupBox2.Name = "groupBox2";
this.groupBox2.Size = new System.Drawing.Size(296, 496);
this.groupBox2.TabIndex = 1;
this.groupBox2.TabStop = false;
this.groupBox2.Text = "功能详细信息:";
//
// lbl_Detail
//
this.lbl_Detail.Location = new System.Drawing.Point(56, 320);
this.lbl_Detail.Name = "lbl_Detail";
this.lbl_Detail.Size = new System.Drawing.Size(48, 23);
this.lbl_Detail.TabIndex = 13;
this.lbl_Detail.Text = "描述:";
//
// lbl_BelongTo
//
this.lbl_BelongTo.Location = new System.Drawing.Point(8, 272);
this.lbl_BelongTo.Name = "lbl_BelongTo";
this.lbl_BelongTo.Size = new System.Drawing.Size(94, 23);
this.lbl_BelongTo.TabIndex = 12;
this.lbl_BelongTo.Text = "所属窗口标识:";
//
// lbl_FunEntity
//
this.lbl_FunEntity.Location = new System.Drawing.Point(40, 224);
this.lbl_FunEntity.Name = "lbl_FunEntity";
this.lbl_FunEntity.Size = new System.Drawing.Size(64, 23);
this.lbl_FunEntity.TabIndex = 11;
this.lbl_FunEntity.Text = " 功能体:";
//
// lbl_Type
//
this.lbl_Type.Location = new System.Drawing.Point(56, 176);
this.lbl_Type.Name = "lbl_Type";
this.lbl_Type.Size = new System.Drawing.Size(48, 23);
this.lbl_Type.TabIndex = 10;
this.lbl_Type.Text = "类别:";
//
// lbl_IsInterface
//
this.lbl_IsInterface.Location = new System.Drawing.Point(32, 128);
this.lbl_IsInterface.Name = "lbl_IsInterface";
this.lbl_IsInterface.Size = new System.Drawing.Size(72, 23);
this.lbl_IsInterface.TabIndex = 9;
this.lbl_IsInterface.Text = "有无界面:";
//
// lbl_Tag
//
this.lbl_Tag.Location = new System.Drawing.Point(56, 88);
this.lbl_Tag.Name = "lbl_Tag";
this.lbl_Tag.Size = new System.Drawing.Size(48, 23);
this.lbl_Tag.TabIndex = 8;
this.lbl_Tag.Text = "标识:";
//
// lbl_Name
//
this.lbl_Name.Location = new System.Drawing.Point(56, 40);
this.lbl_Name.Name = "lbl_Name";
this.lbl_Name.Size = new System.Drawing.Size(48, 23);
this.lbl_Name.TabIndex = 7;
this.lbl_Name.Text = "名称:";
//
// cbo_Type
//
this.cbo_Type.Enabled = false;
this.cbo_Type.ForeColor = System.Drawing.Color.FromArgb(((System.Byte)(30)), ((System.Byte)(66)), ((System.Byte)(94)));
this.cbo_Type.Location = new System.Drawing.Point(104, 168);
this.cbo_Type.Name = "cbo_Type";
this.cbo_Type.Size = new System.Drawing.Size(176, 20);
this.cbo_Type.TabIndex = 6;
//
// txt_Detail
//
this.txt_Detail.ForeColor = System.Drawing.Color.FromArgb(((System.Byte)(30)), ((System.Byte)(66)), ((System.Byte)(94)));
this.txt_Detail.Location = new System.Drawing.Point(104, 312);
this.txt_Detail.Name = "txt_Detail";
this.txt_Detail.ReadOnly = true;
this.txt_Detail.Size = new System.Drawing.Size(176, 21);
this.txt_Detail.TabIndex = 5;
this.txt_Detail.Text = "";
//
// txt_BelongTo
//
this.txt_BelongTo.ForeColor = System.Drawing.Color.FromArgb(((System.Byte)(30)), ((System.Byte)(66)), ((System.Byte)(94)));
this.txt_BelongTo.Location = new System.Drawing.Point(104, 264);
this.txt_BelongTo.Name = "txt_BelongTo";
this.txt_BelongTo.ReadOnly = true;
this.txt_BelongTo.Size = new System.Drawing.Size(176, 21);
this.txt_BelongTo.TabIndex = 4;
this.txt_BelongTo.Text = "";
//
// txt_FunEntity
//
this.txt_FunEntity.ForeColor = System.Drawing.Color.FromArgb(((System.Byte)(30)), ((System.Byte)(66)), ((System.Byte)(94)));
this.txt_FunEntity.Location = new System.Drawing.Point(104, 216);
this.txt_FunEntity.Name = "txt_FunEntity";
this.txt_FunEntity.ReadOnly = true;
this.txt_FunEntity.Size = new System.Drawing.Size(176, 21);
this.txt_FunEntity.TabIndex = 3;
this.txt_FunEntity.Text = "";
//
// ckb_IsInterface
//
this.ckb_IsInterface.Enabled = false;
this.ckb_IsInterface.Location = new System.Drawing.Point(104, 120);
this.ckb_IsInterface.Name = "ckb_IsInterface";
this.ckb_IsInterface.Size = new System.Drawing.Size(24, 24);
this.ckb_IsInterface.TabIndex = 2;
//
// txt_Tag
//
this.txt_Tag.ForeColor = System.Drawing.Color.FromArgb(((System.Byte)(30)), ((System.Byte)(66)), ((System.Byte)(94)));
this.txt_Tag.Location = new System.Drawing.Point(104, 80);
this.txt_Tag.Name = "txt_Tag";
this.txt_Tag.ReadOnly = true;
this.txt_Tag.Size = new System.Drawing.Size(176, 21);
this.txt_Tag.TabIndex = 1;
this.txt_Tag.Text = "";
//
// txt_Name
//
this.txt_Name.ForeColor = System.Drawing.Color.FromArgb(((System.Byte)(30)), ((System.Byte)(66)), ((System.Byte)(94)));
this.txt_Name.Location = new System.Drawing.Point(104, 32);
this.txt_Name.Name = "txt_Name";
this.txt_Name.ReadOnly = true;
this.txt_Name.Size = new System.Drawing.Size(176, 21);
this.txt_Name.TabIndex = 0;
this.txt_Name.Text = "";
//
// dataSet1
//
this.dataSet1.DataSetName = "NewDataSet";
this.dataSet1.Locale = new System.Globalization.CultureInfo("zh-CN");
this.dataSet1.Tables.AddRange(new System.Data.DataTable[] {
this.funcDataTable});
//
// funcDataTable
//
this.funcDataTable.Columns.AddRange(new System.Data.DataColumn[] {
this.dataColumn1,
this.dataColumn2,
this.dataColumn3,
this.dataColumn4,
this.dataColumn5,
this.dataColumn6,
this.dataColumn7,
this.dataColumn8,
this.dataColumn9,
this.dataColumn10,
this.dataColumn11});
this.funcDataTable.TableName = "funcDataTable";
//
// dataColumn1
//
this.dataColumn1.ColumnName = "功能ID";
//
// dataColumn2
//
this.dataColumn2.ColumnName = "功能标识";
//
// dataColumn3
//
this.dataColumn3.ColumnName = "功能名称";
//
// dataColumn4
//
this.dataColumn4.ColumnName = "功能简述";
//
// dataColumn5
//
this.dataColumn5.ColumnName = "所属窗体";
//
// dataColumn6
//
this.dataColumn6.ColumnName = "有无界面";
//
// dataColumn7
//
this.dataColumn7.ColumnName = "功能类别";
//
// dataColumn8
//
this.dataColumn8.ColumnName = "功能体";
//
// dataColumn9
//
this.dataColumn9.ColumnName = "上级功能ID";
//
// dataColumn10
//
this.dataColumn10.ColumnName = "可否展显";
//
// dataColumn11
//
this.dataColumn11.ColumnName = "版本号";
//
// btn_Help
//
this.btn_Help.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("btn_Help.BackgroundImage")));
this.btn_Help.Location = new System.Drawing.Point(536, 536);
this.btn_Help.Name = "btn_Help";
this.btn_Help.Size = new System.Drawing.Size(75, 22);
this.btn_Help.TabIndex = 10;
this.btn_Help.Text = "帮 助";
this.btn_Help.Click += new System.EventHandler(this.btn_Help_Click);
//
// btn_Remove
//
this.btn_Remove.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("btn_Remove.BackgroundImage")));
this.btn_Remove.Location = new System.Drawing.Point(344, 536);
this.btn_Remove.Name = "btn_Remove";
this.btn_Remove.Size = new System.Drawing.Size(75, 22);
this.btn_Remove.TabIndex = 9;
this.btn_Remove.Text = "删 除";
this.btn_Remove.Click += new System.EventHandler(this.btn_Remove_Click);
//
// btn_Modify
//
this.btn_Modify.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("btn_Modify.BackgroundImage")));
this.btn_Modify.Location = new System.Drawing.Point(248, 536);
this.btn_Modify.Name = "btn_Modify";
this.btn_Modify.Size = new System.Drawing.Size(75, 22);
this.btn_Modify.TabIndex = 8;
this.btn_Modify.Text = "修 改";
this.btn_Modify.Click += new System.EventHandler(this.btn_Modify_Click);
//
// btn_AddSon
//
this.btn_AddSon.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("btn_AddSon.BackgroundImage")));
this.btn_AddSon.Location = new System.Drawing.Point(152, 536);
this.btn_AddSon.Name = "btn_AddSon";
this.btn_AddSon.Size = new System.Drawing.Size(80, 22);
this.btn_AddSon.TabIndex = 7;
this.btn_AddSon.Text = "新增子节点";
this.btn_AddSon.Click += new System.EventHandler(this.btn_AddSon_Click);
//
// btn_AddRoot
//
this.btn_AddRoot.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("btn_AddRoot.BackgroundImage")));
this.btn_AddRoot.Location = new System.Drawing.Point(56, 536);
this.btn_AddRoot.Name = "btn_AddRoot";
this.btn_AddRoot.Size = new System.Drawing.Size(80, 22);
this.btn_AddRoot.TabIndex = 6;
this.btn_AddRoot.Text = "新增根节点";
this.btn_AddRoot.Click += new System.EventHandler(this.btn_AddRoot_Click);
//
// btn_Close
//
this.btn_Close.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("btn_Close.BackgroundImage")));
this.btn_Close.Location = new System.Drawing.Point(632, 536);
this.btn_Close.Name = "btn_Close";
this.btn_Close.Size = new System.Drawing.Size(75, 22);
this.btn_Close.TabIndex = 11;
this.btn_Close.Text = "关 闭";
this.btn_Close.Click += new System.EventHandler(this.btn_Close_Click);
//
// SM_FunctionMaintenance
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(242)), ((System.Byte)(247)), ((System.Byte)(250)));
this.ClientSize = new System.Drawing.Size(800, 600);
this.ControlBox = false;
this.Controls.Add(this.btn_Close);
this.Controls.Add(this.btn_Help);
this.Controls.Add(this.btn_Remove);
this.Controls.Add(this.btn_Modify);
this.Controls.Add(this.btn_AddSon);
this.Controls.Add(this.btn_AddRoot);
this.Controls.Add(this.groupBox2);
this.Controls.Add(this.groupBox1);
this.ForeColor = System.Drawing.Color.FromArgb(((System.Byte)(30)), ((System.Byte)(66)), ((System.Byte)(94)));
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "SM_FunctionMaintenance";
this.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
this.Load += new System.EventHandler(this.SM_FunctionMaintenance_Load);
this.groupBox1.ResumeLayout(false);
this.groupBox2.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.dataSet1)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.funcDataTable)).EndInit();
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new SM_FunctionMaintenance(null));
}

/// <summary>
/// 查询功能表的全部内容,并初始化所有的树节点信息;把查询结果保存在funcDataTable这个DataTable对象中
/// 该过程对变量funcDataTable和treeNodes进行赋值
/// </summary>
/// <param name=""></param>
/// <returns></returns>
private void QueryAllFuncInfo()
{
funcTableConn.Open ();
OracleCommand mySelectCmd= new OracleCommand();
mySelectCmd.Connection = funcTableConn;
mySelectCmd.CommandText = "SELECT * FROM 功能 ORDER BY 功能ID" ;
mySelectCmd.CommandType = CommandType.Text ;
OracleDataAdapter myOracleDataAdapter = new OracleDataAdapter();
myOracleDataAdapter.SelectCommand=mySelectCmd;
myOracleDataAdapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;
try
{
myOracleDataAdapter.Fill(funcDataTable);
}
catch(Exception ex)
{
ErrorHandle.show("查询功能表时发生异常错误。", ex, "功能维护");
//MessageBox.Show("查询功能表时出现错误:"+ex.ToString());
}
finally
{
funcTableConn.Close ( ) ;
}

}

/// <summary>
/// 查询功能ID为funcID的记录内容;把查询结构保存在funcItem这个FunctionTable对象中并且返回该结果;
/// </summary>
/// <param name="funcID"></param>
/// <returns>funcItem</returns>
private FunctionTable QueryFuncItem(long funcID)
{
int funcEntityNum=funcDataTable.Rows.Count;
int findindex=-1;
FunctionTable funcItem=new FunctionTable();
for(int i=0;i<funcEntityNum;i++)
{
if (funcID ==long.Parse((funcDataTable.Rows[i].ItemArray[0].ToString())))
{
findindex=i;
break;
}
}
if(findindex==-1)
{
ClientMessageBox.showWarn("没有查到相关的数据!", "功能维护");
//MessageBox.Show("没有查到相关的数据!");
}
else
{
funcItem.funcID=long.Parse((funcDataTable.Rows[findindex].ItemArray[0].ToString()));
funcItem.funcTag=(funcDataTable.Rows[findindex].ItemArray[1].ToString());
funcItem.funcName=(funcDataTable.Rows[findindex].ItemArray[2].ToString());
funcItem.funcDetail=(funcDataTable.Rows[findindex].ItemArray[3].ToString());
funcItem.funcBelongTo=(funcDataTable.Rows[findindex].ItemArray[4].ToString());
funcItem.funcIsInterface=(funcDataTable.Rows[findindex].ItemArray[5].ToString());
funcItem.funcType=(funcDataTable.Rows[findindex].ItemArray[6].ToString());
funcItem.funcEntity=(funcDataTable.Rows[findindex].ItemArray[7].ToString());
funcItem.funcFatherID=long.Parse((funcDataTable.Rows[findindex].ItemArray[8].ToString()));
funcItem.funcIsExpand=(funcDataTable.Rows[findindex].ItemArray[9].ToString());
funcItem.funcVer=(funcDataTable.Rows[findindex].ItemArray[10].ToString());
}
return funcItem;

}

/// <summary>
/// 查询数字字典中功能类别的内容;并把查询结构保存在ClientDictionary对象DIC_funcType中;
/// </summary>
/// <param name=""></param>
/// <returns></returns>
private void QueryFuncTypeDictionary()
{
funcTableConn.Open ();
OracleCommand mySelectCmd= new OracleCommand();
mySelectCmd.Connection = funcTableConn;
mySelectCmd.CommandText = "select * from 数据字典,属性 where 数据字典.属性ID = 属性.属性ID and 属性.属性名称 = '功能类别'";
mySelectCmd.CommandType = CommandType.Text ;
try
{
OracleDataReader myReader = mySelectCmd.ExecuteReader();
int dicCount=0;
if (myReader.HasRows)
{
while (myReader.Read())
{
dicCount++;
}
}
myReader.Close();
myReader = mySelectCmd.ExecuteReader();
DIC_funcType=new ClientDictionary[dicCount];
for(int i=0;i<dicCount;i++)
{
myReader.Read();
DIC_funcType[i]=new ClientDictionary(myReader.GetString(4),myReader.GetString(3));
}
myReader.Close();
}
catch(Exception ex)
{
MessageBox.Show("查询数据字典时出现错误:"+ex.ToString());
}
finally
{
funcTableConn.Close ( ) ;
}
}


/// <summary>
/// 将数据添加到TreeView控件中
/// </summary>
/// <param name="Nds">当前层的所有结点</param>
/// <param name="parentId">当前层的父亲结点</param>
private void InitTreeView(TreeNodeCollection Nds,string parentId)
{
//建立视图,为以后设立查询条件作准备
DataView mydataview=new DataView();
//新节点
TreeNode newNode;
//新节点的父亲节点ID
string newNodeFatherId;
//dataview的数据源
mydataview.Table=funcDataTable;
//过滤条件
mydataview.RowFilter="上级功能ID="+parentId;

foreach(DataRowView drv in mydataview)
{
newNode=new TreeNode();
newNode.Tag=drv["功能ID"].ToString();
newNode.Text=drv["功能名称"].ToString();
Nds.Add(newNode);
newNodeFatherId=drv["上级功能ID"].ToString();
//递归调用遍历当前节点的子节点
InitTreeView(newNode.Nodes,newNode.Tag.ToString ());
}
}


/// <summary>
/// 取得TreeView某一节点所有的子结点编号和子结点显示名称
/// </summary>
/// <param name="enumNodes"></param>
/// <returns></returns>
private Hashtable GetNodesValue(IEnumerator enumNodes)
{
//存储树结点信息的哈希表
Hashtable result =new Hashtable();

//当前结点
TreeNode node = null;
//当前节点的父亲结点
TreeNode father = null;

//取得所有结点的编号和显示名称
while(enumNodes.MoveNext())
{
//取得一个结点和它的父亲结点
node = (TreeNode)enumNodes.Current;
father = node.Parent;

//取得当前结点的所有儿子
TreeNodeCollection sonNodes = node.Nodes;

IEnumerator sonEnumNodes = sonNodes.GetEnumerator();
//递归取得所有儿子结点的编号和显示名称
Hashtable sonResult = GetNodesValue(sonEnumNodes);

if (sonResult.Count !=0)
{
//取得所有儿子结点信息的列举
IDictionaryEnumerator enumDic = sonResult.GetEnumerator();
while(enumDic.MoveNext())
{
//将所有儿子信息添加到存储节点信息的哈希表中
result.Add(enumDic.Key, enumDic.Value);
}
}
result.Add(node.Tag, node.Text);
}

//将保存结点信息的结果返回
return result;
}

 

/// <summary>
/// 选中某个节点后取得该节点的ID,并显示该ID对应的信息;
/// </summary>
private void trv_Function_AfterSelect(object sender, System.Windows.Forms.TreeViewEventArgs e)
{
DataView mydataview = new DataView ();
mydataview.Table= funcDataTable;
mydataview.RowFilter= "功能ID="+ e.Node.Tag.ToString();
foreach ( DataRowView editRow in mydataview)
{
this.txt_Tag.Text=editRow["功能标识"].ToString();
this.txt_Name.Text=editRow["功能名称"].ToString();
this.txt_Detail.Text=editRow["功能简述"].ToString();
this.txt_BelongTo.Text=editRow["所属窗体"].ToString();
;
this.cbo_Type.SelectedValue=editRow["功能类别"].ToString();
this.txt_FunEntity.Text=editRow["功能体"].ToString();
if (editRow["有无界面"].ToString() =="0")
{
this.ckb_IsInterface.Checked=false;
}
else
{
this.ckb_IsInterface.Checked=true;
}
}
}

/// <summary>
/// 初始化树,并把焦点定位在第一个根节点上;
/// </summary>
private void SM_FunctionMaintenance_Load(object sender, System.EventArgs e)
{
InitTreeView(trv_Function.Nodes,ROOT_NODE_VALUE);
//聚焦到树的第一个节点上
trv_Function.SelectedNode=trv_Function.Nodes[0];
trv_Function.Focus();
//折叠所有的树的节点
trv_Function.CollapseAll();
}

private void btn_AddRoot_Click(object sender, System.EventArgs e)
{
SM_AddNewFunction addRootForm=new SM_AddNewFunction(0,DIC_funcType);
//addRootForm窗口关闭时将会对addRootForm.NewFuncItem这个属性进行赋值
addRootForm.ShowDialog();
//如果返回的功能ID不为-1表示增加成功,则
//更新funcDataTable,把新增的节点加到funcDataTable中
if(addRootForm.NewFuncItem.funcID !=-1)
{
DataRow myRow=funcDataTable.NewRow();
myRow["功能ID"] = addRootForm.NewFuncItem.funcID;
myRow["功能标识"] = addRootForm.NewFuncItem.funcTag;
myRow["功能名称"] = addRootForm.NewFuncItem.funcName;
myRow["功能简述"] = addRootForm.NewFuncItem.funcDetail;
myRow["所属窗体"] = addRootForm.NewFuncItem.funcBelongTo;
myRow["有无界面"] = addRootForm.NewFuncItem.funcIsInterface;
myRow["功能类别"] = addRootForm.NewFuncItem.funcType;
myRow["功能体"] = addRootForm.NewFuncItem.funcEntity;
myRow["上级功能ID"] = addRootForm.NewFuncItem.funcFatherID;
myRow["可否展显"] = addRootForm.NewFuncItem.funcIsExpand;
myRow["版本号"] = addRootForm.NewFuncItem.funcVer;

funcDataTable.Rows.Add(myRow);

//在树的根部添加新增加的节点
TreeNode newRootNode=new TreeNode(addRootForm.NewFuncItem.funcName);
newRootNode.Tag=addRootForm.NewFuncItem.funcID.ToString();
trv_Function.Nodes.Add(newRootNode);
//聚焦到新增的节点上
trv_Function.SelectedNode=newRootNode;
}
trv_Function.Focus();
}

private void btn_AddSon_Click(object sender, System.EventArgs e)
{
if(trv_Function.SelectedNode!= null)
{
long newFuncItemFatherID=long.Parse(trv_Function.SelectedNode.Tag.ToString());
SM_AddNewFunction addSonForm=new SM_AddNewFunction(newFuncItemFatherID,DIC_funcType);
//addSonForm窗口关闭时将会对addSonForm.NewFuncItem这个属性进行赋值
addSonForm.ShowDialog();
//如果返回的功能ID不为-1表示增加成功,则
//更新funcDataTable,把新增的节点加到funcDataTable中
if(addSonForm.NewFuncItem.funcID !=-1)
{
DataRow myRow=funcDataTable.NewRow();
myRow["功能ID"] = addSonForm.NewFuncItem.funcID;
myRow["功能标识"] = addSonForm.NewFuncItem.funcTag;
myRow["功能名称"] = addSonForm.NewFuncItem.funcName;
myRow["功能简述"] = addSonForm.NewFuncItem.funcDetail;
myRow["所属窗体"] = addSonForm.NewFuncItem.funcBelongTo;
myRow["有无界面"] = addSonForm.NewFuncItem.funcIsInterface;
myRow["功能类别"] = addSonForm.NewFuncItem.funcType;
myRow["功能体"] = addSonForm.NewFuncItem.funcEntity;
myRow["上级功能ID"] = addSonForm.NewFuncItem.funcFatherID;
myRow["可否展显"] = addSonForm.NewFuncItem.funcIsExpand;
myRow["版本号"] = addSonForm.NewFuncItem.funcVer;

funcDataTable.Rows.Add(myRow);

//在树的选定的节点添加新的子节点
TreeNode newSonNode=new TreeNode(addSonForm.NewFuncItem.funcName);
newSonNode.Tag=addSonForm.NewFuncItem.funcID.ToString();
trv_Function.SelectedNode.Nodes.Add(newSonNode);
//聚焦到新增的节点上
trv_Function.SelectedNode=newSonNode;
}
trv_Function.Focus();
}
else
{
MessageBox.Show("没有选中节点!","警告",MessageBoxButtons.OK,MessageBoxIcon.Warning);
}

}

private void btn_Modify_Click(object sender, System.EventArgs e)
{
if(trv_Function.SelectedNode!= null)
{
long editFuncItemFatherID=long.Parse(trv_Function.SelectedNode.Tag.ToString());
FunctionTable editFuncItem =QueryFuncItem(editFuncItemFatherID);

SM_EditFunction editForm=new SM_EditFunction(editFuncItem,DIC_funcType);
//editForm窗口关闭时将会对editForm.NewFuncItem和editForm.IsEdit这两个属性进行赋值
editForm.ShowDialog();
//如果用户进行了修改,则更新funcDataTable;
if(editForm.IsEdit==true)
{
DataView mydataview = new DataView ();
mydataview.Table= funcDataTable;
mydataview.RowFilter= "功能ID="+ editFuncItemFatherID.ToString();
foreach ( DataRowView editRow in mydataview)
{
editRow["功能标识"] = editForm.NewFuncItem.funcTag;
editRow["功能名称"] = editForm.NewFuncItem.funcName;
editRow["功能简述"] = editForm.NewFuncItem.funcDetail;
editRow["所属窗体"] = editForm.NewFuncItem.funcBelongTo;
editRow["有无界面"] = editForm.NewFuncItem.funcIsInterface;
editRow["功能类别"] = editForm.NewFuncItem.funcType;
editRow["功能体"] = editForm.NewFuncItem.funcEntity;
editRow["上级功能ID"] = editForm.NewFuncItem.funcFatherID;
editRow["可否展显"] = editForm.NewFuncItem.funcIsExpand;
editRow["版本号"] = editForm.NewFuncItem.funcVer;
}
//修改对应的树的节点名称
trv_Function.SelectedNode.Text=editForm.NewFuncItem.funcName;

//更改显示区域的相应的内容
this.txt_Tag.Text=editForm.NewFuncItem.funcTag;
this.txt_Name.Text=editForm.NewFuncItem.funcName;
this.txt_Detail.Text=editForm.NewFuncItem.funcDetail;
this.txt_BelongTo.Text=editForm.NewFuncItem.funcBelongTo;
;
this.cbo_Type.SelectedValue=editForm.NewFuncItem.funcType;
this.txt_FunEntity.Text=editForm.NewFuncItem.funcEntity;
if (editForm.NewFuncItem.funcIsInterface =="0")
{
this.ckb_IsInterface.Checked=false;
}
else
{
this.ckb_IsInterface.Checked=true;
}
}

trv_Function.Focus();
}
else
{
MessageBox.Show("没有选中要修改的节点!","警告",MessageBoxButtons.OK,MessageBoxIcon.Warning);
}

}


private void btn_Remove_Click(object sender, System.EventArgs e)
{
if(trv_Function.SelectedNode!= null)
{
if (ClientMessageBox.showConfirm("你确定要删除这些数据吗?", "功能维护")== DialogResult.Yes)
//if (MessageBox.Show ("你确定要删除这些数据吗?", "删除数据",
//MessageBoxButtons.YesNo, MessageBoxIcon.Question)== DialogResult.Yes)
{
TreeNode nextFocusNode=new TreeNode();
//如果选中的是根节点(根据路径名是否等于节点名判断,两者相等的话则表示是根节点
if(trv_Function.SelectedNode.FullPath==trv_Function.SelectedNode.Text)
{
//取得第一层的节点数目
int nodesCount=trv_Function.Nodes.Count;

//如果不是同一层中的最后一个节点,则取下一个节点为聚焦节点
//根据Index判断节点的位置,Index等于节点数减一表示是最后一个节点
if(trv_Function.SelectedNode.Index !=nodesCount-1)
{
nextFocusNode=trv_Function.SelectedNode.NextVisibleNode;
}
//否则取该层第一节点为聚焦节点
else
{
nextFocusNode=trv_Function.Nodes[0];
}
}
//如果选中的不是根节点
else
{
//取得第该层的节点数目
int nodesCount=trv_Function.SelectedNode.Parent.Nodes.Count;

//如果选中的节点的父节点只有一个子节点,则删除后聚焦的节点为父节点
if(nodesCount==1)
{
nextFocusNode=trv_Function.SelectedNode.Parent;
}
//根据Index判断节点的位置,Index等于节点数减一表示是最后一个节点
//如果不是同一层中的最后一个节点,则取下一个节点为聚焦节点
else if(trv_Function.SelectedNode.Index !=nodesCount-1)
{
nextFocusNode=trv_Function.SelectedNode.NextVisibleNode;
}
//否则取该层第一节点为聚焦节点
else
{
nextFocusNode=trv_Function.SelectedNode.Parent.Nodes[0];
}
}
long deleteFuncItemID=long.Parse(trv_Function.SelectedNode.Tag.ToString());
//
TreeNodeCollection deleteNodes = trv_Function.SelectedNode.Nodes;
IEnumerator enumNodes = deleteNodes.GetEnumerator();

//调用GetNodesValue取得要删除的节点及其子节点的节点数目
Hashtable hashNodes =GetNodesValue(enumNodes);

//arrDeleteID数组保存要删除的节点及其子节点的ID;
long[] arrDeleteID=new long[hashNodes.Count+1];

//arrDeleteName数组保存要删除的节点及其子节点的名称;
string[] arrDeleteName=new string[hashNodes.Count+1];

//把要删除的节点的ID和名称作为数组的第一个元素
arrDeleteID[0]=deleteFuncItemID;
arrDeleteName[0]=trv_Function.SelectedNode.Text;

IDictionaryEnumerator enumHashNodes = hashNodes.GetEnumerator();
int index=0;
//把要删除的节点的所有子节点作为数组的其他元素
while(enumHashNodes.MoveNext())
{
arrDeleteID[index+1]=long.Parse(enumHashNodes.Key.ToString());
arrDeleteName[index+1]=enumHashNodes.Value.ToString();
index++;
}
//

for(int i=0;i<arrDeleteID.Length;i++)
{
DeleteFuncItem(arrDeleteID[i],arrDeleteName[i]);
}
Console.WriteLine();

//更新funcDataTable
for(int h=0;h<arrDeleteID.Length;h++)
{
for(int i=0;i<funcDataTable.Rows.Count;i++)
{
if (funcDataTable.Rows[i].ItemArray[0].ToString()==arrDeleteID[h].ToString())
{
funcDataTable.Rows[i].Delete();
break;
}
}
funcDataTable.AcceptChanges();
}

//删除树上的相关节点
trv_Function.Nodes.Remove(trv_Function.SelectedNode);

//聚焦到下一个所要聚焦的节点
trv_Function.SelectedNode=nextFocusNode;
trv_Function.Focus();
}
else
{
trv_Function.Focus();
return;
}
}
else
{
MessageBox.Show("没有选中要删除的节点!","警告",MessageBoxButtons.OK,MessageBoxIcon.Warning);
}

}


/// <summary>
/// 删除某个功能,并且删除角色功能表和操作员授权表中相应的功能的记录;
/// 且把界面定制表中相应的功能名称后面加上“(该功能已收回)”字样,并对改表中
/// 相应的功能ID置为null
/// </summary>
private void DeleteFuncItem(long funcID,string funcName)
{
funcTableConn.Open();
System.Data.OracleClient.OracleTransaction myTran = funcTableConn.BeginTransaction ();
try
{
OracleCommand cmd = new OracleCommand ();
cmd.Transaction = myTran;
cmd.Connection =funcTableConn;
cmd.CommandText = "DELETE from 功能 where 功能ID = '"+funcID+"'";
cmd.ExecuteNonQuery ();
cmd.CommandText = "DELETE from 角色功能 where 功能ID = '"+funcID+"'";
cmd.ExecuteNonQuery ();
cmd.CommandText = "DELETE from 操作员授权 where 功能ID = '"+funcID+"'";
cmd.ExecuteNonQuery ();
string newFuncName=funcName+"(该功能已收回!)";
cmd.CommandText = "UPDATE 界面定制 set 功能ID=null,节点名称='" + newFuncName+"'"+ " where 功能ID ="+funcID;
cmd.ExecuteNonQuery ();
myTran.Commit ();
}
catch(Exception ee)
{
try
{
myTran.Rollback();
MessageBox.Show ("rollback over");
}
catch (OracleException ex)
{
if (myTran.Connection != null)
{
MessageBox.Show ("在回滚时发生 " + ex.GetType() +" 异常!",
"警告",System.Windows.Forms.MessageBoxButtons.OK,
System.Windows.Forms.MessageBoxIcon.Warning);
}
}

MessageBox.Show ("发生" + ee.GetType() +
"异常/n" +"删除功能出错!","警告",System.Windows.Forms.MessageBoxButtons.OK,
System.Windows.Forms.MessageBoxIcon.Warning);
}
finally
{
funcTableConn.Close();
}
}

private void btn_Help_Click(object sender, System.EventArgs e)
{
trv_Function.Nodes.Clear();
InitTreeView(trv_Function.Nodes,ROOT_NODE_VALUE);
trv_Function.ExpandAll();

}
private void btn_Close_Click(object sender, System.EventArgs e)
{
this.Close();
}


private void trv_Function_ItemDrag(object sender, System.Windows.Forms.ItemDragEventArgs e)
{
if(e.Button == MouseButtons.Left)
{
//开始进行"Drag"操作
DoDragDrop ((TreeNode)e.Item,DragDropEffects.Move);
}
}

private void trv_Function_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
{


TreeNode temp = new TreeNode ();
//得到要移动的节点
TreeNode moveNode = (TreeNode)e.Data.GetData (temp.GetType());
//转换坐标为控件treeview的坐标
Point position=new Point(0,0);
position.X = e.X ;
position.Y = e.Y ;
position = trv_Function.PointToClient(position);

//得到移动的目的地的节点
TreeNode aimNode =trv_Function.GetNodeAt(position) ;

if (IsDragEnable(aimNode,moveNode)==true)
{
if (aimNode!=moveNode)
{
TreeNode tempNode= new TreeNode ();
trv_Function.Nodes.Remove(moveNode);
if (aimNode ==null)
{
trv_Function.Nodes.Insert(trv_Function.Nodes.Count,moveNode);
}
else
{
aimNode.Nodes.Add(moveNode);
}

//更新funcDataTable
DataView mydataview = new DataView ();
mydataview.Table= funcDataTable;
mydataview.RowFilter= "功能ID="+ moveNode.Tag.ToString()+"";
//保存要移动的节点的新的父节点ID;
string moveNodeFatherID="0";
foreach ( DataRowView editRow in mydataview)
{
if (aimNode==null)
{
//如果是根节点
moveNodeFatherID="0";
}
else
{
moveNodeFatherID=aimNode.Tag.ToString();
}
editRow["上级功能ID"]=moveNodeFatherID;
}
//聚焦到要移动的节点上
trv_Function.SelectedNode=moveNode;

//更新数据库中的功能表:改变移动节点的父节点字段为新的父节点
funcTableConn.Open();
System.Data.OracleClient.OracleTransaction myTran = funcTableConn.BeginTransaction ();
try
{
OracleCommand cmd = new OracleCommand ();
cmd.CommandText ="UPDATE 功能 set 上级功能ID='"+moveNodeFatherID +"' where 功能ID = '"+moveNode.Tag.ToString()+"'";
cmd.Transaction = myTran;
cmd.Connection =funcTableConn;
cmd.ExecuteNonQuery ();
myTran.Commit ();
}
catch(Exception ee)
{
try
{
myTran.Rollback();
}
catch (OracleException ex)
{
if (myTran.Connection != null)
{
MessageBox.Show ("在回滚时发生 " + ex.GetType() +" 异常!",
"警告",System.Windows.Forms.MessageBoxButtons.OK,
System.Windows.Forms.MessageBoxIcon.Warning);
}
}

MessageBox.Show ("发生" + ee.GetType() +
"异常/n" +"修改记录失败!","警告",System.Windows.Forms.MessageBoxButtons.OK,
System.Windows.Forms.MessageBoxIcon.Warning);
}
finally
{
funcTableConn.Close();
}
//数据库更新完毕!
}
}

}

private void trv_Function_DragEnter(object sender, System.Windows.Forms.DragEventArgs e)
{
e.Effect = DragDropEffects.Move;
}

private void trv_Function_DragOver(object sender, System.Windows.Forms.DragEventArgs e)
{
Point position=new Point(0,0);
position.X = e.X ;
position.Y = e.Y ;
position = trv_Function.PointToClient ( position ) ;
TreeNode dropNode = trv_Function.GetNodeAt ( position ) ;
trv_Function.SelectedNode=dropNode ;
trv_Function.Focus();
}

/// <summary>
/// 判断是否可以拖动动目标节点,如果可以则返回true,否则为false;
/// 判断根据是:目标节点不能是被拖动的节点的父亲节点!
/// </summary>
private bool IsDragEnable( TreeNode aimNode,TreeNode oriNode)
{
while (aimNode!=null)
{
if (aimNode.Parent !=oriNode)
{
aimNode = aimNode.Parent;
IsDragEnable( aimNode,oriNode);
}
else
{
return false;
}
}
return true;
}


}
}
C#中TreeView类操作全攻略(二)
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using com.prm.client.tools;
using System.Data.OracleClient;
//using com.prm.client.common;

namespace com.prm.client.forms
{
/// <summary>
/// SM_AddNewFunction 的摘要说明。
/// </summary>
public class SM_AddNewFunction: System.Windows.Forms.Form
{
private System.Windows.Forms.Label lbl_Name;
private System.Windows.Forms.TextBox txt_Name;
private System.Windows.Forms.Label lbl_Tag;
private System.Windows.Forms.TextBox txt_Tag;
private System.Windows.Forms.CheckBox ckb_IsInterface;
private System.Windows.Forms.Label lbl_IsInterface;
private System.Windows.Forms.Label lbl_Type;
private System.Windows.Forms.Label lbl_FunEntity;
private System.Windows.Forms.ComboBox cbo_Type;
private System.Windows.Forms.TextBox txt_FunEntity;
private System.Windows.Forms.Label lbl_BelongTo;
private System.Windows.Forms.TextBox txt_BelongTo;
private System.Windows.Forms.TextBox txt_Detail;
private System.Windows.Forms.Label lbl_Detail;
private System.Windows.Forms.Button btn_Confirm;
private System.Windows.Forms.Button btn_Cancel;
private System.Windows.Forms.Button btn_Help;


//用来保存数字字典功能类别的数据
ClientDictionary[] DIC_funcType;
//用来保存Connection属性
private OracleConnection funcTableConn;
//用来保存新增加的记录的主键(功能ID)
private long newFuncID;
//只读属性,用来读取新增加的记录的主键(功能ID)
//public long NewFuncID {get {return newFuncID;}}
//用来保存新增加的功能记录
private FunctionTable newFuncItem;
public FunctionTable NewFuncItem {get {return newFuncItem;}}

/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;

/// <summary>
/// 构造函数,通过参数dic_funcType对窗口进行初始化值,通过参数fatherID得到
/// 新增加的功能的上级功能ID
/// </summary>
public SM_AddNewFunction(long fatherID,ClientDictionary[] dic_funcType)
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
newFuncItem=new FunctionTable();
newFuncItem.funcFatherID=fatherID;
//初始化ID为-1,如果新增成功则将重新赋值
newFuncItem.funcID=-1;
DIC_funcType=dic_funcType;
const string VALUEMEMBER="valueMember",DISPLAYMEMBER="displayMember",NULLTEXT="";
cbo_Type.DataSource = DIC_funcType;
cbo_Type.ValueMember = VALUEMEMBER;
cbo_Type.DisplayMember = DISPLAYMEMBER;

DataAccessObject funcTableAccessObject=new DataAccessObject();
funcTableConn=funcTableAccessObject.Connection;
}

/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(SM_AddNewFunction));
this.lbl_Name = new System.Windows.Forms.Label();
this.lbl_Tag = new System.Windows.Forms.Label();
this.lbl_Type = new System.Windows.Forms.Label();
this.lbl_FunEntity = new System.Windows.Forms.Label();
this.lbl_BelongTo = new System.Windows.Forms.Label();
this.txt_Name = new System.Windows.Forms.TextBox();
this.txt_Tag = new System.Windows.Forms.TextBox();
this.cbo_Type = new System.Windows.Forms.ComboBox();
this.txt_BelongTo = new System.Windows.Forms.TextBox();
this.txt_Detail = new System.Windows.Forms.TextBox();
this.lbl_Detail = new System.Windows.Forms.Label();
this.ckb_IsInterface = new System.Windows.Forms.CheckBox();
this.btn_Confirm = new System.Windows.Forms.Button();
this.btn_Cancel = new System.Windows.Forms.Button();
this.btn_Help = new System.Windows.Forms.Button();
this.lbl_IsInterface = new System.Windows.Forms.Label();
this.txt_FunEntity = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// lbl_Name
//
this.lbl_Name.Location = new System.Drawing.Point(56, 24);
this.lbl_Name.Name = "lbl_Name";
this.lbl_Name.Size = new System.Drawing.Size(48, 23);
this.lbl_Name.TabIndex = 0;
this.lbl_Name.Text = "名称:";
//
// lbl_Tag
//
this.lbl_Tag.Location = new System.Drawing.Point(56, 56);
this.lbl_Tag.Name = "lbl_Tag";
this.lbl_Tag.Size = new System.Drawing.Size(48, 23);
this.lbl_Tag.TabIndex = 1;
this.lbl_Tag.Text = "标识:";
//
// lbl_Type
//
this.lbl_Type.Location = new System.Drawing.Point(56, 112);
this.lbl_Type.Name = "lbl_Type";
this.lbl_Type.Size = new System.Drawing.Size(48, 23);
this.lbl_Type.TabIndex = 2;
this.lbl_Type.Text = "类别:";
//
// lbl_FunEntity
//
this.lbl_FunEntity.Location = new System.Drawing.Point(32, 144);
this.lbl_FunEntity.Name = "lbl_FunEntity";
this.lbl_FunEntity.Size = new System.Drawing.Size(72, 23);
this.lbl_FunEntity.TabIndex = 3;
this.lbl_FunEntity.Text = " 功能体:";
//
// lbl_BelongTo
//
this.lbl_BelongTo.Location = new System.Drawing.Point(8, 176);
this.lbl_BelongTo.Name = "lbl_BelongTo";
this.lbl_BelongTo.Size = new System.Drawing.Size(96, 23);
this.lbl_BelongTo.TabIndex = 4;
this.lbl_BelongTo.Text = "所属窗口标识:";
//
// txt_Name
//
this.txt_Name.ForeColor = System.Drawing.Color.FromArgb(((System.Byte)(30)), ((System.Byte)(66)), ((System.Byte)(94)));
this.txt_Name.Location = new System.Drawing.Point(104, 16);
this.txt_Name.Name = "txt_Name";
this.txt_Name.Size = new System.Drawing.Size(152, 21);
this.txt_Name.TabIndex = 5;
this.txt_Name.Text = "";
//
// txt_Tag
//
this.txt_Tag.ForeColor = System.Drawing.Color.FromArgb(((System.Byte)(30)), ((System.Byte)(66)), ((System.Byte)(94)));
this.txt_Tag.Location = new System.Drawing.Point(104, 48);
this.txt_Tag.Name = "txt_Tag";
this.txt_Tag.Size = new System.Drawing.Size(152, 21);
this.txt_Tag.TabIndex = 6;
this.txt_Tag.Text = "";
//
// cbo_Type
//
this.cbo_Type.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.cbo_Type.ForeColor = System.Drawing.Color.FromArgb(((System.Byte)(30)), ((System.Byte)(66)), ((System.Byte)(94)));
this.cbo_Type.Location = new System.Drawing.Point(104, 104);
this.cbo_Type.Name = "cbo_Type";
this.cbo_Type.Size = new System.Drawing.Size(152, 20);
this.cbo_Type.TabIndex = 7;
//
// txt_BelongTo
//
this.txt_BelongTo.ForeColor = System.Drawing.Color.FromArgb(((System.Byte)(30)), ((System.Byte)(66)), ((System.Byte)(94)));
this.txt_BelongTo.Location = new System.Drawing.Point(104, 168);
this.txt_BelongTo.Name = "txt_BelongTo";
this.txt_BelongTo.Size = new System.Drawing.Size(152, 21);
this.txt_BelongTo.TabIndex = 8;
this.txt_BelongTo.Text = "";
//
// txt_Detail
//
this.txt_Detail.ForeColor = System.Drawing.Color.FromArgb(((System.Byte)(30)), ((System.Byte)(66)), ((System.Byte)(94)));
this.txt_Detail.Location = new System.Drawing.Point(104, 200);
this.txt_Detail.Name = "txt_Detail";
this.txt_Detail.Size = new System.Drawing.Size(152, 21);
this.txt_Detail.TabIndex = 9;
this.txt_Detail.Text = "";
//
// lbl_Detail
//
this.lbl_Detail.Location = new System.Drawing.Point(56, 208);
this.lbl_Detail.Name = "lbl_Detail";
this.lbl_Detail.Size = new System.Drawing.Size(48, 23);
this.lbl_Detail.TabIndex = 10;
this.lbl_Detail.Text = "描述:";
//
// ckb_IsInterface
//
this.ckb_IsInterface.Location = new System.Drawing.Point(104, 72);
this.ckb_IsInterface.Name = "ckb_IsInterface";
this.ckb_IsInterface.Size = new System.Drawing.Size(24, 24);
this.ckb_IsInterface.TabIndex = 11;
//
// btn_Confirm
//
this.btn_Confirm.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("btn_Confirm.BackgroundImage")));
this.btn_Confirm.ForeColor = System.Drawing.Color.FromArgb(((System.Byte)(30)), ((System.Byte)(66)), ((System.Byte)(94)));
this.btn_Confirm.Location = new System.Drawing.Point(40, 240);
this.btn_Confirm.Name = "btn_Confirm";
this.btn_Confirm.Size = new System.Drawing.Size(64, 22);
this.btn_Confirm.TabIndex = 12;
this.btn_Confirm.Text = "确 定";
this.btn_Confirm.Click += new System.EventHandler(this.btn_Confirm_Click);
//
// btn_Cancel
//
this.btn_Cancel.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("btn_Cancel.BackgroundImage")));
this.btn_Cancel.ForeColor = System.Drawing.Color.FromArgb(((System.Byte)(30)), ((System.Byte)(66)), ((System.Byte)(94)));
this.btn_Cancel.Location = new System.Drawing.Point(128, 240);
this.btn_Cancel.Name = "btn_Cancel";
this.btn_Cancel.Size = new System.Drawing.Size(64, 22);
this.btn_Cancel.TabIndex = 13;
this.btn_Cancel.Text = "取 消";
this.btn_Cancel.Click += new System.EventHandler(this.btn_Cancel_Click);
//
// btn_Help
//
this.btn_Help.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("btn_Help.BackgroundImage")));
this.btn_Help.ForeColor = System.Drawing.Color.FromArgb(((System.Byte)(30)), ((System.Byte)(66)), ((System.Byte)(94)));
this.btn_Help.Location = new System.Drawing.Point(216, 240);
this.btn_Help.Name = "btn_Help";
this.btn_Help.Size = new System.Drawing.Size(64, 22);
this.btn_Help.TabIndex = 14;
this.btn_Help.Text = "帮 助";
//
// lbl_IsInterface
//
this.lbl_IsInterface.Location = new System.Drawing.Point(32, 80);
this.lbl_IsInterface.Name = "lbl_IsInterface";
this.lbl_IsInterface.Size = new System.Drawing.Size(72, 23);
this.lbl_IsInterface.TabIndex = 15;
this.lbl_IsInterface.Text = "有无界面:";
//
// txt_FunEntity
//
this.txt_FunEntity.ForeColor = System.Drawing.Color.FromArgb(((System.Byte)(30)), ((System.Byte)(66)), ((System.Byte)(94)));
this.txt_FunEntity.Location = new System.Drawing.Point(104, 136);
this.txt_FunEntity.Name = "txt_FunEntity";
this.txt_FunEntity.Size = new System.Drawing.Size(152, 21);
this.txt_FunEntity.TabIndex = 16;
this.txt_FunEntity.Text = "";
//
// SM_AddNewFunction
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(242)), ((System.Byte)(247)), ((System.Byte)(250)));
this.ClientSize = new System.Drawing.Size(320, 273);
this.Controls.Add(this.txt_FunEntity);
this.Controls.Add(this.lbl_IsInterface);
this.Controls.Add(this.btn_Help);
this.Controls.Add(this.btn_Cancel);
this.Controls.Add(this.btn_Confirm);
this.Controls.Add(this.ckb_IsInterface);
this.Controls.Add(this.lbl_Detail);
this.Controls.Add(this.txt_Detail);
this.Controls.Add(this.txt_BelongTo);
this.Controls.Add(this.cbo_Type);
this.Controls.Add(this.txt_Tag);
this.Controls.Add(this.txt_Name);
this.Controls.Add(this.lbl_BelongTo);
this.Controls.Add(this.lbl_FunEntity);
this.Controls.Add(this.lbl_Type);
this.Controls.Add(this.lbl_Tag);
this.Controls.Add(this.lbl_Name);
this.ForeColor = System.Drawing.Color.FromArgb(((System.Byte)(30)), ((System.Byte)(66)), ((System.Byte)(94)));
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.Name = "SM_AddNewFunction";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "新增功能";
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// 按确定按钮后,先检查输入的数据的合法性;
/// 接着根据输入对变量newFuncItem进行相应的赋值,然后调用InsertFuncItem过程在功能表中增加一条记录
/// </summary>
private void btn_Confirm_Click(object sender, System.EventArgs e)
{
if(txt_Name.Text=="")
{
MessageBox.Show("功能名称不能为空");
return;
}
if(txt_Tag.Text=="")
{
MessageBox.Show("功能标识不能为空");
return;
}
if(this.txt_FunEntity.Text=="")
{
MessageBox.Show("功能体不能为空");
return;
}
newFuncItem.funcName=txt_Name.Text.Trim();
newFuncItem.funcTag=txt_Tag.Text.Trim();
if(this.ckb_IsInterface.Checked==true)
{
newFuncItem.funcIsInterface="1";
}
else
{
newFuncItem.funcIsInterface="0";
}
newFuncItem.funcID=QueryNextFuncID();
//如果新生成的ID为0则表示没有成功自动生成新ID,应该返回,不再执行增加操作。
if(newFuncItem.funcID==0) return;
newFuncItem.funcType=this.cbo_Type.SelectedValue.ToString().Trim();
newFuncItem.funcEntity=this.txt_FunEntity.Text.Trim();
newFuncItem.funcBelongTo=this.txt_BelongTo.Text.Trim();
newFuncItem.funcDetail=this.txt_Detail.Text.Trim();
newFuncItem.funcIsExpand="";
newFuncItem.funcVer="";
InsertFuncItem(newFuncItem);
this.Close();

}

/// <summary>
///输出自动生成的ID号,如果生成失败则输出0
/// </summary>
private long QueryNextFuncID()
{
funcTableConn.Open ();
OracleCommand mySelectCmd= new OracleCommand();
mySelectCmd.Connection = funcTableConn;
mySelectCmd.CommandText = "select SEQ_功能.nextval from dual";
mySelectCmd.CommandType = CommandType.Text ;
long nextID=0;
try
{
OracleDataReader myReader = mySelectCmd.ExecuteReader();
myReader.Read();
nextID= myReader.GetInt64(0);
myReader.Close();
}
catch(Exception ex)
{
MessageBox.Show("查询时出现错误:"+ex.ToString());
}
finally
{
funcTableConn.Close ( ) ;
}
return nextID;
}

/// <summary>
/// 在功能表中插入一条新记录,利用了回滚功能,即如果插入不成功则功能表
/// 恢复为插入前的状态
/// </summary>
/// <param name="newItem">要插入的新纪录的信息</param>
private void InsertFuncItem(FunctionTable newItem)
{
funcTableConn.Open();
System.Data.OracleClient.OracleTransaction myTran = funcTableConn.BeginTransaction ();
try
{
OracleCommand cmd = new OracleCommand ();

cmd.CommandText = "INSERT INTO 功能 (功能ID,功能标识,功能名称,功能简述, 所属窗体,有无界面,功能类别,功能体,上级功能ID,可否展显,版本号) values ('"
+newItem.funcID+"', '"
+newItem.funcTag+"', '"
+newItem.funcName+"', '"
+newItem.funcDetail+"', '"
+newItem.funcBelongTo+"', '"
+newItem.funcIsInterface+"', '"
+newItem.funcType+"', '"
+newItem.funcEntity+"', '"
+newItem.funcFatherID+"', '"
+newItem.funcIsExpand+"', '"
+newItem.funcVer+"')";
cmd.Transaction = myTran;
cmd.Connection =funcTableConn;
cmd.ExecuteNonQuery ();
myTran.Commit ();
}
catch(Exception ee)
{
try
{
myTran.Rollback();
}
catch (OracleException ex)
{
if (myTran.Connection != null)
{
MessageBox.Show ("在回滚时发生 " + ex.GetType() +" 异常!",
"警告",System.Windows.Forms.MessageBoxButtons.OK,
System.Windows.Forms.MessageBoxIcon.Warning);
}
}

MessageBox.Show ("发生" + ee.GetType() +
"异常/n" +"没有添加任何记录!","警告",System.Windows.Forms.MessageBoxButtons.OK,
System.Windows.Forms.MessageBoxIcon.Warning);
}
finally
{
funcTableConn.Close();
}
}

private void btn_Cancel_Click(object sender, System.EventArgs e)
{
//如果点击取消的话则newFuncItem.funcID赋值为-1
newFuncItem.funcID=-1;
this.Close();
}

}
}
C#中TreeView类操作全攻略(三)
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using com.prm.client.tools;
using System.Data.OracleClient;
//using com.prm.client.common;

namespace com.prm.client.forms
{
/// <summary>
/// SM_EditFunction 的摘要说明。
/// </summary>
public class SM_EditFunction: System.Windows.Forms.Form
{
private System.Windows.Forms.Label lbl_Name;
private System.Windows.Forms.TextBox txt_Name;
private System.Windows.Forms.Label lbl_Tag;
private System.Windows.Forms.TextBox txt_Tag;
private System.Windows.Forms.CheckBox ckb_IsInterface;
private System.Windows.Forms.Label lbl_IsInterface;
private System.Windows.Forms.Label lbl_Type;
private System.Windows.Forms.Label lbl_FunEntity;
private System.Windows.Forms.ComboBox cbo_Type;
private System.Windows.Forms.TextBox txt_FunEntity;
private System.Windows.Forms.Label lbl_BelongTo;
private System.Windows.Forms.TextBox txt_BelongTo;
private System.Windows.Forms.TextBox txt_Detail;
private System.Windows.Forms.Label lbl_Detail;
private System.Windows.Forms.Button btn_Confirm;
private System.Windows.Forms.Button btn_Cancel;
private System.Windows.Forms.Button btn_Help;

//用来保存原始的功能记录
private FunctionTable oriFuncItem;

//用来保存数字字典功能类别的数据
ClientDictionary[] DIC_funcType;
//用来保存Connection属性
private OracleConnection funcTableConn;
//用来保存修改后的记录
private FunctionTable newFuncItem;
public FunctionTable NewFuncItem {get {return newFuncItem;}}
//用来判断是否用户进行了修改
private bool isEdit=false;
public bool IsEdit {get {return isEdit;}}
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;

/// <summary>
/// 构造函数,通过editFuncItem参数和dic_funcType对窗口进行初始化值
/// </summary>
public SM_EditFunction(FunctionTable editFuncItem,ClientDictionary[] dic_funcType)
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//用参数值对本窗口相应的变量进行赋值保存
oriFuncItem=editFuncItem;
DIC_funcType=dic_funcType;
const string VALUEMEMBER="valueMember",DISPLAYMEMBER="displayMember",NULLTEXT="";
cbo_Type.DataSource = DIC_funcType;
cbo_Type.ValueMember = VALUEMEMBER;
cbo_Type.DisplayMember = DISPLAYMEMBER;

DataAccessObject funcTableAccessObject=new DataAccessObject();
funcTableConn=funcTableAccessObject.Connection;

this.txt_Name.Text=oriFuncItem.funcName;
this.txt_Tag.Text=oriFuncItem.funcTag;
if (oriFuncItem.funcIsInterface =="0")
{
this.ckb_IsInterface.Checked=false;
}
else
{
this.ckb_IsInterface.Checked=true;
}
this.cbo_Type.SelectedValue=oriFuncItem.funcType;
this.txt_FunEntity.Text=oriFuncItem.funcEntity;
this.txt_BelongTo.Text=oriFuncItem.funcBelongTo;
this.txt_Detail.Text=oriFuncItem.funcDetail;

}

/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(SM_EditFunction));
this.lbl_Name = new System.Windows.Forms.Label();
this.lbl_Tag = new System.Windows.Forms.Label();
this.lbl_Type = new System.Windows.Forms.Label();
this.lbl_FunEntity = new System.Windows.Forms.Label();
this.lbl_BelongTo = new System.Windows.Forms.Label();
this.txt_Name = new System.Windows.Forms.TextBox();
this.txt_Tag = new System.Windows.Forms.TextBox();
this.cbo_Type = new System.Windows.Forms.ComboBox();
this.txt_BelongTo = new System.Windows.Forms.TextBox();
this.txt_Detail = new System.Windows.Forms.TextBox();
this.lbl_Detail = new System.Windows.Forms.Label();
this.ckb_IsInterface = new System.Windows.Forms.CheckBox();
this.btn_Confirm = new System.Windows.Forms.Button();
this.btn_Cancel = new System.Windows.Forms.Button();
this.btn_Help = new System.Windows.Forms.Button();
this.lbl_IsInterface = new System.Windows.Forms.Label();
this.txt_FunEntity = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// lbl_Name
//
this.lbl_Name.Location = new System.Drawing.Point(56, 24);
this.lbl_Name.Name = "lbl_Name";
this.lbl_Name.Size = new System.Drawing.Size(48, 23);
this.lbl_Name.TabIndex = 0;
this.lbl_Name.Text = "名称:";
//
// lbl_Tag
//
this.lbl_Tag.Location = new System.Drawing.Point(56, 56);
this.lbl_Tag.Name = "lbl_Tag";
this.lbl_Tag.Size = new System.Drawing.Size(48, 23);
this.lbl_Tag.TabIndex = 1;
this.lbl_Tag.Text = "标识:";
//
// lbl_Type
//
this.lbl_Type.Location = new System.Drawing.Point(56, 112);
this.lbl_Type.Name = "lbl_Type";
this.lbl_Type.Size = new System.Drawing.Size(48, 23);
this.lbl_Type.TabIndex = 2;
this.lbl_Type.Text = "类别:";
//
// lbl_FunEntity
//
this.lbl_FunEntity.Location = new System.Drawing.Point(32, 144);
this.lbl_FunEntity.Name = "lbl_FunEntity";
this.lbl_FunEntity.Size = new System.Drawing.Size(72, 23);
this.lbl_FunEntity.TabIndex = 3;
this.lbl_FunEntity.Text = " 功能体:";
//
// lbl_BelongTo
//
this.lbl_BelongTo.Location = new System.Drawing.Point(8, 176);
this.lbl_BelongTo.Name = "lbl_BelongTo";
this.lbl_BelongTo.Size = new System.Drawing.Size(96, 23);
this.lbl_BelongTo.TabIndex = 4;
this.lbl_BelongTo.Text = "所属窗口标识:";
//
// txt_Name
//
this.txt_Name.ForeColor = System.Drawing.Color.FromArgb(((System.Byte)(30)), ((System.Byte)(66)), ((System.Byte)(94)));
this.txt_Name.Location = new System.Drawing.Point(104, 16);
this.txt_Name.Name = "txt_Name";
this.txt_Name.Size = new System.Drawing.Size(152, 21);
this.txt_Name.TabIndex = 5;
this.txt_Name.Text = "";
//
// txt_Tag
//
this.txt_Tag.ForeColor = System.Drawing.Color.FromArgb(((System.Byte)(30)), ((System.Byte)(66)), ((System.Byte)(94)));
this.txt_Tag.Location = new System.Drawing.Point(104, 48);
this.txt_Tag.Name = "txt_Tag";
this.txt_Tag.Size = new System.Drawing.Size(152, 21);
this.txt_Tag.TabIndex = 6;
this.txt_Tag.Text = "";
//
// cbo_Type
//
this.cbo_Type.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.cbo_Type.ForeColor = System.Drawing.Color.FromArgb(((System.Byte)(30)), ((System.Byte)(66)), ((System.Byte)(94)));
this.cbo_Type.Location = new System.Drawing.Point(104, 104);
this.cbo_Type.Name = "cbo_Type";
this.cbo_Type.Size = new System.Drawing.Size(152, 20);
this.cbo_Type.TabIndex = 7;
//
// txt_BelongTo
//
this.txt_BelongTo.ForeColor = System.Drawing.Color.FromArgb(((System.Byte)(30)), ((System.Byte)(66)), ((System.Byte)(94)));
this.txt_BelongTo.Location = new System.Drawing.Point(104, 168);
this.txt_BelongTo.Name = "txt_BelongTo";
this.txt_BelongTo.Size = new System.Drawing.Size(152, 21);
this.txt_BelongTo.TabIndex = 8;
this.txt_BelongTo.Text = "";
//
// txt_Detail
//
this.txt_Detail.ForeColor = System.Drawing.Color.FromArgb(((System.Byte)(30)), ((System.Byte)(66)), ((System.Byte)(94)));
this.txt_Detail.Location = new System.Drawing.Point(104, 200);
this.txt_Detail.Name = "txt_Detail";
this.txt_Detail.Size = new System.Drawing.Size(152, 21);
this.txt_Detail.TabIndex = 9;
this.txt_Detail.Text = "";
//
// lbl_Detail
//
this.lbl_Detail.Location = new System.Drawing.Point(56, 208);
this.lbl_Detail.Name = "lbl_Detail";
this.lbl_Detail.Size = new System.Drawing.Size(48, 23);
this.lbl_Detail.TabIndex = 10;
this.lbl_Detail.Text = "描述:";
//
// ckb_IsInterface
//
this.ckb_IsInterface.Location = new System.Drawing.Point(104, 72);
this.ckb_IsInterface.Name = "ckb_IsInterface";
this.ckb_IsInterface.Size = new System.Drawing.Size(24, 24);
this.ckb_IsInterface.TabIndex = 11;
//
// btn_Confirm
//
this.btn_Confirm.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("btn_Confirm.BackgroundImage")));
this.btn_Confirm.Location = new System.Drawing.Point(40, 240);
this.btn_Confirm.Name = "btn_Confirm";
this.btn_Confirm.Size = new System.Drawing.Size(64, 22);
this.btn_Confirm.TabIndex = 12;
this.btn_Confirm.Text = "确 定";
this.btn_Confirm.Click += new System.EventHandler(this.btn_Confirm_Click);
//
// btn_Cancel
//
this.btn_Cancel.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("btn_Cancel.BackgroundImage")));
this.btn_Cancel.Location = new System.Drawing.Point(128, 240);
this.btn_Cancel.Name = "btn_Cancel";
this.btn_Cancel.Size = new System.Drawing.Size(64, 22);
this.btn_Cancel.TabIndex = 13;
this.btn_Cancel.Text = "取 消";
this.btn_Cancel.Click += new System.EventHandler(this.btn_Cancel_Click);
//
// btn_Help
//
this.btn_Help.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("btn_Help.BackgroundImage")));
this.btn_Help.Location = new System.Drawing.Point(216, 240);
this.btn_Help.Name = "btn_Help";
this.btn_Help.Size = new System.Drawing.Size(64, 22);
this.btn_Help.TabIndex = 14;
this.btn_Help.Text = "帮 助";
//
// lbl_IsInterface
//
this.lbl_IsInterface.Location = new System.Drawing.Point(32, 80);
this.lbl_IsInterface.Name = "lbl_IsInterface";
this.lbl_IsInterface.Size = new System.Drawing.Size(72, 23);
this.lbl_IsInterface.TabIndex = 15;
this.lbl_IsInterface.Text = "有无界面:";
//
// txt_FunEntity
//
this.txt_FunEntity.ForeColor = System.Drawing.Color.FromArgb(((System.Byte)(30)), ((System.Byte)(66)), ((System.Byte)(94)));
this.txt_FunEntity.Location = new System.Drawing.Point(104, 136);
this.txt_FunEntity.Name = "txt_FunEntity";
this.txt_FunEntity.Size = new System.Drawing.Size(152, 21);
this.txt_FunEntity.TabIndex = 16;
this.txt_FunEntity.Text = "";
//
// SM_EditFunction
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(242)), ((System.Byte)(247)), ((System.Byte)(250)));
this.ClientSize = new System.Drawing.Size(320, 273);
this.Controls.Add(this.txt_FunEntity);
this.Controls.Add(this.lbl_IsInterface);
this.Controls.Add(this.btn_Help);
this.Controls.Add(this.btn_Cancel);
this.Controls.Add(this.btn_Confirm);
this.Controls.Add(this.ckb_IsInterface);
this.Controls.Add(this.lbl_Detail);
this.Controls.Add(this.txt_Detail);
this.Controls.Add(this.txt_BelongTo);
this.Controls.Add(this.cbo_Type);
this.Controls.Add(this.txt_Tag);
this.Controls.Add(this.txt_Name);
this.Controls.Add(this.lbl_BelongTo);
this.Controls.Add(this.lbl_FunEntity);
this.Controls.Add(this.lbl_Type);
this.Controls.Add(this.lbl_Tag);
this.Controls.Add(this.lbl_Name);
this.ForeColor = System.Drawing.Color.FromArgb(((System.Byte)(30)), ((System.Byte)(66)), ((System.Byte)(94)));
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.Name = "SM_EditFunction";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "修改功能";
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// 按确定按钮后,先检查输入的数据的合法性;接着生成一个FunctionTable类的实例
/// 并根据输入进行 相应的赋值,接着调用UpdateFuncItem过程对功能表进行相应的修改
/// </summary>
private void btn_Confirm_Click(object sender, System.EventArgs e)
{
if(txt_Name.Text=="")
{
MessageBox.Show("功能名称不能为空");
return;
}
if(txt_Tag.Text=="")
{
MessageBox.Show("功能标识不能为空");
return;
}
if(this.txt_FunEntity.Text=="")
{
MessageBox.Show("功能体不能为空");
return;
}
newFuncItem=new FunctionTable();
newFuncItem.funcName=txt_Name.Text.Trim();
newFuncItem.funcTag=txt_Tag.Text.Trim();
if(this.ckb_IsInterface.Checked==true)
{
newFuncItem.funcIsInterface="1";
}
else
{
newFuncItem.funcIsInterface="0";
}
newFuncItem.funcType=this.cbo_Type.SelectedValue.ToString().Trim();
newFuncItem.funcEntity=this.txt_FunEntity.Text.Trim();
newFuncItem.funcBelongTo=this.txt_BelongTo.Text.Trim();
newFuncItem.funcDetail=this.txt_Detail.Text.Trim();
newFuncItem.funcIsExpand=oriFuncItem.funcIsExpand;
newFuncItem.funcVer=oriFuncItem.funcVer;
newFuncItem.funcID=oriFuncItem.funcID;
newFuncItem.funcFatherID=oriFuncItem.funcFatherID;
UpdateFuncItem(newFuncItem);
isEdit=true;
this.Close();

}

/// <summary>
/// 对功能表进行修改,利用了回滚功能,即如果修改不成功则功能表
/// 恢复为修改前的状态
/// </summary>
/// <param name="newItem">要修改的新的纪录信息</param>
private void UpdateFuncItem(FunctionTable newItem)
{
funcTableConn.Open();
System.Data.OracleClient.OracleTransaction myTran = funcTableConn.BeginTransaction ();
try
{
OracleCommand cmd = new OracleCommand ();
cmd.CommandText ="UPDATE 功能 set 功能标识='"+newItem.funcTag
+"', 功能名称='"+newItem.funcName
+"', 功能简述='"+newItem.funcDetail
+"', 所属窗体='"+newItem.funcBelongTo
+"', 有无界面='"+newItem.funcIsInterface
+"', 功能类别='"+newItem.funcType
+"', 功能体='"+newItem.funcEntity
+"', 上级功能ID='"+newItem.funcFatherID
+"', 可否展显='"+newItem.funcIsExpand
+"', 版本号='"+newItem.funcVer
+"' where 功能ID = '"+newItem.funcID+"'";
cmd.Transaction = myTran;
cmd.Connection =funcTableConn;
cmd.ExecuteNonQuery ();
myTran.Commit ();
}
catch(Exception ee)
{
try
{
myTran.Rollback();
}
catch (OracleException ex)
{
if (myTran.Connection != null)
{
MessageBox.Show ("在回滚时发生 " + ex.GetType() +" 异常!",
"警告",System.Windows.Forms.MessageBoxButtons.OK,
System.Windows.Forms.MessageBoxIcon.Warning);
}
}

MessageBox.Show ("发生" + ee.GetType() +
"异常/n" +"修改记录失败!","警告",System.Windows.Forms.MessageBoxButtons.OK,
System.Windows.Forms.MessageBoxIcon.Warning);
}
finally
{
funcTableConn.Close();
}
}

private void btn_Cancel_Click(object sender, System.EventArgs e)
{
//用户点击了取消键,则表示没有进行修改
isEdit=false;
this.Close();
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值