如何从TreeNode派生? [英] How to derive from TreeNode?

查看:136
本文介绍了如何从TreeNode派生?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,


我仍​​然是C#的新手,并且想从

TreeNode派生来扩展TreeNode类。这是测试程序:

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


我在第0077行收到错误说:


"发生了'System.InvalidCastException'类型的未处理异常
inheritance.exe中的



附加信息:已指定演员无效。


请帮助,谢谢。


-

Mark Allison,SQL服务器MVP
http://www.markallison.co.uk


寻找SQL Server复制书?
http://www.nwsu.com/0974973602m.html

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

推荐答案

Mark Allison< ma *** @ no.tinned.meat.mvps.org>写道:
Mark Allison <ma***@no.tinned.meat.mvps.org> wrote:
我仍然是C#的新手,并希望从TreeNode派生来扩展TreeNode类。这是测试程序:

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

我在第0077行收到错误说:

" ;在inheritance.exe中发生了'System.InvalidCastException'类型的未处理异常

附加信息:指定的强制转换无效。
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."




你正在添加一个TreeNodeEx,还有一个直的TreeNode - 和

,这是在tvwTest_AfterSelect中立即使用的那个。


-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet

如果回复该群组,请不要给我发邮件



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

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


标记,


如果覆盖树节点类,则需要子类化

TreeVi ew,所以当它添加节点时,它会添加你的类型的节点,而不是
TreeViewNode。如果你查看你发布的代码,有一个节点是

类型TreeViewNode(不是你的)正在添加,不能强制转换为

你的类型。


希望这会有所帮助。

-

- Nicholas Paldino [.NET / C#MVP]

- mv*@spam.guard.caspershouse.com


" ; Mark Allison <毫安*** @ no.tinned.meat.mvps.org>在留言中写道

新闻:%2 **************** @ TK2MSFTNGP15.phx.gbl ...
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Mark Allison" <ma***@no.tinned.meat.mvps.org> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
你好,我仍然是C#的新手,并希望从TreeNode派生来扩展TreeNode类。这是测试程序:

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

我在第0077行收到错误说:

" ;
inheritance.exe中发生了'System.InvalidCastException'类型的未处理异常

附加信息:指定的强制转换无效。

请帮助,谢谢。

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

寻找SQL Server复制书?
http://www.nwsu.com/0974973602m.html
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



这只是一个猜测,但在你的代码中,InitializeComponent()将一个TreeNode

(名为Test)添加到树中,如果你试图选择此节点然后在第0077行中投出


TreeNodeEx tne =(TreeNodeEx)e.Node;

到TreeNodeEx确实无效。也许你应该将AfterSelect

处理程序重写为:

private void tvwTest_AfterSelect(object sender,

System.Windows.Forms.TreeViewEventArgs e)

{

TreeNodeEx tne = e.Node as TreeNodeEx;

if(tne!= null)

MessageBox。显示(选中的节点是+ tne.Type);

其他

MesageBox.Show("选择的节点不是TreeNodeEx);




Chris Jobson


" Mark Allison" <毫安*** @ no.tinned.meat.mvps.org>在留言中写道

新闻:%2 **************** @ TK2MSFTNGP15.phx.gbl ...
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

"Mark Allison" <ma***@no.tinned.meat.mvps.org> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
你好,我仍然是C#的新手,并希望从TreeNode派生来扩展TreeNode类。这是测试程序:

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

我在第0077行收到错误说:

" ;
inheritance.exe中发生了'System.InvalidCastException'类型的未处理异常

附加信息:指定的强制转换无效。

请帮助,谢谢。

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

寻找SQL Server复制书?
http://www.nwsu.com/0974973602m.html
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



这篇关于如何从TreeNode派生?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