General C# programming Questions

Started by
6 comments, last by ISOPimp 21 years, 7 months ago
I have a few questions if there are any C# experts out there. 1. Has anyone found a really good forum for c# help? This is my first time posting here so it might be good, but then again this is gamedev.net not appdev.net... Now for the real questions: 2. I have a TreeView on a form that has a context menu assigned to it. When I right click to bring up the menu, it pops up and selects the node I right-clicked on. Then when I press one of the menu items, or cancel the menu by clicking outside of it, the node selected returns to the previously selected node. The problem is, I cant figure out how to identify the node selected when the context menu is up in the code. Anybody know how? 3. I have successfully inherited a TreeNode into the class I made called TreeNodeEx, which has some extra variables that I need. I can add/edit/delete this new node class to any existing treeview using casting and all seems well. I tried to do the same thing with a ListViewItem by inheriting it to a ListViewItemEx class and adding a few variables. Suprisingly, I cannot add this ListViewItemEx to a ListView class, no matter what I do. Question is, why can''t I do this? Any help to these questions would be greatly appreciated. Thank You.
Advertisement
quote:Original post by ISOPimp
I have a few questions if there are any C# experts out there.

1. Has anyone found a really good forum for c# help? This is my first time posting here so it might be good, but then again this is gamedev.net not appdev.net...

I prefer the official newsgroup: news://msnews.microsoft.com/microsoft.public.dotnet.languages.csharp . However there are also more specialized newsgroups for technologies like Windows forms, GDI+ and ASP.NET.

quote:
2. I have a TreeView on a form that has a context menu assigned to it. When I right click to bring up the menu, it pops up and selects the node I right-clicked on. Then when I press one of the menu items, or cancel the menu by clicking outside of it, the node selected returns to the previously selected node. The problem is, I cant figure out how to identify the node selected when the context menu is up in the code. Anybody know how?

Wouldn''t the public TreeNode GetNodeAt(int, int); method be useful here?
quote:
3. I have successfully inherited a TreeNode into the class I made called TreeNodeEx, which has some extra variables that I need. I can add/edit/delete this new node class to any existing treeview using casting and all seems well. I tried to do the same thing with a ListViewItem by inheriting it to a ListViewItemEx class and adding a few variables. Suprisingly, I cannot add this ListViewItemEx to a ListView class, no matter what I do. Question is, why can''t I do this?

What happens when you try?

"When you know the LORD you have no need for masturbation!"
--AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [IRC channel] [Blog]
Yep, use the GetNodeAt() method. If you need further accountability for each node...there is an Index property on TreeNodes. Also, there is a Tag property on nodes, that you could set to a GUID for each of the nodes.

"Wisdom is proportionate to your reference base. Intelligence is the ability to appropriately use that Wisdom."


[edited by - Leathrewulfe on August 29, 2002 10:14:15 PM]
"Wisdom is proportionate to your reference base. Intelligence is the ability to appropriately use that Wisdom."
Inheritance may not be required for "adding a few variables" to a tree or list. You should look into the Tag object. It has a infinate number of cool uses :-)

-SniperBoB-

-SniperBoB-

What if The Matrix was recursive?
quote:Wouldn''t the public TreeNode GetNodeAt(int, int); method be useful here?


I believe it is useful, I didnt even see it. I tried to implement this but I then realised that the PopUp event handler doesnt pass the mouse position, and I cant find information about getting the mouse position independently. Any help with this?

quote:What happens when you try?

quote:You should look into the Tag object. It has a infinate number of cool uses :-)


Both of these options throw an invalid cast exception.

Here are some code snippets of how I used the tag object.

  //Here is my own derived tag class//--stripped of 2 other long properties for simplicity--public class TagEx: object{	private long m_UniqueID;	public TagEx(): base()	{		m_UniqueID = 0L;	}	public TagEx(long UniqueID): this()	{		m_UniqueID = UniqueID;	}	public long UniqueID	{		get		{			return m_UniqueID;		}		set		{			m_UniqueID = value;		}	}}//In another section of code I do the followingSystem.Windows.Forms.ListViewItem li = new System.Windows.Forms.ListViewItem("New Item");//this next line throws the invalid cast exceptionli.Tag = new TagEx(1L);  
You can try CodeProject for C# forums. The web site also has tons of code for MFC and general Windows programming.

quote:Original post by ISOPimp
I cant find information about getting the mouse position independently. Any help with this?

System.Windows.Forms.Cursor.Point returns the cursor position in screen coordinates.

quote:
public class TagEx: object

Deriving explicitly from object is redundant, but harmless.
quote:
li.Tag = new TagEx(1L);

There is no reason whatsoever that line would throw InvalidCastException, so I would venture to guess that throw comes from within the ctor proper. Maybe the problem is with the two properties you omitted?

"When you know the LORD you have no need for masturbation!"
--AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [IRC channel] [Blog]
Thank you very much Arild and others. The problem with the casting was one of those "I can''t believe I didn''t see that" type problems. I was doing 2 other casts on the same line to get some DataRow elements into the TagEx but I was casting the wrong column.

All problems are now solved (for now). Thanks again.

This topic is closed to new replies.

Advertisement