Click here to Skip to main content
11,813,557 members (64,581 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: C#

I want to create a class which inherits textBox control in c# . the new class contains a new property (size)with 3 possible values (maximize ,minimize,center )

How I can do that?

Thanks a lot!

Posted 2-Jan-13 5:14am
Turbo_23 at 2-Jan-13 11:20am

Do you want to create custom textbox control?
Sergey Alexandrovich Kryukov at 2-Jan-13 11:38am
What don't you know? using inheritance is the basic OOP chore; if you don't know it, you should not be doing any UI development — just yet. Learn basics of OOP first; if you don't, you will just waste your time.
Sergey Alexandrovich Kryukov at 2-Jan-13 11:39am
Also, what kind of UI library do you use? In other words, give a full name of the TextBox type.
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

This detailed article Custom Controls in Visual C# .NET[^] should put you on the right path. After that, you can take it as an homework task to inherit the Textbox control and create a new property / enumeration.
Sergey Alexandrovich Kryukov at 2-Jan-13 12:01pm
That's can be useful, a 5.
Abhinav S at 2-Jan-13 12:04pm
Thank you SA.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Please see my advice given in my comment to the question. And follow it — there is no other way.

What you need will look something like this:
public class MyTextBox : System.Windows.Forms.TextBox {
    public enum SizeBehaviorOption { Maximize, Minimize, Center, }
    public SizeBehaviorOption SizeBehavior {
        get { return this.sizeBehavior; }
        set {
            if (this.sizeBehavior == value) return;
            this.sizeBehavior = value;
            // now, do something to adjust your control size/location/something, as the behavior has been changed
    SizeBehaviorOption sizeBehavior;
} //class MyTextBox

Note the role of the setter of the property. This is the main idea behind property: a setter (sometime getter, rarely) is used to add some side effect to read/write operation for the property values.

However, I suspect all this activity is redundant. You can usually implement desired behavior using docking containers, docking and padding properties, in some cases anchors. The further detail depend on UI library you want to use. Next time you ask a question, always tag this important detail.

nada2006 at 2-Jan-13 11:57am
Thanks a lot for your help!Sergey,I'm using Windows forms applications ..
Sergey Alexandrovich Kryukov at 2-Jan-13 12:03pm
You are welcome.
Thanks for clarification; next time add this detail as a tag with your question. In this case, use the tag "Forms".

Yes, my explanation is fully applicable to this case, I can just confirm it.
So, I think now you can formally accept this answer, too(green button) — thanks.
Of course, your follow-up questions, if you have any, will be welcome anyway.

Sergey Alexandrovich Kryukov at 2-Jan-13 13:35pm
I also adjusted the code/comments to reflect your clarification: System.Windows.Forms.TextBox, to be certain.
Abhinav S at 2-Jan-13 11:58am
Perfect. 5!
Sergey Alexandrovich Kryukov at 2-Jan-13 12:04pm
Thank you, Abhinav.
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

You can't - or at least you shouldn't.
The name Size is already a property of all classes derived from Control and if you override it to a different type, then it becomes difficult to work with.

But, to actually do it is easy:
    myTextbox myt = new myTextbox();
    myt.Size = Sizes.Maximize;
public enum Sizes
public class myTextbox : TextBox
    public new Sizes Size { get; set; }
Sergey Alexandrovich Kryukov at 2-Jan-13 12:07pm
For a record: it's quite possible to create an unrelated property with the same name; even the warning will not appear if "new" is used. But your "you shouldn't" is of course reasonable.
Also, one problem with your solution is the lack of setter side effect. Without some "refresh" operation (quotation makes are intended: this is not a call of the method with this name), the behavior immediately after assignment will be incorrect.

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

  Print Answers RSS
0 Abhinav S 275
1 Palash Mondal_ 190
2 CHill60 150
3 Sergey Alexandrovich Kryukov 134
4 KrunalRohit 129
0 OriginalGriff 3,877
1 KrunalRohit 2,496
2 CPallini 2,240
3 Maciej Los 2,205
4 ppolymorphe 1,750

Advertise | Privacy | Mobile
Web03 | 2.8.151002.1 | Last Updated 2 Jan 2013
Copyright © CodeProject, 1999-2015
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