Multiline TreeNode

L

lord.zoltar

Hello,
I have to display some tree nodes like this:

group 1
Event#1
From $startDate
To $endDate
group 2
Event#2
From $startDate
Event #2 details
Some sort of subnode...
Event#3
From $startDate

"Event#1", "Event#2", and "Event#3" are single nodes which I'd like to
display the extra info (start and optional end dates) below. They
could have detail child nodes which could have further sub nodes,
etc...
Currently, I set the Text for the node as "Event#1\nFrom
$startDate...", the \n does not affect the way the node displays in
the TreeView... BUT it DOES affect the tooltips and they format
correctly. The only problem is that the user will have to "scrub"
several events and read the tooltips to find info about start and end
dates. They will not be able to see multiple start/end dates.

Is there any way to get multiple lines of text for a treeview node?
 
M

Morten Wennevik [C# MVP]

Hail Lord Zoltar,

I'm afraid there is no simple solution to multiline nodes in the .Net
TreeView as for one it does not have a MeasureItem event like the ListBox
does so the only way to adjust the size of a node is by handling the
TVM_GETITEMRECT windows event. Even handling this event will not solve
scroll problems that may occur. These will have to be handled using
SetScrollInfo.

Basically if possible, there will be a lot of code and frustration to get it
to work, so you might want to rethink how you display the information. Maybe
using various icons and/or show the details when you select the node.
 
L

lord.zoltar

Hail Lord Zoltar,

I'm afraid there is no simple solution to multiline nodes in the .Net
TreeView as for one it does not have a MeasureItem event like the ListBox
does so the only way to adjust the size of a node is by handling theTVM_GETITEMRECTwindows event.  Even handling this event will not solve
scroll problems that may occur.  These will have to be handled using
SetScrollInfo.

Basically if possible, there will be a lot of code and frustration to getit
to work, so you might want to rethink how you display the information.  Maybe
using various icons and/or show the details when you select the node.

Heh well I'm in the mood for a challenge tonight (even if I don't have
any idea what I'm doing - I'll learn damnit!)
:p

So this even looks like it will get me the bounding box of some
treeNode.
I wrote a little method to intercept it in a class that extends
TreeView:

protected override void WndProc(ref Message m)
{
if (m.Msg == TVM_GETITEMRECT)
{
Console.WriteLine("message received: {0} LPARAM {1} WPARAM
{2}",m.Msg,m.LParam,m.WParam);
//lparam is a pointer to the rectangle that holds the item.

RECT rect = new RECT();
if (m.LParam.ToInt32()!=0)
rect = (RECT) Marshal.PtrToStructure(m.LParam, typeof(RECT));

Console.WriteLine(rect.ToString());
}
else
base.WndProc(ref m);
}

[.....snip.....]

[StructLayout(LayoutKind.Sequential)]
private struct RECT
{
public int left;
public int top;
public int right;
public int bottom;
public override string ToString()
{
return string.Format("Rectangle (L,T),(R,B): ({0},{1})-({2},{3})",
this.left, this.top, this.right, this.bottom);
}
}

I've noticed that this event only gets fired when I click on a
treeNode. I don't know how useful that will be for drawing multiline
text, but we'll see...
The output of this looks something like this:

message received: 4356 LPARAM 70897860 WPARAM 1
Rectangle (L,T),(R,B): (65006680,0)-(0,0)

I thought I'd seen Marshal.PtrToStruct doing this elsewhere, but it
doesn't seem to work the way I had expected in this case...

I also thought I could send a message myself, so I wrote some code
(also in the extended treeView class) to try that:
[DllImport("user32.dll")]
public static extern int SendMessage(
IntPtr hWnd, // handle to destination window
uint Msg, // message
long wParam, // first message parameter
IntPtr lParam // second message parameter
);

public void sndMsg()
{
int i = SendMessage(this.Handle, TVM_GETITEMRECT, 1 , this.Nodes
[0].Handle);
}

When I call THIS method, my WndProc catches the message as expected,
but lParam has nothing in it (its value is 0), so nothing really
happens (Oh and Nodes[0] DOES have a value).

Any tips or pointers?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top