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);
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:
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:
… and then also make sure it is instantiated as such:
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:
… and I reiterate that the class name should use UpperCamelCase naming convention: