How to derive from TreeNode?

M

Mark Allison

Hi there,

I'm still very much a newbie to C#, and would like to derive from
TreeNode to extend the TreeNode class. Here's test program:

http://www.vkarlsen.no/pastebin/default.asp?id=4090

I get an error on line 0077 saying:

"An unhandled exception of type 'System.InvalidCastException' occurred
in inheritance.exe

Additional information: Specified cast is not valid."

Please help, thanks.

--
Mark Allison, SQL Server MVP
http://www.markallison.co.uk

Looking for a SQL Server replication book?
http://www.nwsu.com/0974973602m.html
 
J

Jon Skeet [C# MVP]

Mark Allison said:
I'm still very much a newbie to C#, and would like to derive from
TreeNode to extend the TreeNode class. Here's test program:

http://www.vkarlsen.no/pastebin/default.asp?id=4090

I get an error on line 0077 saying:

"An unhandled exception of type 'System.InvalidCastException' occurred
in inheritance.exe

Additional information: Specified cast is not valid."

You're adding one TreeNodeEx, but also one straight TreeNode - and
that's the one which is immediately being used in tvwTest_AfterSelect.
 
N

Nicholas Paldino [.NET/C# MVP]

Mark,

If you override the tree node class, then you need to subclass the
TreeView, so that when it adds nodes, it adds nodes of your type, and not
TreeViewNode. If you look at the code that you posted, there is a node of
type TreeViewNode (not yours) that is being added, which can not be cast to
your type.

Hope this helps.
 
C

Chris Jobson

This is just a guess, but in your code InitializeComponent() adds a TreeNode
(named "Test") to the tree, and if you try to select this node then the cast
in line 0077
TreeNodeEx tne = (TreeNodeEx)e.Node;
to TreeNodeEx is indeed invalid. Maybe you should rewrite the AfterSelect
handler as:
private void tvwTest_AfterSelect(object sender,
System.Windows.Forms.TreeViewEventArgs e)
{
TreeNodeEx tne = e.Node as TreeNodeEx;
if (tne != null)
MessageBox.Show("Node selected is " + tne.Type);
else
MesageBox.Show("Node selected is not a TreeNodeEx);
)

Chris Jobson
 
B

Brian

Mark said:
Perfect, thanks Jon, Nicholas, Chris.

Please excuse my ignorance in C# and posting for that matter. Just
learning.
Does anyone know how to subclass the Treeview class?
I created my own TreeNode class that inherits from Microsoft's TreeNode
class. I also created my own TreeView class that inherits from
Microsoft's. If I add TreeNodes manually, I obviously can add my own
inherited TreeNode instead and everything is fine but when using the
xmlsrc and xsltsrc properties, how can I tell TreeView to use my
ExtendedTreeNode class?

Thanks,

Brian
 
N

Nicholas Paldino [.NET/C# MVP]

Brian,

I don't think you can. The only way you could do this is to see if the
TreeView class has an overridable method which actually handles the creation
of the new node (which I am not sure it does, and I am not sure if you are
using an ASP.NET tree control or a WinForms one either, so I can't be more
specific).

Hope this helps.
 
B

Brian

Thanks for such a quick reply Nicholas. I apologize for not being
clearer in my question
regarding which control I'm using. I'm using the asp.net control and am
trying to add my
own attributes to the rendered html. Apprarently, you only can add to
the 'NodeData' attribute.
I'm able to succssfully add them if I loop through my xml and use my
own ExtendedTreeNode class
but I don't know how less efficient that is going to be. If you have
any other advice, I'd appreciate it.
Below is what I put together in my test app.

public class WebForm1 : System.Web.UI.Page
{
protected Microsoft.Web.UI.WebControls.TreeView TreeView1;

private void Page_Load(object sender, System.EventArgs e)
{
// This works but can't add my own attributes
TreeView1.TreeNodeSrc = "test.xml";
TreeView1.DataBind();

// This works but can't have to add each treenode
individually
//ExtendedTreeNode n = new ExtendedTreeNode();
//n.Tekuid = Convert.ToInt32("1234");
//n.Tmruid = Convert.ToInt32("5678");
//n.Text="Node Test";
//TreeView1.Nodes.Add(n);
}

/// Extended TreeNode
public class ExtendedTreeNode : Microsoft.Web.UI.WebControls.TreeNode
{
#region Accessors
private Int32 _uid;
public Int32 uid
{
get { return _uid; }
set { _uid = value;}
}
#endregion

// Need to override this to get the attributes to display on
the client
protected override void AddAttributesToRender(HtmlTextWriter
writer)
{
writer.AddAttribute("uid", Convert.ToString(this.uid));
base.AddAttributesToRender(writer);
}
}

Thanks,

Brian
 
B

Brian

Thank you Nicholas for such a quick reply. I apologize for not being
clearer in my post.
I'm using the ASP.NET tree control. What I'm actually trying to
accomplish is adding my
own attributes to the rendered html. Apparently, you can only add to an
attribute called
"NodeData". I can loop through my xml, calling my ExtendedTreeNode
class but I don't
believe that will be very efficient. Below is what I put together in my
test app. Any advice
would be very much appreciated.

public class WebForm1 : System.Web.UI.Page
{
protected Microsoft.Web.UI.WebControls.TreeView TreeView1;
private void Page_Load(object sender, System.EventArgs e)
{
// This works but can't add custom attributes
TreeView1.TreeNodeSrc = "test.xml";
TreeView1.DataBind();

// This works but need to call for each node;
ExtendedTreeNode n = new ExtendedTreeNode();
n.uid = Convert.ToInt32("1234");
n.Text="Node Test";
TreeView1.Nodes.Add(n);
}
}

public class ExtendedTreeNode : Microsoft.Web.UI.WebControls.TreeNode
{
#region Accessors
private Int32 _uid;

public Int32 uid
{
get { return _uid; }
set { _uid = value;}
}
#endregion

// Override this method to get custom attributes rendered on client
protected override void AddAttributesToRender(HtmlTextWriter writer)
{
writer.AddAttribute("uid", Convert.ToString(this.uid));
base.AddAttributesToRender(writer);
}
}

Thanks,

Brian
 
B

Brian

Thanks Nicholas for the quick reply but I'm a little confused. Didn't
you write, "If you override the tree node class, then you need to
subclass the TreeView, so that when it adds nodes, it adds nodes of
your type, and not TreeViewNode." ? Have a missed something?
 

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