B
Benny Raymond
More problems with this... When I run this code, the main form returns
an invalid cast exception as it's executing the line "TreeNode n =
(TreeNode) this.Nodes[0];"
Does anyone know what would cause this? I just want to be able to use
my own node class so that I can store extra data...
=========================
using System;
using System.ComponentModel;
using System.Collections;
using System.Drawing;
using System.Diagnostics;
using System.Windows.Forms;
namespace System.Windows.Forms
{
#region TView
public class tView : TreeView
{
#region Constructor
public tView()
{
SetStyle(ControlStyles.UserPaint, true);
SetStyle(ControlStyles.AllPaintingInWmPaint, true);
SetStyle(ControlStyles.ResizeRedraw, true);
}
#endregion
#region Custom Drawing R Us
protected override void OnPaint(PaintEventArgs pe)
{
/*
* since windows handles the drawing of a tree control,
* we had to use userpaint true, thus the following line
* doesn't work. Normally this line would draw everything
* for me, then i could go back and make some changes...
*
*/
//base.OnPaint(pe);
// Declare and instantiate a new pen.
Graphics g = pe.Graphics;
TreeNode n = (TreeNode) this.Nodes[0];
// either draw the first node, or draw the first visible node
if (n.IsVisible)
{
DrawText(g, n);
}
else
{
DrawText(g, (TreeNode) n.NextVisibleNode);
}
}
protected void DrawText (Graphics g, TreeNode n)
{
Rectangle r = new Rectangle(n.Bounds.X, n.Bounds.Y, n.Bounds.Width +
5, n.Bounds.Height);
if (n.IsVisible)
{
/*
* we have to check IsVisible so we aren't drawing
* what we don't have to, this is because next visible
* node doesn't really return the next "visible" node
* it just returns the next node that would be visible
* if we were drawing everything
*/
if (n.IsSelected)
{
g.FillRectangle(SystemBrushes.Highlight, r);
g.DrawString(n.Text, this.Font, SystemBrushes.HighlightText, r);
DrawLines(g,n);
}
else
{
g.DrawString(n.Text, this.Font, SystemBrushes.WindowText, r);
}
}
if (n.NextVisibleNode != null)
{
DrawText(g, ((TreeNode) n.NextVisibleNode));
}
}
protected void DrawLines (Graphics g, TreeNode n)
{
int indent = n.TreeView.Indent;
Rectangle boxBounds = new Rectangle(n.Bounds.X - indent, n.Bounds.Y,
indent, n.Bounds.Height);
g.DrawRectangle(SystemPens.ControlDark, boxBounds);
g.DrawLine(SystemPens.WindowText, boxBounds.Left + 4,
boxBounds.Top + 2, boxBounds.Left + 4, boxBounds.Top + 6);
g.DrawLine(SystemPens.WindowText, boxBounds.Left + 2, boxBounds.Top +
4, boxBounds.Left + 6, boxBounds.Top + 4);
}
#endregion
#region TreeNode
public class TreeNode : System.Windows.Forms.TreeNode
{
private Hashtable _items;
public Hashtable Items
{
get
{
return _items;
}
set
{
_items = value;
}
}
}
#endregion
}
#endregion
}
an invalid cast exception as it's executing the line "TreeNode n =
(TreeNode) this.Nodes[0];"
Does anyone know what would cause this? I just want to be able to use
my own node class so that I can store extra data...
=========================
using System;
using System.ComponentModel;
using System.Collections;
using System.Drawing;
using System.Diagnostics;
using System.Windows.Forms;
namespace System.Windows.Forms
{
#region TView
public class tView : TreeView
{
#region Constructor
public tView()
{
SetStyle(ControlStyles.UserPaint, true);
SetStyle(ControlStyles.AllPaintingInWmPaint, true);
SetStyle(ControlStyles.ResizeRedraw, true);
}
#endregion
#region Custom Drawing R Us
protected override void OnPaint(PaintEventArgs pe)
{
/*
* since windows handles the drawing of a tree control,
* we had to use userpaint true, thus the following line
* doesn't work. Normally this line would draw everything
* for me, then i could go back and make some changes...
*

*/
//base.OnPaint(pe);
// Declare and instantiate a new pen.
Graphics g = pe.Graphics;
TreeNode n = (TreeNode) this.Nodes[0];
// either draw the first node, or draw the first visible node
if (n.IsVisible)
{
DrawText(g, n);
}
else
{
DrawText(g, (TreeNode) n.NextVisibleNode);
}
}
protected void DrawText (Graphics g, TreeNode n)
{
Rectangle r = new Rectangle(n.Bounds.X, n.Bounds.Y, n.Bounds.Width +
5, n.Bounds.Height);
if (n.IsVisible)
{
/*
* we have to check IsVisible so we aren't drawing
* what we don't have to, this is because next visible
* node doesn't really return the next "visible" node
* it just returns the next node that would be visible
* if we were drawing everything
*/
if (n.IsSelected)
{
g.FillRectangle(SystemBrushes.Highlight, r);
g.DrawString(n.Text, this.Font, SystemBrushes.HighlightText, r);
DrawLines(g,n);
}
else
{
g.DrawString(n.Text, this.Font, SystemBrushes.WindowText, r);
}
}
if (n.NextVisibleNode != null)
{
DrawText(g, ((TreeNode) n.NextVisibleNode));
}
}
protected void DrawLines (Graphics g, TreeNode n)
{
int indent = n.TreeView.Indent;
Rectangle boxBounds = new Rectangle(n.Bounds.X - indent, n.Bounds.Y,
indent, n.Bounds.Height);
g.DrawRectangle(SystemPens.ControlDark, boxBounds);
g.DrawLine(SystemPens.WindowText, boxBounds.Left + 4,
boxBounds.Top + 2, boxBounds.Left + 4, boxBounds.Top + 6);
g.DrawLine(SystemPens.WindowText, boxBounds.Left + 2, boxBounds.Top +
4, boxBounds.Left + 6, boxBounds.Top + 4);
}
#endregion
#region TreeNode
public class TreeNode : System.Windows.Forms.TreeNode
{
private Hashtable _items;
public Hashtable Items
{
get
{
return _items;
}
set
{
_items = value;
}
}
}
#endregion
}
#endregion
}