Visual C# Developer Center > Visual C# Forums > Visual C# General > Problem about override a control's property
Ask a questionAsk a question
 

AnswerProblem about override a control's property

  • Friday, November 06, 2009 9:07 AMLe Vo Quang Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code
    I make a custom control from TextBox control. I override Text property which only have get method:
            public override string Text
            {
                get
                {
                    return base.Text;
                }
                /*set
                {
                    base.Text = value;
                }*/
            }
    


    When I create a object of my custom control. I still be able to set the Text property.
    MyTextBox tb = new MyTextBox();
    tb.Text = "abc"; // this line is ok. Why?
    


    Please help me how to limit set method on Text property. Thanks.

Answers

  • Friday, November 06, 2009 9:49 AMRoman Malinovski Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Hi Le Vo Quang,

    You need a new version of the Text property. Try to use this code:

            public new string Text
            {
                get { return base.Text; }
            }


    Roman, software developer at Coherent Solutions company.
    • Proposed As Answer byVinilV Friday, November 06, 2009 9:58 AM
    • Marked As Answer byLe Vo Quang Friday, November 06, 2009 10:17 AM
    •  

All Replies

  • Friday, November 06, 2009 9:32 AMNetAdvance Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    I think this is beacuse you just comment out the code and didn't override the method.
    If you just comment out the get property, you still can use the get property.

    So, if you don't want anybody change the Text, maybe you can try

            public override string Text
            {
                set
                {
                    base.Text = base.Text;
                }
           }

    Hope this can help you.

  • Friday, November 06, 2009 9:44 AMLe Vo Quang Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    I think this is not good solution. You must understand that I only change control's Text from another property.
    At the moment, I am developing a NumericTextBox, I will create a Value property which represent a numeric. When Value property is changed, the Text also change.

    Ex: Value=1234567 -> Text = "1.23M"

    Please understand my purpose. Thanks.
  • Friday, November 06, 2009 9:49 AMRoman Malinovski Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Hi Le Vo Quang,

    You need a new version of the Text property. Try to use this code:

            public new string Text
            {
                get { return base.Text; }
            }


    Roman, software developer at Coherent Solutions company.
    • Proposed As Answer byVinilV Friday, November 06, 2009 9:58 AM
    • Marked As Answer byLe Vo Quang Friday, November 06, 2009 10:17 AM
    •  
  • Friday, November 06, 2009 10:17 AMLe Vo Quang Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi Le Vo Quang,

    You need a new version of the Text property. Try to use this code:

            public new string Text
            {
                get { return base.Text; }
            }


    Roman, software developer at Coherent Solutions company.

    It works. A difference between new and override keywords. Thanks.