Reputation: 4969
I'm trying to override a simple method within a standard TreeView control;
public class treeView1 : TreeView
{
protected override void OnMouseUp(MouseEventArgs e)
{
MessageBox.Show("Are we getting here?");
base.OnMouseUp(e);
}
}
It seems simple, but I can't understand why it's not being called. I call inherit from the TreeView control, and do a basic override. The TreeView is called treeView1
and responds to event method on it's parent class, just not the overridden ones, why!?
Also I don't want to create a custom user-control, just want to keep this basic. Thanks.
EDIT: I created the treeview in the Form1.Designer.cs
private System.Windows.Forms.TreeView treeView1;
& is initialised with the following;
//
// treeView1
//
this.treeView1.Location = new System.Drawing.Point(13, 316);
this.treeView1.Name = "treeView1";
this.treeView1.Size = new System.Drawing.Size(539, 474);
this.treeView1.TabIndex = 2;
this.treeView1.AfterSelect += new System.Windows.Forms.TreeViewEventHandler(this.treeView1_AfterSelect);
this.treeView1.MouseUp += new System.Windows.Forms.MouseEventHandler(this.treeView1_MouseUp);
Upvotes: 0
Views: 797
Reputation: 71573
With this declaration, you are creating a new "type", which is derived from TreeView and which can be used to declare instances that behave in the customized way.
You didn't show your usage code (EDIT - now you have), but I would bet a nickel that the actual control that displays on the form is still declared:
protected System.Windows.Forms.TreeView treeView1;
If that is indeed the case, you're not using your derived type; the actual control is still an instance of the built-in TreeView class, not your derived class. So, at runtime, your object's method won't be called because your object isn't even being used.
Instead, you must declare the control as an instance of your new class:
protected treeView1 treeView1;
... and then also make sure it is instantiated as such:
private void InitializeComponents()
{
...
treeView1 = new treeView1(); //not System.Windows.Forms.TreeView
...
}
To avoid confusion between the type and the instance of that type, I would define the derived class using UpperCamelCase naming convention: TreeView1
.
EDIT: Thanks for showing the usage. It's definitely what I thought it was; your variable treeView1 is an instance of the built-in System.Windows.Forms.TreeView, not your custom derived treeView1 class. There is a big difference between the name of a class and the name of the instance of a class. The proper declaration should be:
private treeView1 treeView1;
... and I reiterate that the class name should use UpperCamelCase naming convention:
public class TreeView1 : TreeView
{
protected override void OnMouseUp(MouseEventArgs e)
{
MessageBox.Show("Are we getting here?");
base.OnMouseUp(e);
}
}
...
//now when declaring it it'll be easier to tell the difference
private TreeView1 treeView1;
Upvotes: 2
Reputation: 4463
The event should work correctly. Based on your class name I will assume that you are not implementing it correctly.
First you need to create a new class and inherit from treeview:
class MyTreeView : TreeView
Add your override method
class MyTreeView : TreeView
{
protected override void OnMouseUp(MouseEventArgs e)
{
MessageBox.Show("Are we getting here?");
base.OnMouseUp(e);
}
}
Then make sure your compile your project once. After that the a new control with the name of your class should be under Components in the toolbox.Just drag and drop that control in your form and use that one from that point on.
Upvotes: 0