hi there,

I have a mask text box with the mask as $9999999999.99 as money. when i type in a value as 12345.67 it appears as $_____12345.67 and when i save it to the database it is saved in the correct format as 12345.67 as money as the data type. but when i get the value to the same it is displayed as $1234567___.__

how can i format and display in the correct format??

thanxxxxxxxxxx

Recommended Answers

All 18 Replies

Are you saving it as a string or integer (/double/decimal etc.)?
Try .ToString("00000000.00")

commented: Good! +10

Are you saving it as a string or integer (/double/decimal etc.)?
Try .ToString("00000000.00")

i am taking it as a string parameter and the MS SQL datatype is money in the MDF file in visual studio.

thankx

i am taking it as a string parameter and the MS SQL datatype is money in the MDF file in visual studio.

thankx

Convert it to decimal.

decimal s;
decimal.TryParse(valueFromDb, out s);
maskedTextBox1.Text = s.ToString("$00000000.00");

Convert it to decimal.

decimal s;
decimal.TryParse(valueFromDb, out s);
maskedTextBox1.Text = s.ToString("$00000000.00");

hey,

how so it is 12345.67 is displayed as $0000012345.67 for the mask $0000000000.00 how can i avoid the 0 z in the value that is being displayed.

thankxxxxx

> how can i avoid the 0 z in the value that is being displayed.
You cannot. This is a limitation of the MaskedTextBox. Edward usually uses a regular TextBox and manages the format manually. For example, just a straight validation is super easy.

private void textBox1_Validating(object sender, CancelEventArgs e) {
    decimal value;

    if (decimal.TryParse(textBox1.Text, NumberStyles.Currency, null, out value)) {
        // Do something with the value
    } else {
        // Notify the user somehow
        e.Cancel = true;
    }
}

By handling text box events you can do some cool stuff. The MaskedTextBox is really just a convenience if you do not need absolute control and do not mind the formatting limitations.

commented: This is the correct way. +1

Ignore. Posted to wrong thread.

void textBox1_Validating(object sender, System.ComponentModel.CancelEventArgs e)
        {
            try
            {
                decimal decRetailCostResult;
                                   if (!(decimal.TryParse(textBox1.Text, out decRetailCostResult)))
                    {
                        // ... Do something
                        e.Cancel = true;
                    }
                    else
                    {
                        textbox1.Text = decRetailCostResult.ToString("0.##");
                     }
           catch (Exception Ex)
            {
                                    ShowError(Ex);
            }
        }

you have a property named 'ShowPlaceHolders', make it false.
you will not get '___'.
use this 'ShowPlaceHolders' through your code.

Thanks,
as a senior poster, i hope u knw, please mark it as solved if its working fine.
sry, just reminding you

you have a property named 'ShowPlaceHolders', make it false.
you will not get '___'.
use this 'ShowPlaceHolders' through your code.

Thanks,
as a senior poster, i hope u knw, please mark it as solved if its working fine.
sry, just reminding you

hey ,

i am using the mask text box to display the money amount in the mask text box,for this i am using the below code

String oamt = reader[13].ToString();

                if (oamt == "0.00")
                    mtxtOAmt.Text = "";
                else
                {
                    decimal d;
                    decimal.TryParse(oamt, out d);
                    mtxtOAmt.Text = d.ToString("$00000000000000.00");
                }

so that the value is displayed as "$00000000555555.55" how can i avoid the "0" in this

thankxxxxxx

ok, for the above case,

amountTextEdit.Text.TrimStart('0');
use this.

ok, for the above case,

amountTextEdit.Text.TrimStart('0');
use this.

yeah, but the thing is if u trim the 0 z the value will be displayed as "23423." not as "23423.00" so i wrote a code but it is too long.

if (amt == "0.00")
                    mtxtAmt.Text = "";
                else
                {
                    decimal d;
                    decimal.TryParse(amt, out d);
                    String Val1 = d.ToString("$00000000000000.00");
                    int index1 = Val1.IndexOf(".");
                    String val1 = Val1.Substring(0,index1);

                    String FValue = val1.Replace("0", " ");
                    String LValue = Val1.Substring(index1,(Val1.Length - index1));
                    mtxtAmt.Text = FValue + LValue;

                }

is there a short way of doing this???

thankxxxxxx

mtxtOAmt.Text.TrimStart('0');
use the above code, u can trim the 0 that is before the number,
but make sure, u take the substring before doing that..

i havent tried it though..

did u try the ShowPlaceHolder property...
I hope that would help you a lot, use it with the first post that you gave on this thread...
let me know the result.

did u try the ShowPlaceHolder property...
I hope that would help you a lot, use it with the first post that you gave on this thread...
let me know the result.

where is the ShowPlaceHolder property, i can't find it

Hi, if want a very simple method,
use a MaskedTextBox control instead of a normal text box,
there is a mask property available, "^(-)?\d+(\.\d\d)?$"

this will suffice your requirement.

Thanks.

Hi ,

In my case mask is 999999.99 and if i enter only 23 i'm getting 230000.00 but i need to show as 000023.00.. Which property do you have used to display like this.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.