Click here to Skip to main content
12,702,455 members (34,042 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C#
Hi,

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!

N.a.s
Posted 2-Jan-13 6:14am
Comments
Turbo_23 2-Jan-13 11:20am
   
hi,

Do you want to create custom textbox control?
   
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.
—SA
   
Also, what kind of UI library do you use? In other words, give a full name of the TextBox type.
—SA
Rate this: bad
 
good
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.
  Permalink  
Comments
   
That's can be useful, a 5.
—SA
Abhinav S 2-Jan-13 12:04pm
   
Thank you SA.
Rate this: bad
 
good
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.

—SA
  Permalink  
v4
Comments
nada2006 2-Jan-13 11:57am
   
Thanks a lot for your help!Sergey,I'm using Windows forms applications ..
   
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.

—SA
   
I also adjusted the code/comments to reflect your clarification: System.Windows.Forms.TextBox, to be certain.
Cheers,
—SA
Abhinav S 2-Jan-13 11:58am
   
Perfect. 5!
   
Thank you, Abhinav.
—SA
Rate this: bad
 
good
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;
    Controls.Add(myt);
    ...

public enum Sizes
    {
    Maximize,
    Minimize,
    Centre,
    }

public class myTextbox : TextBox
    {
    public new Sizes Size { get; set; }
    }
  Permalink  
Comments
   
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.
—SA

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



Advertise | Privacy | Mobile
Web02 | 2.8.170118.1 | Last Updated 2 Jan 2013
Copyright © CodeProject, 1999-2017
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