欢迎光临
我们一直在努力

C#中TreeView类操作全攻略(一)-.NET教程,C#语言

建站超值云服务器,限时71元/月

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;

}

}

}

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » C#中TreeView类操作全攻略(一)-.NET教程,C#语言
分享到: 更多 (0)

相关推荐

  • 暂无文章