Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# .NET WinForm
Hi,
 
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)
    {
        MessageBox.Show("S");
    }
}
 
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.
Posted 25-Feb-13 20:06pm
Edited 25-Feb-13 20:07pm
v2
Comments
Sergey Alexandrovich Kryukov at 26-Feb-13 1:29am
   
You mixed up some control types. Who told you that this method exists in panel?
—SA
Sergey Alexandrovich Kryukov at 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.
Cheers,
—SA

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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; // 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.
 
—SA
  Permalink  
v3
Comments
Sergey Alexandrovich Kryukov at 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).
—SA
Sergey Alexandrovich Kryukov at 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...
—SA
Mohamad77 at 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 at 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.
—SA

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

  Print Answers RSS
0 OriginalGriff 365
1 Sergey Alexandrovich Kryukov 319
2 CPallini 275
3 DamithSL 214
4 Maciej Los 185
0 OriginalGriff 5,455
1 DamithSL 4,457
2 Maciej Los 3,885
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,115


Advertise | Privacy | Mobile
Web01 | 2.8.141216.1 | Last Updated 26 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100