Visual C# Developer Center >
Visual C# Forums
>
Visual C# General
>
Problem about override a control's property
Problem about override a control's property
- 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
- 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
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.- 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. - 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
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.


