原创|其它|编辑:郝浩|2012-10-09 16:37:42.000|阅读 3172 次
概述:本文总结了Deverpress TreeList控件的使用方法。
# 慧都年终大促·界面/图表报表/文档/IDE等千款热门软控件火热促销中 >>
方法一:此事件用于在tree之前设置显示的图片,具体使用方法是首先在窗体上放置imagelist,设置好图片,即可在事件中判断后,选择要显示的图片的索引值即可。
private void treeList_GetStateImage(object sender, DevExpress.XtraTreeList.GetStateImageEventArgs e) { CheckState check = GetCheckState(e.Node.Tag); if(check == CheckState.Unchecked) e.NodeImageIndex = 0; else if(check == CheckState.Checked) e.NodeImageIndex = 1; else e.NodeImageIndex = 2; }
方法二:为tree中指定的节点区分现实不同的图片。
事例代码如下:
private void roleTree_GetStateImage(object sender, DevExpress.XtraTreeList.GetStateImageEventArgs e) { if(e.Node.Id.ToString() == "0") { this.roleTree.Nodes[0].RootNode.StateImageIndex = 1; } else { e.NodeImageIndex = 0; } }
提取tree中的值所用事件为MouseDown,在事件中填写如下代码:
private void roleTree_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) { if(e.Button == MouseButtons.Left) { DevExpress.XtraTreeList.TreeListHitInfo hInfo = roleTree.GetHitInfo(new Point(e.X, e.Y)); if(hInfo.HitInfoType == DevExpress.XtraTreeList.HitInfoType.StateImage) { // this.roleTree.DeleteNode(hInfo.Node); if(MessageBox.Show("你确定要删除"+hInfo.Node[1].ToString()+"角色","提示信 息",MessageBoxButtons.YesNo,MessageBoxIcon.Question)==DialogResult.Yes) { TB_Accounts_RolesEntity role = new TB_Accounts_RolesEntity(); role.iRoleID = Convert.ToInt32(hInfo.Node[1].ToString()); role.Retrieve(); role.Delete(); InitRoleTree(); } else {} } else if(hInfo.HitInfoType == DevExpress.XtraTreeList.HitInfoType.Cell) { // MessageBox.Show(hInfo.Node[1].ToString()); //获取选择的iRoleID // SetCheckedNode(treeList.Nodes[0].Nodes[0]); //得到指定iRoleID的全部的vDescript的允许的Table string roleID = hInfo.Node[1].ToString(); string sqlString="SELECT dbo.TB_Accounts_Permissions.vDescript FROM dbo.TB_Accounts_Permissions INNER JOIN dbo.TB_Accounts_RolePermissions ON dbo.TB_Accounts_Permissions.iPermissionID = dbo.TB_Accounts_RolePermissions.iPermissionID WHERE 1=1"; //编写SQL语句 sqlString +=" and dbo.TB_Accounts_RolePermissions.iRoleID = '"+roleID+"'"; //执行SQL语句 DataTable dt=Query.ProcessSql(sqlString,"EasySales"); for(int i = 0; i<dt.Rows.Count; i++) { string treeText = treeList.GetNodeByVisibleIndex(i).GetDisplayText(0).ToString(); if(treeText == dt.Rows[i]["vDescript"].ToString()) { SetCheckedNode(treeList.Nodes[0].Nodes[i]); } } /**/////// //判断treeList的行数和DataTable的行数是否一致 ////// if(this.treeList.VisibleNodesCount.ToString()==dt.Rows.Count.ToString()) ////// { ////// ////// //循环treeList的全部的vDescript的值 ////// for(int i = 0; i<Convert.ToInt32(this.treeList.VisibleNodesCount.ToString());i++) ////// { ////// //判断treeList的vDescript的值和Table的vDescipte的值是否相等 ////// if(this.treeList.GetNodeByVisibleIndex(i).GetDisplayText(0).ToString() != dt.Rows[i]["vDescript"].ToString()) ////// { ////// MessageBox.Show("得到不同的vDescript"); //////// Checked(); ////// } ////// else ////// { ////// MessageBox.Show("得到相同的vDescript"); //////// UnChecked(); ////// } ////// } ////// } ////// else ////// { ////// MessageBox.Show("长度不一致"); ////// } } } }
TreeList中并为提供对应实现此功能的CheckBox自定义列,故必须用上面提到的GetStateImage事件添加三个(全选、部分全选、为选中)CheckBox的图片,具体样式可自己设计,然后放到窗体中的ImageList中去,触发及调用用到下面的代码:
private CheckState GetCheckState(object obj) { if(obj != null) return (CheckState)obj; return CheckState.Unchecked; } private void SetCheckedNode(string treeText) { CheckState check = new CheckState(); check = CheckState.Checked; } private void SetCheckedNode(DevExpress.XtraTreeList.Nodes.TreeListNode node) { CheckState check = GetCheckState(node.Tag); if(check == CheckState.Indeterminate || check == CheckState.Unchecked) check = CheckState.Checked; else check = CheckState.Unchecked; treeList.BeginUpdate(); node.Tag = check; SetCheckedChildNodes(node, check); SetCheckedParentNodes(node, check); treeList.EndUpdate(); } private void SetCheckedChildNodes(DevExpress.XtraTreeList.Nodes.TreeListNode node, CheckState check) { for(int i = 0; i < node.Nodes.Count; i++) { node.Nodes[i].Tag = check; SetCheckedChildNodes(node.Nodes[i], check); } } private void SetCheckedParentNodes(DevExpress.XtraTreeList.Nodes.TreeListNode node, CheckState check) { if(node.ParentNode != null) { bool b = false; for(int i = 0; i < node.ParentNode.Nodes.Count; i++) { if(!check.Equals(node.ParentNode.Nodes[i].Tag)) { b = !b; break; } } node.ParentNode.Tag = b ? CheckState.Indeterminate : check; SetCheckedParentNodes(node.ParentNode, check); } } private void treeList_GetStateImage(object sender, DevExpress.XtraTreeList.GetStateImageEventArgs e) { CheckState check = GetCheckState(e.Node.Tag); if(check == CheckState.Unchecked) e.NodeImageIndex = 0; else if(check == CheckState.Checked) e.NodeImageIndex = 1; else e.NodeImageIndex = 2; } private void treeList_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e) { if(e.KeyData == Keys.Space) SetCheckedNode(treeList.FocusedNode); } private void treeList_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) { if(e.Button == MouseButtons.Left) { DevExpress.XtraTreeList.TreeListHitInfo hInfo = treeList.GetHitInfo(new Point(e.X, e.Y)); if(hInfo.HitInfoType == DevExpress.XtraTreeList.HitInfoType.StateImage) SetCheckedNode(hInfo.Node); } } private void roleTree_GetStateImage(object sender, DevExpress.XtraTreeList.GetStateImageEventArgs e) { if(e.Node.Id.ToString() == "0") { this.roleTree.Nodes[0].RootNode.StateImageIndex = 1; } else { e.NodeImageIndex = 0; } }
这样就可实现上述功能。
本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@pclwef.cn
文章转载自:网络转载