How to derive TreeNodeCollection class

Hi,
I am trying to extend the TreeView class in C#. I am not able to derive the TreeNodeCollection class. It seems that its constructor is private.
Is there any way to inherit this class?
LVL 1
tandreiAsked:
Who is Participating?
 
GuryTraubConnect With a Mentor Commented:
I dont understand what you are saying.
You are wrong, my friend
I did exactly this, in an application I wrote not long ago.
I derived from TreeNode.

and then you can do something like

TreeNodeCollection col;

col.Add(YourDerivedTreeNode node);

and it will work, and you won't lose your extensions..

I probably dont fully understand the problem, so please elaborate..
Gury
0
 
CoolAssCommented:
Well, TreeNodeCollection is not sealed... but you're correct, it doesn't have any constructor specified.

You don't need a constructor, however, to derive your new class.
0
 
tandreiAuthor Commented:
CoolAss,
Please tell me how, cause I get a compile error when I try to derive it because I can't override the constructor.
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
CoolAssCommented:
Paste the code and the error you're getting.
0
 
tandreiAuthor Commented:
public class BLTreeNodeCollection: System.Windows.Forms.TreeNodeCollection
{
  public BLTreeNodeCollection()
  {
  }
//...
}
0
 
CJ_SCommented:
Indeed no need to override

public class BLTreeNodeCollection: System.Windows.Forms.TreeNodeCollection
{
 public BLTreeNodeCollection() : base()
 {
    // in here do all YOUR code.
 }
//...
}
0
 
tandreiAuthor Commented:
public class BLTreeNodeCollection: System.Windows.Forms.TreeNodeCollection
{
  public BLTreeNodeCollection()
  {
  }
//...
}
0
 
tandreiAuthor Commented:
CJ_S,
I derived the class as you sugested, but I get the same error. I put no code in the constructor.


C:\Documents and Settings\Administrator\My Documents\Visual Studio Projects\BLTree\Class1.cs(10): No overload for method 'TreeNodeCollection' takes '0' arguments
0
 
tandreiAuthor Commented:
CJ_S,
I derived the class as you sugested, but I get the same error. I put no code in the constructor.


C:\Documents and Settings\Administrator\My Documents\Visual Studio Projects\BLTree\Class1.cs(10): No overload for method 'TreeNodeCollection' takes '0' arguments
0
 
tandreiAuthor Commented:
CJ_S,
I derived the class as you sugested, but I get the same error. I put no code in the constructor.


C:\Documents and Settings\Administrator\My Documents\Visual Studio Projects\BLTree\Class1.cs(10): No overload for method 'TreeNodeCollection' takes '0' arguments
0
 
GuryTraubCommented:
Why not inherit the TreeNode class itself, and use the old known TreeNodeCollection, but with yourTreeNode classes instead of the regular.

I did this in my application recently, and it works fine.
In fact in the same manner you can inherit ListViewItem, etc'..

That's the best way to do it I think..
Gury
0
 
CJ_SCommented:
Or have a public TreeNodeCollection object and you add the functions to the class itself.

I haven't been able to derive from it either.

CJ
0
 
tandreiAuthor Commented:
GuryTraub,
I wanted to do this , but how can I add my TreeNode to the system TreeNodeCollection?If my TreeNode extends the system TreeNode and I cast it to the system TreeNode in order to add it to the system TreeNodeCollection class I should lose all my extends!
0
 
DarthPedroCommented:
You wouldn't lose all of your extensions.  When you get your TreeNode out of the collection, you just need to cast it to your class, and then you can access the new methods/properties that you've added.
0
 
CoolAssCommented:
GuryTraub,

Please do not propose an answer like that. Use the comment post, then, if the person asking the question feels your comment has helped, he or she will accept it as the answer.
0
 
tandreiAuthor Commented:
Thanx GuriTraub, I already did that and it really works OK.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.