c# - How to deactiveating Treeview's CheckBox -


i have 2 treeview nodes there check-boxes behind them using required command. want make nodes gray/deative. so, pick 1 node first treeview , in other treeview nodes node.text not equal text gray. did following function:

public void deactive_checkboxs(object sender, eventargs e) {     treenodecollection nodes_checked1 = treeview1.nodes[0].lastnode.nodes;     treenodecollection nodes_checked2 = treeview2.nodes[0].lastnode.nodes;      foreach (treenode node1 in nodes_checked1)     {         if (node1.checked)         {             foreach (treenode node2 in nodes_checked2)             {                 if (node1.text.equals(node2.text))                 {                     node2.checked = false;                 }                 else                 {                     node2.checked = false;                 }              }          }     } } 

of course not going gray nodes rather remove checked boxes behind nodes( if let me know making gray methods appreciate it).

now not know should put function have effect on code. me?

this not possible setting property.

you can neither disable single node nor remove checkbox.

here can do:

  • you can change forecolor of each node individually
  • you can prevent changing checked state of node individually

here example:

enter image description here

    treeview1.checkboxes = true;     treenode tn = new treenode("node 1");     treenode tn1 = new treenode("node 11");     treenode tn2 = new treenode("node 12");     tn1.checked = true;     tn2.checked = true;     tn.nodes.add(tn1);     tn.nodes.add(tn2);     treeview1.nodes.add(tn);      // gray text:     tn2.forecolor = color.gray;      // mark 1 node somehow:     tn2.tag = "x";     // cancel changes marked node:     treeview1.beforecheck += (s, e)          => { if (e.node.tag != null && e.node.tag.tostring() == "x") e.cancel = true; }; 

to enable/disable node best use function:

void setnode(treenode node, bool enabled) {     node.forecolor = enabled ? systemcolors.controltext : color.gray;     node.tag = enabled ? null : "x"; } 

to make checkbox look disabled have owner-draw node yourself. quite bit of code..

enter image description here

first few preparations:

    treeview1.drawmode = treeviewdrawmode.ownerdrawall;      // cancel changes marked node:     treeview1.beforecheck += (s, e)          => { if (e.node.tag != null && e.node.tag.tostring() == "x") e.cancel = true; };      // small correction;     treeview1.afterexpand += (s,e) => {treeview1.refresh();}; 

and actual drawing of nodes:

    treeview1.drawnode += (s, e)         =>         {             if (e.node.tag == null || e.node.tag.tostring() != "x")             { e.drawdefault = true; return; }              checkboxstate cbschdis = checkboxstate.checkeddisabled;             checkboxstate cbsucdis = checkboxstate.uncheckeddisabled;              size glyph = size.empty;             glyph = checkboxrenderer.getglyphsize(e.graphics, cbschdis);              rectangle tbounds = e.node.bounds;  // real bounds of hittest area              if (e.node.isselected)             {                 e.graphics.fillrectangle(systembrushes.menuhighlight, tbounds);                 e.graphics.drawstring(e.node.text , font, brushes.white,                                         tbounds.x , tbounds.y);             }             else             {                 checkboxrenderer.drawparentbackground(e.graphics, e.bounds, this);                 e.graphics.drawstring(e.node.text , font, brushes.black,                                         tbounds.x , tbounds.y);             }              point cboxlocation = new point(tbounds.left - glyph.width , tbounds.top);             checkboxstate bs1 = e.node.checked ? cbschdis : cbsucdis;             checkboxrenderer.drawcheckbox(e.graphics, cboxlocation, bs1);          }; 

Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -