本文介绍了如何在树视图中动态地使特定节点启用false的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有使用复选框动态加载数据库的树视图。加载这个树视图时我想禁用这个树视图的一些节点。当用户点击这些特定节点时,没有动作执行此复选框....我该怎么办?哪个属性使用?

以下是我的代码:





private void LoadTreeview()

{

// KpiBAL objkpibal = new KpiBAL();

DataSet ds = new DataSet();

ds = balkeyrole .GetTreeviewData(Globalclass.Activedatabase.ToString());

foreach(DataRow dr在ds.Tables [Department]。行)

{

TreeNode tn = new TreeNode(dr [DeptName]。ToString());

tn.Tag = dr [DeptID];

foreach( Dr.GetChildRows中的DataRow drChild(Dept_sub))

{

TreeNode tnChild = tn.Nodes.Add(drChild [SubDeptName]。ToString());

tnChild.Tag = drChild [SubDeptId];



foreach(ds.Tables中的DataRow dr1 [SubDepartment]。行)

{

foreach(dr1.GetChildRows中的DataRow drChild1(Sub_KPI))

{

if(drChild [SubDeptId]。ToString()== drChild1 [ SubDeptId]。ToString())

{

TreeNode kpi = tnChild.Nodes.Add(drChild1 [KPIName]。ToString());

kpi.Tag = drChild1 [KPIID];

DataTable Dt = new DataTable();

Dt = balkeyrole.GetKPIID(Globalclass.Activedatabase) ;

foreach(Dt.Rows中的DataRow dr2)

{

if(dr2 [KPIID]。ToString()== kpi .Tag.ToString())

{

//treeViewRoleAssignmentKPI.SelectedNode = kpi;

// ReadOnlyNodeTrue();



treeViewRoleAssignmentKPI.SelectedNode = kpi;

kpi.Checked = true; //这段代码我会尝试禁用但无法工作

kpi.EndEdit(false);

// kpi.EnsureVisible();

// kpi.EndEdit(false);

// ReadOnlyNodeTrue(kpi);

// treeViewRoleAssignmentKPI.CheckBoxes = true;

// HideCheckBox(ref kpi);

}

}

}

}

}

}

treeViewRoleAssignmentKPI.Nodes.Add(tn);

treeViewRoleAssignmentKPI.SelectedNode = treeViewRoleAssignmentKPI .TopNode;

colorTreeViewNodes(treeViewRoleAssignmentKPI.Nodes);

}

}

i have treeview which is loaded from database dynamically with checkbox. when this treeview loaded i want to disable some nodes of this treeview .i.e when user click on these perticular node there is no action perform with this checkbox ....what can i do? which property use?
following is my code:


private void LoadTreeview()
{
//KpiBAL objkpibal = new KpiBAL();
DataSet ds = new DataSet();
ds = balkeyrole.GetTreeviewData(Globalclass.Activedatabase.ToString());
foreach (DataRow dr in ds.Tables["Department"].Rows)
{
TreeNode tn = new TreeNode(dr["DeptName"].ToString());
tn.Tag = dr["DeptID"];
foreach (DataRow drChild in dr.GetChildRows("Dept_sub"))
{
TreeNode tnChild = tn.Nodes.Add(drChild["SubDeptName"].ToString());
tnChild.Tag = drChild["SubDeptId"];

foreach (DataRow dr1 in ds.Tables["SubDepartment"].Rows)
{
foreach (DataRow drChild1 in dr1.GetChildRows("Sub_KPI"))
{
if (drChild["SubDeptId"].ToString() == drChild1["SubDeptId"].ToString())
{
TreeNode kpi = tnChild.Nodes.Add(drChild1["KPIName"].ToString());
kpi.Tag = drChild1["KPIID"];
DataTable Dt = new DataTable();
Dt = balkeyrole.GetKPIID(Globalclass.Activedatabase);
foreach (DataRow dr2 in Dt.Rows )
{
if (dr2["KPIID"].ToString() == kpi.Tag.ToString())
{
//treeViewRoleAssignmentKPI.SelectedNode = kpi;
//ReadOnlyNodeTrue();

treeViewRoleAssignmentKPI.SelectedNode = kpi;
kpi.Checked = true; //this code i will try for disable but cant work
kpi.EndEdit(false);
// kpi.EnsureVisible();
// kpi.EndEdit(false);
//ReadOnlyNodeTrue(kpi);
// treeViewRoleAssignmentKPI.CheckBoxes=true;
//HideCheckBox(ref kpi);
}
}
}
}
}
}
treeViewRoleAssignmentKPI.Nodes.Add(tn);
treeViewRoleAssignmentKPI.SelectedNode = treeViewRoleAssignmentKPI.TopNode;
colorTreeViewNodes(treeViewRoleAssignmentKPI.Nodes);
}
}

推荐答案


// a List to hold TreeNodes with CheckBox disabled
List<TreeNode> DisabledCheckNodes = new List<TreeNode>();

// method to disable CheckBox use in specific TreeNodes
private void SetDisabledCheckNodes(params TreeNode[] theNodes)
{
    DisabledCheckNodes.AddRange(theNodes);
}

// method to re-enable CheckBox use in specific TreeNodes
private void EnableCheckNodes(params TreeNode[] theNodes)
{
    foreach (var theNode in theNodes)
    {
        if (DisabledCheckNodes.Contains(theNode)) DisabledCheckNodes.Remove(theNode);
    }
}

// a flag to control possible recursion in changing
// TreeNode CheckState in a 'CheckChanged EventHandler
private bool dontRecurse = false;

// function to use to try and prevent CheckBox use
private bool StopCheckBoxUse(TreeNode theNode)
{
    if (DisabledCheckNodes.Contains(theNode))
    {
        dontRecurse = true;
        theNode.Checked = false;
        treeView1.Update();
        dontRecurse = false;
        return true;
    }
    else
    {
        return false;
    }
}

// the EventHandlers for the TreeView
private void treeView1_BeforeCheck(object sender, TreeViewCancelEventArgs e)
{
    if (dontRecurse) return;

    e.Cancel = StopCheckBoxUse(e.Node);
}

private void treeView1_AfterCheck(object sender, TreeViewEventArgs e)
{
    if (dontRecurse) return;

    StopCheckBoxUse(e.Node);
}

设置哪些节点已禁用CheckBoxes:

To set which Nodes have disabled CheckBoxes:

SetDisabledCheckNodes(treeView1.Nodes[2],treeView1.Nodes[4],treeView1.Nodes[6]);

要在TreeNode上重新启用CheckBoxes:

To re-enable CheckBoxes on a TreeNode:

EnableCheckNodes(treeView1.Nodes[6]);

现在,坏消息;用户,如果确定,可以通过快速重复单击CheckBox来检查CheckBox,或者取消选中。



我实验(很久以前)定义了每个TreeView事件可能会影响CheckBox状态,所有MouseClicks,MouseDowns,After-Before Select等,并将CheckBox设置为取消选中,但没有成功。我已经得出结论,这种现象是Win Form TreeView的另一个怪癖(另一个原因,其中很多,我转而使用商业Lidor Systems IntegralUI TreeView)。



但是,这段代码是在几年前完成的,最近我还没有研究过是否有任何其他修复可用于在WinForm TreeView的所有条件下禁用CheckBoxes棒。

And now, the bad news; the user, if determined, can get the CheckBox Checked, or unchecked by quickly repeated clicking on the CheckBox.

I experimented (long ago) with defining every TreeView Event that could possibly affect the CheckBox state, all MouseClicks, MouseDowns, After-Before Select, etc., and set the CheckBox to unchecked in them, without success. I have concluded that this phenomenon is yet another quirk of the Win Form TreeView (another reason, among many, I switched to using the commercial Lidor Systems IntegralUI TreeView).

However, this code was done several years ago, and I have not researched, recently, to see if there is any other "fix" available for making disabling the CheckBoxes "stick" under all conditions for the WinForm TreeView.


这篇关于如何在树视图中动态地使特定节点启用false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 20:56