Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / June 2007

Tip: Looking for answers? Try searching our database.

help on treeview

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
AVL - 25 Jun 2007 13:58 GMT
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?
Morten Wennevik [C# MVP] - 25 Jun 2007 16:28 GMT
> 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;
        }

Signature

Happy coding!
Morten Wennevik [C# MVP]


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.