TreeView/Node Question
-
Hopefully someone can help. I’ve been researching this for two days now, and I'm beginning to feel like an ID10T! Is it me, or does Microsoft INSIST on making things that should be easy really, really HARD. :-D Here's my problem: When an item appears in your inbox in Outlook, the treenode text becomes BOLD, and when you have looked at all the mail, it is reset. That is basically what I want to do. I need to be able to make a TreeNode bold when it is selected, and reset it when another TreeNode is selected. I have tried several of the events, but so far have not been able to find one that gives me access to make the text bold. I can change the font for the entire control, but have not been able to figure out how to change the code for just the selected node (and it's children) Any ideas? Thanks. WhiteWizard (aka Gandalf)
-
Hopefully someone can help. I’ve been researching this for two days now, and I'm beginning to feel like an ID10T! Is it me, or does Microsoft INSIST on making things that should be easy really, really HARD. :-D Here's my problem: When an item appears in your inbox in Outlook, the treenode text becomes BOLD, and when you have looked at all the mail, it is reset. That is basically what I want to do. I need to be able to make a TreeNode bold when it is selected, and reset it when another TreeNode is selected. I have tried several of the events, but so far have not been able to find one that gives me access to make the text bold. I can change the font for the entire control, but have not been able to figure out how to change the code for just the selected node (and it's children) Any ideas? Thanks. WhiteWizard (aka Gandalf)
Can be done (with some minor annoyances) pretty easily. I created a blank windows app and dropped a single treeview onto it (treeView1). I created a simple structure of nodes as follows
-+ Root --+ Node 1 ---- Node 1_1 ---- Node 1_2 -- Node 2
Next, created 2 recursive methods to make a node and all children bold or regular.private void MakeBoldRecursive(TreeNode node) { node.NodeFont = new Font(this.treeView1.Font,FontStyle.Bold); foreach(TreeNode child in node.Nodes) { MakeBoldRecursive(child); } } private void MakeRegularRecursive(TreeNode node) { node.NodeFont = new Font(this.treeView1.Font,FontStyle.Regular); foreach(TreeNode child in node.Nodes) { MakeUnboldRecursive(child); } }
Next, have a private variable to hold onto the selected node, so that we can revert it when it is no longer selected:private TreeNode selectedNode = null;
Then subscribe to theBeforeSelect
event of the treeview (note: this can also be in the AfterSelect event handler, but BefoerSelect was visually slightly quicker)private void treeView1_BeforeSelect(object sender, System.Windows.Forms.TreeViewCancelEventArgs e) { if(this.selectedNode != null) MakeRegularRecursive(this.selectedNode); MakeBoldRecursive(e.Node); this.selectedNode = e.Node; }
I think this gives the effect you describe, one minor annoyance is that when turning the node from regular to bold it does not re-layout the control so sometimes the last few pixels of the bolded text get chopped off. You could workaround this by appending a few extra spaces to the end of the text for the node. Hope that helps, and that you can read the C# code. This was really a coding question and could have been put in your prefered language board. -
Can be done (with some minor annoyances) pretty easily. I created a blank windows app and dropped a single treeview onto it (treeView1). I created a simple structure of nodes as follows
-+ Root --+ Node 1 ---- Node 1_1 ---- Node 1_2 -- Node 2
Next, created 2 recursive methods to make a node and all children bold or regular.private void MakeBoldRecursive(TreeNode node) { node.NodeFont = new Font(this.treeView1.Font,FontStyle.Bold); foreach(TreeNode child in node.Nodes) { MakeBoldRecursive(child); } } private void MakeRegularRecursive(TreeNode node) { node.NodeFont = new Font(this.treeView1.Font,FontStyle.Regular); foreach(TreeNode child in node.Nodes) { MakeUnboldRecursive(child); } }
Next, have a private variable to hold onto the selected node, so that we can revert it when it is no longer selected:private TreeNode selectedNode = null;
Then subscribe to theBeforeSelect
event of the treeview (note: this can also be in the AfterSelect event handler, but BefoerSelect was visually slightly quicker)private void treeView1_BeforeSelect(object sender, System.Windows.Forms.TreeViewCancelEventArgs e) { if(this.selectedNode != null) MakeRegularRecursive(this.selectedNode); MakeBoldRecursive(e.Node); this.selectedNode = e.Node; }
I think this gives the effect you describe, one minor annoyance is that when turning the node from regular to bold it does not re-layout the control so sometimes the last few pixels of the bolded text get chopped off. You could workaround this by appending a few extra spaces to the end of the text for the node. Hope that helps, and that you can read the C# code. This was really a coding question and could have been put in your prefered language board.THANK YOU. That is REALLY close to what I want and will get me started. The only change I'll need to make is to NOT reset when a child node is selected, but can probably figure that out. I thought about putting this in the C# group, but figured if someone had a VB solution I could get it and convert. MANY Thanks.
-
Hopefully someone can help. I’ve been researching this for two days now, and I'm beginning to feel like an ID10T! Is it me, or does Microsoft INSIST on making things that should be easy really, really HARD. :-D Here's my problem: When an item appears in your inbox in Outlook, the treenode text becomes BOLD, and when you have looked at all the mail, it is reset. That is basically what I want to do. I need to be able to make a TreeNode bold when it is selected, and reset it when another TreeNode is selected. I have tried several of the events, but so far have not been able to find one that gives me access to make the text bold. I can change the font for the entire control, but have not been able to figure out how to change the code for just the selected node (and it's children) Any ideas? Thanks. WhiteWizard (aka Gandalf)
MCSD-Gandalf wrote:
but so far have not been able to find one that gives me access to make the text bold.
Set the
TVIS_BOLD
state on the item you want to be bold.--Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | NEW!! PimpFish | CP SearchBar v3.0 | C++ Forum FAQ
-
THANK YOU. That is REALLY close to what I want and will get me started. The only change I'll need to make is to NOT reset when a child node is selected, but can probably figure that out. I thought about putting this in the C# group, but figured if someone had a VB solution I could get it and convert. MANY Thanks.
Glad I could help, hope you spotted the deliberate mistake... I renamed
MakeUnboldRecursive
toMakeRegularRecursive
but didnt compile afterwards...so missed one call from inside the recursive method. I thought you were language agnostic...so all worked out well. -
Hopefully someone can help. I’ve been researching this for two days now, and I'm beginning to feel like an ID10T! Is it me, or does Microsoft INSIST on making things that should be easy really, really HARD. :-D Here's my problem: When an item appears in your inbox in Outlook, the treenode text becomes BOLD, and when you have looked at all the mail, it is reset. That is basically what I want to do. I need to be able to make a TreeNode bold when it is selected, and reset it when another TreeNode is selected. I have tried several of the events, but so far have not been able to find one that gives me access to make the text bold. I can change the font for the entire control, but have not been able to figure out how to change the code for just the selected node (and it's children) Any ideas? Thanks. WhiteWizard (aka Gandalf)
mark :) I Love C#/VS2005! http://justinw.cnblogs.com