locked
How to format a single TextBox

    Question

  • HI,

    Coming from a ‘Oracle Developer’ background and just entering C# developments I am kind of disappointed with C#.

    I mean you have to write tons of code to do anything. Also the number of web sites you have to go to and also the amount of on-line help you have to read in order to learn even a simple operation really frustrating.

    All I want to do is this.  I have a database table called EMPLOYEES and I have created a form for it .  The form works fine. 

     

    All I want to do is format the SALARY column to this format: 999,999,990.00 (I.E. if salary is 49749.55 then it should display 49,749.55). 


    This format should be applied automatically when ever the SALARY textbox gets a value, either when the user types in a value or when brought by the database during loading and browing.
     

    How can u do this simple operation?  In Oracle Developer all you have to do is put the format mast in the Format property of the textbox.

     

    I have been searching the web for 3 days and also reading the C# help but have not come across anything that will help me.

     

    Can somebody please tell me a solution to this.   Is it me or is C# (and similar languages such as C++ and Java) as difficult or is it that I am not looking at the right places or not going the right way when it comes to learning C#?

     

    I would be really grateful if you could help me out here.

     

    Thanks in advance & with best regards,

     

    MCFS

     

    Friday, July 22, 2005 10:37 AM

Answers

  • (CHECK OUT THE SOLUTION GIVEN BY A MEMBER OF www.gotdotnet.com

    THIS WORKDS


    Well having used Oracle for a number of years and having to deal with the inadequacies of it's coding structures, I consider us even ;/

    Anyways, I can't believe that you couldn't find this in VS Help, but here goes:

    // You only need these lines if valueToConvert
    // is a string value.
    string valueToConvert = valueToConvert.Replace("$", "");
    valueToConvert = valueToConvert.Replace(",", "");
    decimal numericValue =
      Convert.ToDecimal(valueToConvert);

    // Format the value...
    string displayValue = numericValue.ToString("C");

    Also, you DO know (being an ‘Oracle Developer’) that you can format the value in your SQL statement don't you:
    SELECT TO_CHAR(value, '$99,999') AS aliasName

    NC...

    Friday, July 22, 2005 1:44 PM

All replies

  • What you are looking for is the MaskedTextBox. This control is available in VS 2005. This control is not available in Visual Studio 2003. If you search for MaskedTextBox I'm sure you'll find some custom made controls that'll do the job.

    for instance:
    http://www.c-sharpcorner.com/Code/2003/March/MaskEditControl.asp

    Marinus
    Friday, July 22, 2005 11:32 AM
  • Thanks, but I still say that this should be a feature in any programming language 4GL or 3GL!
    Friday, July 22, 2005 1:12 PM
  • (CHECK OUT THE SOLUTION GIVEN BY A MEMBER OF www.gotdotnet.com

    THIS WORKDS


    Well having used Oracle for a number of years and having to deal with the inadequacies of it's coding structures, I consider us even ;/

    Anyways, I can't believe that you couldn't find this in VS Help, but here goes:

    // You only need these lines if valueToConvert
    // is a string value.
    string valueToConvert = valueToConvert.Replace("$", "");
    valueToConvert = valueToConvert.Replace(",", "");
    decimal numericValue =
      Convert.ToDecimal(valueToConvert);

    // Format the value...
    string displayValue = numericValue.ToString("C");

    Also, you DO know (being an ‘Oracle Developer’) that you can format the value in your SQL statement don't you:
    SELECT TO_CHAR(value, '$99,999') AS aliasName

    NC...

    Friday, July 22, 2005 1:44 PM
  • ToString("n2") will give you a number formatted with the system specified thousand seperators  and two decimal places.
    ToString("c") will give you a number formatted with system specified currency format, which will include 2 decimal places for US, UK, Australia, etc. and also the local currency symbol.
    ToString("c2") will give you a number formatted with the system specified currency format with 2 decimal places regardless of location.

    These method calls can be placed in the LostFocus event handler of a TextBox to allow the user to enter a number without worrying about any thing but the digits and the decimal point.  The MaskedTextBox does this also, but requires that you know the currency format to set the mask.  If you know it then fine, but the use of ToString localises automatically.  If you use this method you would want to remove the formatting when the TextBox gets focus again.
    Sunday, July 24, 2005 3:34 AM