473,666 Members | 2,474 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Unable to cast object of type TreeViewItem to custom type

I'm currently trying to extend the TreeViewItem class so I can attach
some additional attributes to the item.

I created a custom class

class myTreeNode : System.Windows. Controls.TreeVi ewItem
{
public bool updated;
public myTreeNode()
{

}

}
}
I then tried to do the following::
private void treeDoubleClick (object sender,
System.Windows. Input.MouseButt onEventArgs e)
{

System.Windows. Controls.TreeVi ew currentTreeView =
(System.Windows .Controls.TreeV iew)sender;

myTreeNode myNode = (myTreeNode)((T reeView)sender) .SelectedItem;
myTreeNode.upda ted = false;
....
}

But I get the error Unable to cast object of type TreeViewItem to type
myTreeNode
Any suggestions?

Nov 14 '07 #1
8 6733

<br************ @gmail.comwrote in message
news:11******** *************@1 9g2000hsx.googl egroups.com...
I'm currently trying to extend the TreeViewItem class so I can attach
some additional attributes to the item.

I created a custom class

class myTreeNode : System.Windows. Controls.TreeVi ewItem
{
public bool updated;
public myTreeNode()
{

}

}
}
I then tried to do the following::
private void treeDoubleClick (object sender,
System.Windows. Input.MouseButt onEventArgs e)
{

System.Windows. Controls.TreeVi ew currentTreeView =
(System.Windows .Controls.TreeV iew)sender;

myTreeNode myNode = (myTreeNode)((T reeView)sender) .SelectedItem;
myTreeNode.upda ted = false;
....
}

But I get the error Unable to cast object of type TreeViewItem to type
myTreeNode
Any suggestions?
Make sure that you add items of type myTreeNode and not TreeViewItem when
you populate the control.
Nov 14 '07 #2
On Nov 14, 9:52 am, "Ben Voigt [C++ MVP]" <r...@nospam.no spamwrote:
<bryan.kardi... @gmail.comwrote in message

news:11******** *************@1 9g2000hsx.googl egroups.com...
I'm currently trying to extend the TreeViewItem class so I can attach
some additional attributes to the item.
I created a custom class
class myTreeNode : System.Windows. Controls.TreeVi ewItem
{
public bool updated;
public myTreeNode()
{
}
}
}
I then tried to do the following::
private void treeDoubleClick (object sender,
System.Windows. Input.MouseButt onEventArgs e)
{
System.Windows. Controls.TreeVi ew currentTreeView =
(System.Windows .Controls.TreeV iew)sender;
myTreeNode myNode = (myTreeNode)((T reeView)sender) .SelectedItem;
myTreeNode.upda ted = false;
....
}
But I get the error Unable to cast object of type TreeViewItem to type
myTreeNode
Any suggestions?

Make sure that you add items of type myTreeNode and not TreeViewItem when
you populate the control.
I don't think I'm entirely clear of what you mean...

I appologize for my ignorance on the subject matter and lack of
understanding of what you're suggesting I do

Nov 14 '07 #3
On Nov 14, 3:09 pm, bryan.kardi...@ gmail.com wrote:
Make sure that you add items of type myTreeNode and not TreeViewItem when
you populate the control.

I don't think I'm entirely clear of what you mean...

I appologize for my ignorance on the subject matter and lack of
understanding of what you're suggesting I do
When you populate the tree with nodes, how are you doing it? If you're
not adding instances of your own class to start with, you won't be
able to later "pretend" that the selected node is a different type.

Jon

Nov 14 '07 #4
On Nov 14, 10:12 am, "Jon Skeet [C# MVP]" <sk...@pobox.co mwrote:
On Nov 14, 3:09 pm, bryan.kardi...@ gmail.com wrote:
Make sure that you add items of type myTreeNode and not TreeViewItem when
you populate the control.
I don't think I'm entirely clear of what you mean...
I appologize for my ignorance on the subject matter and lack of
understanding of what you're suggesting I do

When you populate the tree with nodes, how are you doing it? If you're
not adding instances of your own class to start with, you won't be
able to later "pretend" that the selected node is a different type.

Jon
Initiallaly I am going

TreeView myTreeView = new TreeView();
TreeViewItem myItem = new TreeViewItem();
....
myTreeView.Item s.Add(myItem);

However, later I want to create new treeViewItems with additional
attributes so I assumed I could make a class called MyTreeNode and
have it extend the TreeViewItem class and do something like

myTreeNode myNode = new myTreeNode();
myNode.attribut e = "foobar";

myTreeView.Item s.Add(myNode);

Nov 14 '07 #5
On Nov 14, 3:21 pm, bryan.kardi...@ gmail.com wrote:

<snip>
Initiallaly I am going

TreeView myTreeView = new TreeView();
TreeViewItem myItem = new TreeViewItem();
...
myTreeView.Item s.Add(myItem);

However, later I want to create new treeViewItems with additional
attributes so I assumed I could make a class called MyTreeNode and
have it extend the TreeViewItem class and do something like

myTreeNode myNode = new myTreeNode();
myNode.attribut e = "foobar";

myTreeView.Item s.Add(myNode);
That's fine, but what do you expect to happen if someone clicks on one
of the "plain" TreeViewItems that you originally populated the tree
with? They won't be instances of myTreeNode, which is why the cast
fails.

