Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Override TextBox ".Text" web control

Status
Not open for further replies.

johnfraser

Programmer
Jul 25, 2007
33
US
I'm attempting to override a textbox control .Text property with the simple "AddCommasToNumber" when you call the Text Set property.

The catch is that I want the Text Get to return just the number.

Code:
control.Text = "1000";

//Displays "1,000";
string myString = control.Text;

//myString = 1000;

Well apparently to display the text it must call it's own "Text Get Property" because if I have it setup as below, the commas don't show on the form:

Code:
        public override string Text
        {
            get
            {
                //return text;
                return RemoveComma(text);

            }
            set
            {
                string dec = StoreDecimal(value, out value);
                value = AddComma(value);
                value = RestoreDecimal(value, dec);
                text = value;
            }
        }

So really want I'm looking for is to create a control that will display values with commas but setting and getting the values from the control itself is plain and simple.

Seems easy enough, apparently I'm missing something vital (or stupid) here.
 
why not just format the value? when assigning the value
Code:
decimal x = 1000
MyTextBox.Text = x.ToString("d");
this will produce 1,000.00
for more formatting pointers check out this pdf and this MSDN article

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Thanks, I forgot about that trick since I had to manually add the decimals on the fly in JS anyway, I just reused most of that code.

But that only solves have the question.... how to (behind the scene, convert from 1,000 to 1000?
 
it's a decimal as a string. the comma shouldn't matter.
when you pull the value from the textbox parse the text.
Code:
demcial d = Decimal.parse(MyTextBox.Text);
to avoid an exception use tryparse instead
Code:
demcial d = 0D;
Decimal.TryPrase(MyTextBox.Text, out d);

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Thanks Jason but I wanted the programmers to avoid using a tryparse and parse each and everytime.

Remember, the issue isn't the conversion from string to number. The problem is that I can't override the Text Property because internally to display the Text, it uses the Text property to display.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top