0

i have a text block where all text display on button click by user.

XAML

<TextBlock x:Name="TextDisplay" TextAlignment="Center"
                       Style="{StaticResource PhoneTextTitle1Style}" />


<Button Style="{StaticResource MyButtonStyle}" Content="1" Click="Number_Click" />
<Button Style="{StaticResource MyButtonStyle}" Content="2" Click="Number_Click" />
<Button Style="{StaticResource MyButtonStyle}" Content="3" Click="Number_Click" />
<Button Style="{StaticResource MyButtonStyle}" Content="4" Click="Number_Click" />
<Button Style="{StaticResource MyButtonStyle}" Content="5" Click="Number_Click" />
<Button Style="{StaticResource MyButtonStyle}" Content="6" Click="Number_Click" />
<Button Style="{StaticResource MyButtonStyle}" Content="7" Click="Number_Click" />
<Button Style="{StaticResource MyButtonStyle}" Content="8" Click="Number_Click" />
<Button Style="{StaticResource MyButtonStyle}" Content="9" Click="Number_Click" />
<Button Style="{StaticResource MyButtonStyle}" Content="0" Click="Number_Click" />

And i want when user click on button content of button show in text block, but in this limited format ("###.##") only.

C#

    private void Number_Click(object sender, RoutedEventArgs e)
    {
        Button b = (Button)sender;
        TextDisplay.Text += b.Content.ToString("###.##");
    }

but show error on ToString when i erase "###.##" code works, but button can be click infinitely.

Platform : windows phone silverlight app /c# help if any body know to show text in textblock in "###.##" this limited format only.

Shubham Sahu
  • 1,963
  • 1
  • 17
  • 34

2 Answers2

1

you can use

String.Format("{0:000.00}", value);

like

TextDisplay.Text += String.Format("{0:000.00}", b.Content);

The "0" custom format specifier serves as a zero-placeholder symbol. If the value that is being formatted has a digit in the position where the zero appears in the format string, that digit is copied to the result string; otherwise, a zero appears in the result string. The position of the leftmost zero before the decimal point and the rightmost zero after the decimal point determines the range of digits that are always present in the result string.

Mohit S
  • 13,723
  • 6
  • 34
  • 69
  • Okay but by this code when user click on button text not formatted in my given format "000.00", i try your code , but text show as many length type by user without any decimal after three digit "000.00" – Shubham Sahu Oct 26 '16 at 07:46
  • Okay i am checking – Shubham Sahu Oct 26 '16 at 07:59
  • Okay, currently i am not with pc once i check , definitely i will Mark and upvote working answer – Shubham Sahu Oct 26 '16 at 12:15
  • TextDisplay.Text = String.Format("{0:###.##}", b.content); works – Shubham Sahu Oct 26 '16 at 14:24
  • Your first answer is right, i made two handlers one for button click to show button content to text block and another for what app to do after click of button then i delete button click event and combine button click event code to first one then your code worked – Shubham Sahu Oct 27 '16 at 04:41
  • Thanks @ShubhamSahu. Happy to help. Happy Coding. – Mohit S Oct 27 '16 at 04:43
1

The content is not an numeric value but a string (content = "1")

So if you want a specific numeric format like "###.##", you should convert the string "1" to a numeric type:

double.Parse(b.Content.ToString()).ToString("###.##")

Then your format will work.

Instead of "###.##" you'll maybe want "000.00"?

  • Okay i am trying your code and if work , i will upvote your answer – Shubham Sahu Oct 26 '16 at 07:48
  • sorry but, i tried your code but if i use 000.00 (e.g when type 1 it show 001.00 and then i type next digit e.g 5 it show 00.5.00 and remove 001.00) but i want type text continuously in ###.## (e.g 105.56) and if i use ###.## text show simple without any format and without limit to this format ###.## – Shubham Sahu Oct 26 '16 at 07:56
  • @ShubhamSahu: OK, but that is a slightly different question than your text indicates. –  Oct 26 '16 at 08:02