Setting a Format for Items Displayed in a List Based Control

You can format the items that you display in a list-based control. For example, if you are displaying a list of monetary values, you can format them all as currency, and they will be displayed in the currency format that is appropriate to the culture the application is running under.

You can set a format for a list-based control by setting the FormatString property at design time. Selecting and clicking the FormatString property in the Properties window launches the Format String Dialog dialog box, shown in Figure 3-3.

Figure 3-3 The Format String Dialog dialog box

The FormattingEnabled property determines whether to use the formatting indicated by the FormatString. When the FormattingEnabled property is set to True, the entries in the control will be displayed in the format indicated by FormatString property.

Custom Format Strings If the preset format strings do not provide the correct format for an item, you can create a custom format string. Table 3-4 describes the characters that can be used to create a custom format string.

Table 3-4 Custom Format String Characters

Character Description

0 Zero placeholder. If the value being formatted has a digit in the position where the '0' appears in the format string, then that digit is copied to the result string. The position of the left-most'0' before the decimal point and the right-most '0' after the decimal point determines the range of digits that are always present in the result string. Note that the "00" specifier causes the value to be rounded to the nearest digit preceding the decimal, where rounding away from zero is always used. For example, formatting 34.5 with "00" would result in the value 35.

# Digit placeholder. If the value being formatted has a digit in the position where the '#' appears in the format string, then that digit is copied to the result string. Otherwise, nothing is stored in that position in the result string. Note that this specifier never displays the '0' character if it is not a significant digit, even if '0' is the only digit in the string. It will display the '0' character if it is a significant digit in the number being displayed. The "##" format string causes the value to be rounded to the nearest digit preceding the decimal, where rounding away from zero is always used. For example, formatting 34.5 with "##" would result in the value 35.

. Decimal separator. The first '.' character determines the loca tion of the first decimal separator in the formatted value. Additional '.' characters are ignored. Note that the actual character used will be the decimal separator determined by the current locale.

Table 3-4 Custom Format String Characters

Character Description

, Thousands separator and scaling. First, if the format string contains a ',' character between two digit placeholders (0 or #) and to the left of the decimal point if one is present, then the output will have thousand separators inserted between each group of three digits to the left of the decimal separator. The actual character used as the decimal separator in the result string is determined by the NumberGroupSeparator property of the current NumberFormatlnfo that controls formatting.

If the format string contains one or more ',' characters immediately to the left of the decimal point, then the number will be divided by the number of ',' characters multiplied by 1000 before it is formatted. For example, the format string "0,," will represent 100 million as simply 100.

% Percentage placeholder. The presence of the % symbol causes the number represented to be multiplied by 100 before formatting. The % symbol appears in the place that it occurs in the format string.

E0, E+0, E-0, e0, Scientific notation. If any of the strings "E", "E+", "E-", "e", "e+", e+0, e-0 or "e-" are present in the format string and are followed imme diately by at least one '0' character, then the number is formatted using scientific notation with an 'E' or 'e' inserted between the number and the exponent. The number of '0' characters following the scientific notation indicator determines the minimum number of digits to output for the exponent. The "E+" and "e+" formats indicate that a sign character (plus or minus) should always precede the exponent. The "E", "E-", "e", or "e-" formats indicate that a sign character should only precede negative exponents.

\ Escape character. In C#, this character is used to indicate that the character immediately following the '\' is to be interpreted as an escape sequence. In Visual Basic this character has no effect.

Table 3-4 Custom Format String Characters

Character Description

"ABC", 'ABC' Literal strings. Characters enclosed in "" or '' are displayed as literal strings in the formatted string.

; Section separator. The ';' character is used to separate sections for positive, negative, and zero numbers in the format string. You can provide up to three sections in a format string, each containing its own format. These sections should be separated by ';' characters and will be applied to positive, negative, and zero numbers respectively.

Other Characters Other characters in the format string are represented as literal strings.

Was this article helpful?

0 0

Post a comment