程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> C#中TreeView類操作全攻略(一)

C#中TreeView類操作全攻略(一)

編輯:關於C語言
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;
}


}
}


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved