简体   繁体   中英

Issue in displaying “-” symbol in calculator application in C# WPF

Hi i am trying to learn windows presentation foundation WPF and was trying to develop the simplest application Calculator. But i am having issue in displaying the '-' when i substract a higher value from smaller. For eg. if i do something like this "10 - 20" the output should be "-10" in the screen ie textbox. But it is displaying "10-". Somehow the '-' is coming in the end. my xaml code for textbox looks like following:

    <TextBox Height="33" HorizontalAlignment="Left" Name="outputbox" 
             VerticalAlignment="Top" Width="278" 
             FontFamily="Tahoma" FontSize="18" 
             FlowDirection="righttoleft" IsReadOnly="True" />

and the code for doing substraction and display looks something like this

      if (entry1 > entry2)
      {
        outputbox.Text = (entry1 - entry2).ToString();
      }
      else 
      {
        outputbox.Text = "-" + (entry2 - entry1).ToString();
      }

while debugging it shows the proper string as "-10" but while displaying in the textbox it is showing the string "10-". Any idea about what is missing???

Just remove the FlowDirection attribute from your TextBox and your result will be fine.

<TextBox Height="33" 
         HorizontalAlignment="Left" 
         Name="outputbox" VerticalAlignment="Top" 
         Width="278" 
         FontFamily="Tahoma" 
         FontSize="18"  IsReadOnly="True" />

Or you may specify FlowDirection="LeftToRight" which is the default for the TextBox

Thanks All. I removed the "FlowDirection" property and it displayed the proper "-10" but the text was now displayed on the left side of textbox. So i used the "TextAlignment = right" property and it seems to be working fine. Thanks again all of you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM