Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do you modify the width of a treeview node?

Status
Not open for further replies.

CosmicCharlie

Programmer
Jun 30, 2006
44
US
How can I modify the width of the text portion of a treeview node at run time?

I am working with a treeview control that displays client names of my company. Under certain conditions, some client names need to be flagged by making the backcolor of the node in question yellow and making the text bold. I am using this code:

Code:
If .NeedsAssessment then
     nd.BackColor = System.Drawing.Color.Yellow
     nd.NodeFont = New Font("Arial", 8, FontStyle.Bold)
End If

Fine so far. Then I found that many of the highlighted nodes expand the text but the width of those nodes does not expand with it, so the ends of the client names are getting chopped off.

This isn't a big deal, but it looks bad.

The problem is that I cannot figure out how to get the node to widen to accommodate the extra length. I have tried playing with the Bounds property, but the various methods I use either have no apparent affect, are read only, or raise null object reference errors. I also tried applying the color and font first, then the node text, but that makes no difference. I also tried applying the formatting after the entire treeview had been populated, thinking that this would solve the null reference problem, but that didn't work either.

Does anyone know how this can be done?

RELATED QUESTION: While working on this I tried this If statement:

Code:
If nd.BackColor = System.Drawing.Color.Yellow Then

But .Net told me:

Operator '=' is not defined for types 'System.Drawing.Color' and 'System.Drawing.Color'.

This seems odd. Does anyone have an answer for that?

Thank you.

Charlie

 
K, starting at the end.

You have to take the color values and ".ToString" them and compare them that way. (Easiest work around i have found)

With the text width issue. it gets messed up. The easiest way to deal with it is to set up your tree view so that the default font/color is the widest you will have and then "regular-ize" everything else. The treeview isnt coded to handle the width adjustment.

-The answer to your problem may not be the answer to your question.
 
Thank you, Qik3Coder, for your reply. Using ToString worked exactly as you said it would. I will start trying that out more often when I run into similar problems.

As for your other suggestion, I confess I don't understand it. I tried various things that seemed to be what you were suggesting, but failed to get anywhere. Are you saying that there really is no solution? If you have a code snippet that performs what you are describing, can you please share that?

Thank you.
 
What i meant was that you have to set the default font of your datagrid to the "biggest" font you have, then in code make everything else "regular".

Psuedo code example

So rather than going

"A big <bold>Font</bold> is here"

which will give you:

A big Font is here

you have to go:

<regular>A big</regular>Font<regular> is here</regular>

to get to to show right in the datagrid


-The answer to your problem may not be the answer to your question.
 
Got it! I am actually working on a treeview, but your explanation got me to where I needed to be. Here is the basic code I used:

Code:
Dim fntNormal As New Font("Microsoft Sans Serif", 8, FontStyle.Regular)
trvMembers.Font = New Font("Arial", 8, FontStyle.Bold)

...

If .NeedsAssessment Then
     nd.BackColor = System.Drawing.Color.Yellow
Else
     nd.NodeFont = fntNormal
End If

Of course, there are other tiers in the treeview, and I had to set all of their nodes to fntNormal as well, but it was worth the trouble. All the member names appear as they should.

Thank you for your help here.

Charlie
 
Glad that helped. And i knew you were working with a treeview. It sticks in the head a little longer if you have to figure out part of it. FYI, great job on setting up the form wide font. Makes life easier in the long run.

-The answer to your problem may not be the answer to your question.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top