Click here to Skip to main content
15,667,010 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:

I'm trying something I've never done before ! I don't know if it's possible or not!

when we use WinForm ( add new form in visual studio : form1 for example ), we have this class for Label Controls (Label [from metadata]):
(this class appears when you press F12 when the cursor is on "Label" word)

public class Label : Control
       protected override void OnTextChanged(EventArgs e);

OK. Now I wanna override the OnTextChanged event by writing some code !
How should I do this ??

I think I should derive a subclass from Label class and then override the function like this

public class Class1 : Label
    protected override void OnTextChanged(EventArgs e)

if so, how and where should I add this class?
if not, how can I override functions like this ( which are defined inside the control itselt ) ?

Thanks in advance.
Updated 25-Feb-13 19:07pm
Sergey Alexandrovich Kryukov 26-Feb-13 1:29am    
You mixed up some control types. Who told you that this method exists in panel?
Sergey Alexandrovich Kryukov 26-Feb-13 1:31am    
Despite of a very naive question, it is reasonably formulated: WinForms is tagged, sufficient code sample is shown. Only messed up a bit.
I voted 4 for the question. These days, even this level of inquirer's literacy is quite rare, most requests are not even questions.

I think you have a fair potential to learn.

1 solution

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:[^].

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; // this will add your control to UI
// alternative form:
// somePanel.Controls.Add(textBox);

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.

Share this answer
Sergey Alexandrovich Kryukov 26-Feb-13 2:02am    
Will you please put it in the question, the same one or a separate one and format with "pre" tags as you existing question?
If you want me to see it, please reply to this comment.
In the meanwhile, as you agree my answer if complete enough, please accept it formally (green button).
Sergey Alexandrovich Kryukov 26-Feb-13 2:04am    
Ah, it's OK, I'll answer.
Write MyLabel MyLabel = new MyLabel not in the method (the reference is created on stack, goes nowhere, will simply be removed by GC), but as a member of the class.
Insert it in UI in the form constructor. If you use the designer, it will be added in your InitalizeComponents.
If you want to write the whole form class without the designer (why not), you don't need InitalizeComponents, but you better have some "set up" function to be called from your constructor, anyway. This way, the child controls will be added in constructor, before the form is shown...
Mohamad77 26-Feb-13 2:11am    
Thansk alot SA, I've just got confused for a minute ! :D
I've solve my second question, and I've added the newlabel(with new method) to my winform.
thanks again for checking my problem.
Sergey Alexandrovich Kryukov 26-Feb-13 2:15am    
My pleasure. Hope you are going in right direction and will master it all pretty soon.
Good luck, call again.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900