Jon

Nov 14 '07 #6
On Nov 14, 10:28 am, "Jon Skeet [C# MVP]" <sk...@pobox.co mwrote:
On Nov 14, 3:21 pm, bryan.kardi...@ gmail.com wrote:

<snip>
Initiallaly I am going
TreeView myTreeView = new TreeView();
TreeViewItem myItem = new TreeViewItem();
...
myTreeView.Item s.Add(myItem);
However, later I want to create new treeViewItems with additional
attributes so I assumed I could make a class called MyTreeNode and
have it extend the TreeViewItem class and do something like
myTreeNode myNode = new myTreeNode();
myNode.attribut e = "foobar";
myTreeView.Item s.Add(myNode);

That's fine, but what do you expect to happen if someone clicks on one
of the "plain" TreeViewItems that you originally populated the tree
with? They won't be instances of myTreeNode, which is why the cast
fails.

Jon
That clears up the situation, I guess I need to just basically revamp
the whole backend and make every TreeViewItem an myTreeNode and give
it a bit of a better naming scheme to keep in line with best
practices ... the whole bit.

Thank you

Nov 14 '07 #7
br************@ gmail.com wrote:
>That's fine, but what do you expect to happen if someone clicks on one
of the "plain" TreeViewItems that you originally populated the tree
with? They won't be instances of myTreeNode, which is why the cast
fails.

Jon

That clears up the situation, I guess I need to just basically revamp
the whole backend and make every TreeViewItem an myTreeNode and give
it a bit of a better naming scheme to keep in line with best
practices ... the whole bit.

Not really. You could instead use either _is_ or _as_ to accomplish what
you want to do:

foreach (TreeNode node in treeView.Nodes)
{
if (tvi is myTreeNode)
{
// Do your cast and work
}
}

That is of course assuming you don't mind them being mixed.
Chris.
Nov 14 '07 #8
Chris Shepherd wrote:
foreach (TreeNode node in treeView.Nodes)
{
if (tvi is myTreeNode)
{
// Do your cast and work
}
}
Changing it up on the fly doesn't always work out. "tvi" in the above
should be "node".

Chris.
Nov 14 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
7379
by: Pankaj Jain | last post by:
Hi All, I have a class A which is derived from ServicesComponent to participate in automatic transaction with falg Transaction.Required. Class A is exposed to client through remoting on Http channal hosting into IIS. There is a class B which is also available through remoting hosted on IIS on the same URI. B creates new of A inside a function. It succeed and able to create instance of A inside B first time. But it failes in 2nd attempt when...
3
12985
by: Imran Aziz | last post by:
Hello All, I am getting the following error on our production server, and I dont get the same error on the development box. Unable to cast object of type 'System.Byte' to type 'System.String'. here is the code that I used to create a table and then add columns to it later, later I populate the rows in the table.
0
1571
by: sam | last post by:
Hi: I am not sure if this is the right place to post this question. Please let me know if it is not and I appreciate if someone could point me in the right direction. I am getting this error after converting to .NET 2.0. Unable to cast object of type 'Oracle.DataAccess.Client.OracleCommand' to type 'System.Data.Common.DbCommand'
0
1952
by: a | last post by:
Q. Unable to get a Profile custom (object) collection to bind to GridView,etc (IList objects)? This is my first custom object so I may be doing something rather simple, wrong, or it may be something else to do with the Profile object. Either way, I need help Here's a brief description of the code----------------------------------------------------------------------------------
0
1624
by: hlyall1189 | last post by:
Hi, I recently started upgrading some of my old vs 2003 apps to vs 2005 and used the conversion tool but now i get the following error after building the page. I have typecasted the lines as follows: ((StyleSheetProvider)this.Page).GetStyleSheetPath(); Is there some different way of typecasting that needs to be done in vs2005? Thanks in advance.
3
10249
by: keithb | last post by:
What could be causing this? this code: String Com = ""; if (Com != (String)rw.ItemArray) fails at runtime with the error message: Unable to cast object of type 'System.Int32' to type 'System.String'.
1
2943
by: epatrick | last post by:
I have a series of custom controls developed under ASP.NET 1.1, and I've successfully migrated to ASP.NET 2.0. I have also developed a custom class dervied from System.Web.UI.Page, called qbo.Web.Page. All of these controls compile and run correctly under ASP.NET 2.0. However, several of these controls throw an error in the design mode of VS.NET 2005. Specifically, controls that include a property override of Page (to cast as a...
1
3737
by: =?Utf-8?B?RGF2ZQ==?= | last post by:
I have a master page and a content page to that master. When I try to work with them I get an inconsistent error of casting ability and it happens irregularly, which means sometimes it can work and sometimes not, without making any changes to the code. Is this a bug concern? Or do you have any advice for this issue? Here is the error message I get:
1
2335
by: =?Utf-8?B?U2NvdHQ=?= | last post by:
Hello, Using VS2008 in a C# web service application, a class has been created that inherits from the ConfigurationSelection. This class file has been placed in the App_Code folder. The web.config has been updated with the necessary section. Using System.Web.Configuration.WebConfiguration.GetSection(), the config information is returned without any issues when the GetSection is set to an object. When the object is casted explicitly...
0
8440
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8863
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8636
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7378
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5661
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4192
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4358
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2005
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1763
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.