Click here to Skip to main content
15,891,951 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
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
Comments
[no name] 2-Jan-13 11:20am    
hi,

Do you want to create custom textbox control?
Sergey Alexandrovich Kryukov 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.
—SA
Sergey Alexandrovich Kryukov 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.
—SA

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.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 2-Jan-13 12:01pm    
That's can be useful, a 5.
—SA
Abhinav S 2-Jan-13 12:04pm    
Thank you SA.
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:
C#
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
 
Share this answer
 
v4
Comments
nada2006 2-Jan-13 11:57am    
Thanks a lot for your help!Sergey,I'm using Windows forms applications ..
Sergey Alexandrovich Kryukov 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.

—SA
Sergey Alexandrovich Kryukov 2-Jan-13 13:35pm    
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!
Sergey Alexandrovich Kryukov 2-Jan-13 12:04pm    
Thank you, Abhinav.
—SA
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:
C#
    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; }
    }
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 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.
—SA

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