I am doing a calculator.
How to do a comma here?
My CultureInfo is se-SE
XAML:
<Window x:Class="Calculator.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Calculator"
mc:Ignorable="d"
Title="Calculator" Height="361.313" Width="208.941" ResizeMode="NoResize" WindowStartupLocation="CenterScreen" PreviewKeyDown="Window_KeyDownPreview">
<Grid HorizontalAlignment="Left" Width="199">
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition Height="165*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="203*"/>
<ColumnDefinition Width="6*"/>
</Grid.ColumnDefinitions>
<Button x:Name="One" Content="1" HorizontalAlignment="Left" Margin="13,227.717,0,0" VerticalAlignment="Top" Width="40" Height="40" FontSize="18" Click="Button_Click" Grid.Row="1"/>
<Button x:Name="Two" Content="2" HorizontalAlignment="Left" Margin="58,227.717,0,0" VerticalAlignment="Top" Width="40" Height="40" FontSize="18" Click="Button_Click" Grid.Row="1"/>
<Button x:Name="Three" Content="3" HorizontalAlignment="Left" Margin="103,227.717,0,0" VerticalAlignment="Top" Width="40" Height="40" FontSize="18" Click="Button_Click" Grid.Row="1"/>
<Button x:Name="Four" Content="4" HorizontalAlignment="Left" Margin="13,182.717,0,0" VerticalAlignment="Top" Width="40" Height="40" FontSize="18" Click="Button_Click" Grid.Row="1"/>
<Button x:Name="Five" Content="5" HorizontalAlignment="Left" Margin="58,182.717,0,0" VerticalAlignment="Top" Width="40" Height="40" FontSize="18" Click="Button_Click" Grid.Row="1"/>
<Button x:Name="Six" Content="6" HorizontalAlignment="Left" Margin="103,182.717,0,0" VerticalAlignment="Top" Width="40" Height="40" FontSize="18" Click="Button_Click" Grid.Row="1"/>
<Button x:Name="Seven" Content="7" HorizontalAlignment="Left" Margin="13,137.717,0,0" VerticalAlignment="Top" Width="40" Height="40" FontSize="18" Click="Button_Click" Grid.Row="1"/>
<Button x:Name="Eight" Content="8" HorizontalAlignment="Left" Margin="58,137.717,0,0" VerticalAlignment="Top" Width="40" Height="40" FontSize="18" Click="Button_Click" Grid.Row="1"/>
<Button x:Name="Nine" Content="9" HorizontalAlignment="Left" Margin="103,137.717,0,0" VerticalAlignment="Top" Width="40" Height="40" FontSize="18" Click="Button_Click" Grid.Row="1"/>
<Button x:Name="Zero" Content="0" HorizontalAlignment="Left" Margin="13,273.717,0,0" VerticalAlignment="Top" Width="40" Height="40" FontSize="18" Click="Button_Click" Grid.Row="1"/>
<Button x:Name="Plus" Content="+" HorizontalAlignment="Left" Margin="148,227.717,0,0" VerticalAlignment="Top" Width="40" Height="40" FontSize="18" Click="Button_Operand" Grid.Row="1" />
<Button x:Name="Minus" Content="-" HorizontalAlignment="Left" Margin="148,182.717,0,0" VerticalAlignment="Top" Width="40" Height="40" FontSize="18" Click="Button_Operand" Grid.Row="1"/>
<Button x:Name="Multiply" Content="×" HorizontalAlignment="Left" Margin="148,137.717,0,0" VerticalAlignment="Top" Width="40" Height="40" FontSize="18" Click="Button_Operand" Grid.Row="1"/>
<Button x:Name="Equals" Content="=" HorizontalAlignment="Left" Margin="103,273.717,0,0" VerticalAlignment="Top" Width="85" Height="40" FontSize="18" Click="Button_Equals" Grid.Row="1"/>
<Button x:Name="Dot" Content="." HorizontalAlignment="Left" Margin="58,273.717,0,0" VerticalAlignment="Top" Width="40" Height="40" FontSize="18" Click="Button_Click" Grid.Row="1"/>
<Button x:Name="Clear" Content="C" HorizontalAlignment="Left" Margin="103,92.717,0,0" VerticalAlignment="Top" Width="40" Height="40" FontSize="18" Click="Clear_Click" Grid.Row="1"/>
<Button x:Name="Divide" Content="÷" HorizontalAlignment="Left" Margin="148,92.717,0,0" VerticalAlignment="Top" Width="40" Height="40" FontSize="18" Click="Button_Operand" Grid.Row="1"/>
<Button x:Name="Clear_Copy" Content="CE" HorizontalAlignment="Left" Margin="13,92.717,0,0" VerticalAlignment="Top" Width="85" Height="40" FontSize="18" Click="CE_Click" Grid.Row="1"/>
<Label x:Name="Equation" Content="" HorizontalAlignment="Left" Margin="12,16.717,0,0" VerticalAlignment="Top" Width="118" Height="31" Grid.Row="1"/>
<Label x:Name="Display" Content="0" Height="71" Margin="10,16.717,0,0" VerticalAlignment="Top" Width="175" HorizontalAlignment="Left" FontSize="22" HorizontalContentAlignment="Right" BorderThickness="7,0,0,0" VerticalContentAlignment="Center" Grid.Row="1"/>
</Grid>
</Window>
c#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace Calculator
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
Double Value = 0; // Store Decimal and Int Values if needed
String Operand; // Store the Operand - Will Determine the Maths
Boolean OperandPressed;
public MainWindow()
{
InitializeComponent();
}
private void Clear_Click(object sender, RoutedEventArgs e)
{
Value = 0;
Display.Content = "0";
Equation.Content = "";
}
private void CE_Click(object sender, RoutedEventArgs e)
{
Display.Content = "0";
}
private void Button_Click(object sender, RoutedEventArgs e)
{
Button B = (Button)sender; // Cast Generic Object To Buttom
//To Get Rid of the Initial 0 in the Display
if ((Display.Content.ToString() == "0")||(OperandPressed))
{
Display.Content = " ";
}
// If Decimal Point already exsists within the Display then do not add another Dedcimal Point
if (B.Content.ToString() == ".")
{
if(!Display.Content.ToString().Contains("."))
{
Display.Content = Display.Content.ToString() + B.Content.ToString(); // Append Number onto Display
}
}
else
{
Display.Content = Display.Content.ToString() + B.Content.ToString(); // Append Number onto Display
}
OperandPressed = false;
}
private void Button_Operand(object sender, RoutedEventArgs e)
{
Button B = (Button)sender; // Cast Obj to Button
// Continously operate on the current results without having to press equals.
if(Value != 0)
{
Button_Equals(this, null); // emulate the equals button press
Operand = B.Content.ToString(); // store operand
OperandPressed = true;
Equation.Content = Value + " " + Operand; // Update the Equation Label
}
else
{
Operand = B.Content.ToString();
Value = Double.Parse(Display.Content.ToString()); // Store the First Value
OperandPressed = true;
Equation.Content = Value + " " + Operand;
}
}
private void Button_Equals(object sender, RoutedEventArgs e)
{
Equation.Content = " ";
/*Maths is dependent on which operand has been pressed:
Do the correct maths with first value stored earlier and second value which is currently on the display. */
switch (Operand)
{
case "+":
Display.Content = Value + Double.Parse(Display.Content.ToString());
break;
case "-":
Display.Content = Value - Double.Parse(Display.Content.ToString());
break;
case "×":
Display.Content = Value * Double.Parse(Display.Content.ToString());
break;
case "÷":
Display.Content = Value / Double.Parse(Display.Content.ToString());
break;
default:
break;
}
Value = Double.Parse(Display.Content.ToString()); //Whatever Last one display is stored in Value
Operand = ""; // Clear Operand once pressed
}
/*Functionality Added to make the calculator work with Keyboard NumberPad
When the correct Key is detected it will fire off the corresponding button press*/
private void Window_KeyDownPreview(object sender, KeyEventArgs e)
{
switch (e.Key)
{
case Key.NumPad0:
Zero.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));
break;
case Key.NumPad1:
One.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));
break;
case Key.NumPad2:
Two.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));
break;
case Key.NumPad3:
Three.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));
break;
case Key.NumPad4:
Four.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));
break;
case Key.NumPad5:
Five.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));
break;
case Key.NumPad6:
Six.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));
break;
case Key.NumPad7:
Seven.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));
break;
case Key.NumPad8:
Eight.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));
break;
case Key.NumPad9:
Nine.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));
break;
case Key.Add:
Plus.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));
break;
case Key.Subtract:
Minus.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));
break;
case Key.Multiply:
Multiply.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));
break;
case Key.Divide:
Divide.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));
break;
case Key.Enter:
Equals.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));
break;
}
}
}
}
