DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: DisplayFormat/DisplayStyle property of TextBox (Visual Studio 2010, .Net Framework 4)

  1. #1
    Join Date
    Mar 2009

    Question DisplayFormat/DisplayStyle property of TextBox (Visual Studio 2010, .Net Framework 4)

    Is there any predefined property by which I can set the DisplayFormat/DisplayStyle of a textbox? Suppose I want a textbox to hold the actual value 888888888888.88 and show it formatted as 888,888,888,888.88. In short it should hold 'Double' data type value and show it with thousand separators and decimal places upto two digits. If text is cleared from the textbox, it should have 0 as value and 0.00 as displayed value. Also the number of digits in the textbox may vary accordingly. So how can it be done? Please help. Regards.

  2. #2
    Join Date
    Feb 2004
    Longueuil, Québec
    No, there is no direct way of setting a display format on the TextBox as you can do with other controls surch as the cells in a DataGridView.

    The DataGridView has been designed primarily to display groups of data, and you might want to uniformise the way all the values are displayed otherwise the grid would become a mess. Since the user sees the previous rows when entering a piece of data, he will usually use the same format to type or edit a value.

    The TextBox however has been conceived for the user to type in individual values. The good practice in such a situation is that the user has the control on how to type and see the information. Since the user is supposed to have the control, Microsoft did not give an easy way for programmers to force an input or output format.

    You will need to to format the value with your own code everytime the value change. When you fill the TextBox if the data comes from the code, or in the Leave or Validating event when the user type in the TextBox. Validating is usually more interesting because it can be cancelled in special circumstances.

    I you want to have a specific format, use the String.Format method. In your case, I would however advise against that one unless the TextBox is ReadOnly. If for instance you display a coma as a thousand separator, but the Control Panel is set for a space, the user will try to type in comas, you will get an exception when you will try to convert the typed String into a Double.

    It would be better to format with Double.ToString. This method automatically takes the Control Panel into consideration.
    Jacques Bourgeois

  3. #3
    Join Date
    Mar 2009

Similar Threads

  1. App Object
    By Rob Teixeira in forum .NET
    Replies: 15
    Last Post: 05-31-2002, 03:30 PM
  2. yag's status report - April 4, 2002
    By Yair Alan Griver [MSFT] in forum vb.announcements
    Replies: 0
    Last Post: 04-04-2002, 10:28 PM
  3. Replies: 1
    Last Post: 02-23-2001, 09:58 AM
  4. Welcome to ASP+
    By Robert Scoble in forum ASP.NET
    Replies: 2
    Last Post: 07-20-2000, 02:35 PM
  5. Visual Studio.NET Resources
    By Robert Scoble in forum ASP.NET
    Replies: 0
    Last Post: 07-20-2000, 04:11 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center