Question DisplayFormat/DisplayStyle property of TextBox

priyamtheone

Well-known member
Joined
Sep 20, 2007
Messages
96
Programming Experience
Beginner
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.
(Visual Studio 2010, .Net Framework 4)
 
Use String Formatting?

I don't know of any properties that can set the formatting for a textbox, but you could just use String Formatting, should be fairly simple.

You can just set the Text property of the textbox and when you do use ToString or Format method to set the formatting.
Ex:

        Dim someNumber As Decimal = 888888888888.88
        TextBox1.Text = String.Format("{0:n2}", someNumber)
        'or something like this
        TextBox1.Text = someNumber.ToString("n2")

        'formats text to look like this:
        '   888,888,888,888.88
        '       and if number is zero then 0.00


Take a look at this link, found it through a quick search on Google. Some pretty decent reference for string formatting: http://john-sheehan.com/blog/wp-content/uploads/msnet-formatting-strings.pdf
 
NumericUpDown control may also be used.
 
Back
Top