Your code would be quite correct, but… look properly
System.Windows.Forms.Label
does not have this method, but
System.Windows.Forms.TextBox
has. So, your code will be correct if you use
TextBox
base class and this method:
http://msdn.microsoft.com/en-us/library/z0bhdw6s.aspx[
^].
Only never use the names like "Class1". Use good Microsoft naming conventions; no numeric, give it semantic names. And using the word "Class" in a name of the class is as weird as the name "MyApplication.EXE" for application, "MyLabel" for some label, etc.
Now, by such override, you do not "override event". There is no such notion. You can only override method. And by overriding this method, you already handle the event accordingly, you don't need anything else. Alternatively, you can
handle corresponding event by adding an
event handler to an
invocation list of corresponding event instance of some instance of
TextBox
. This is a related alternative technique, but it was not a part of your question. Learn event by yourself; this knowledge is critically important for development.
As to the question "how and where should I add this class?", it makes no sense at all. You add it where you need it. If you don't know where, I'm not sure you need it. Use the general rules of the language and general semantic.
Basically, something like this:
public class MyTextBox : TextBox {}
MyTextBox textBox = new MyTextBox();
textBox.Parent = somePanel;
The designer will also be able to recognize your control type. There is nothing to do about that; when your code is compiled, look at the toolbox. If you add your custom control using the designer, eventually, it will generate some code equivalent to the code I've shown above.
—SA