简体   繁体   中英

Prevent user from altering Check boxes in TreeView

I have a winforms treeview with nodes added and check state set programmatically based on the database values. I am trying to prevent users from altering the check status and am having trouble. I am not sure what event to fire to keep the check state unaltered.

Below is my code:

private void BuildRolesTree(int ParentID, TreeNode pNode, DataSet SourceDS)
    {
        
        DataView dvwData = new DataView(SourceDS.Tables[0]);
        dvwData.RowFilter = "[parent_id] = " + ParentID;
        if (this.InvokeRequired)
        {
            BuildReportTreeDelegate d = new BuildReportTreeDelegate(BuildRolesTree);
            this.Invoke(d, new object[] { ParentID, pNode, SourceDS });
        }
        else
        {
            foreach (DataRowView Row in dvwData)
            {
                TreeNode zNode;

                if (pNode == null)
                    zNode = tv_Permissions.Nodes.Add(Row["node_id"].ToString(), Row["display_access_description"].ToString().Trim());
                else zNode = pNode.Nodes.Add(Row["node_id"].ToString(), Row["display_access_description"].ToString().Trim());

                if (Convert.ToInt32(Row["is_selected"]) == 1)
                    zNode.Checked = true;
                else if (Convert.ToInt32(Row["is_selected"]) == 0)
                    zNode.Checked = false;
                                    


                BuildRolesTree(Convert.ToInt32(Row["node_id"].ToString()), zNode, SourceDS);
            }
        }
    }

    private void PermissionsNode_AfterCheck(object sender, TreeViewEventArgs e)
    {           
       if(e.Action != TreeViewAction.Unknown)
       {
            if(e.Node.Checked) //leave it checked
               e.Node.Checked = e.Node.Checked????
               //I am looking for something like the below
               //e.Checked.NewValue = e.Checked.CurrentValue;
       }    
     }

}
ANy help is appreciated.

You can handle BeforeCheck and set e.Cancel = true to prevent changing the check:

private void treeView1_BeforeCheck(object sender, TreeViewCancelEventArgs e)
{
    e.Cancel = true;
}

However, there is a problem in TreeView when double click on CheckBoxes the check events not work as expected. Follow the solution that I've shared in the linked post or use the following ExTreeView which has a CheckBoxEdit property (similar to LabelEdit ) which allows you enable or disable checking on CheckBoxes:

public class ExTreeView : TreeView
{
    private const int WM_LBUTTONDBLCLK = 0x0203;
    protected override void WndProc(ref Message m)
    {
        if (m.Msg == WM_LBUTTONDBLCLK)
        {
            var info = this.HitTest(PointToClient(Cursor.Position));
            if (info.Location == TreeViewHitTestLocations.StateImage)
            {
                m.Result = IntPtr.Zero;
                return;
            }
        }
        base.WndProc(ref m);
    }
    [DefaultValue(true)]
    public bool CheckBoxEdit { get; set; } = true;
    protected override void OnBeforeCheck(TreeViewCancelEventArgs e)
    {
        base.OnBeforeCheck(e);
        e.Cancel = !CheckBoxEdit;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM