help on treeview

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

hi,
i've used a treeview control in my windows form....
i want to display the text of my treeview nodes as hyperlink..instead of
showing it as plain text......
how can i acheive it?
 
hi,
i've used a treeview control in my windows form....
i want to display the text of my treeview nodes as hyperlink..instead of
showing it as plain text......
how can i acheive it?

Hi,

There may be far better options, but you can create this functionality by ownerdrawing the nodes.
The code below uses ownerdrawing to hide the background color and draw the treenodes with blue and underlined if the mouse hovers over it.

The code isn't very good and the drawing code leaves lot to be desired (does not show selected node etc)

public Form1()
{
InitializeComponent();
treeView1.DrawNode += new DrawTreeNodeEventHandler(treeView1_DrawNode);
treeView1.NodeMouseClick += new TreeNodeMouseClickEventHandler(treeView1_NodeMouseClick);
treeView1.HotTracking = true;
}

void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
{
if(e.Node.Tag != null)
Process.Start(e.Node.Tag.ToString());
}

void treeView1_DrawNode(object sender, DrawTreeNodeEventArgs e)
{
e.Graphics.FillRectangle(Brushes.White, e.Bounds);
if ((e.State & TreeNodeStates.Hot) > 0)
{
e.Graphics.DrawString(e.Node.Text, treeView1.Font, Brushes.Blue, e.Bounds);
e.Graphics.DrawLine(Pens.Blue, e.Bounds.Left + 2, e.Bounds.Bottom - 2, e.Bounds.Left + 2 + MeasureLength(e.Graphics, e.Node.Text, treeView1.Font, e.Bounds), e.Bounds.Bottom - 2);
}
else
{
e.Graphics.DrawString(e.Node.Text, treeView1.Font, SystemBrushes.ControlText, e.Bounds);
e.Graphics.DrawLine(Pens.White, e.Bounds.Left + 2, e.Bounds.Bottom - 2, e.Bounds.Left + 2 + MeasureLength(e.Graphics, e.Node.Text, treeView1.Font, e.Bounds), e.Bounds.Bottom - 2);
}
}

private int MeasureLength(Graphics g, string s, Font f, Rectangle r)
{
StringFormat format = new StringFormat();
CharacterRange range = new CharacterRange();
range.First = 0;
range.Length = s.Length;
format.SetMeasurableCharacterRanges(new CharacterRange[] { range });
Region[] regions = g.MeasureCharacterRanges(s, f, r, format);
return (int)regions[0].GetBounds(g).Width;
}
 
Back
Top