Calculator Sample
This sample demonstrates the creation of a functional desktop calculator application using .NET technologies. It covers UI layout, event handling, and basic arithmetic operations.
Key Features:
- Intuitive user interface resembling a standard calculator.
- Support for basic arithmetic operations: addition, subtraction, multiplication, division.
- Handling of decimal numbers.
- Clear and clear-entry functionality.
Project Files:
The sample project consists of the following primary files:
CalculatorWindow.xaml
: Defines the visual structure of the calculator using XAML.CalculatorWindow.xaml.cs
: Contains the C# code-behind for the calculator's logic.App.xaml
: The application entry point.
XAML Structure (CalculatorWindow.xaml
):
The XAML defines the grid layout for the display and buttons. Here's a snippet of the button grid:
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<!-- Display -->
<TextBlock Grid.Row="0" Text="{Binding DisplayValue}" Style="{StaticResource DisplayStyle}" />
<!-- Buttons -->
<Grid Grid.Row="1" Margin="10">
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Button Content="C" Grid.Column="0" Grid.Row="0" Command="{Binding ClearCommand}" />
<Button Content="+/-" Grid.Column="1" Grid.Row="0" Command="{Binding SignChangeCommand}" />
<Button Content="%" Grid.Column="2" Grid.Row="0" Command="{Binding PercentageCommand}" />
<Button Content="/" Grid.Column="3" Grid.Row="0" Style="{StaticResource OperatorStyle}" Command="{Binding OperationCommand, ConverterParameter='/'}" />
<Button Content="7" Grid.Column="0" Grid.Row="1" Command="{Binding NumberCommand, ConverterParameter='7'}" />
<Button Content="8" Grid.Column="1" Grid.Row="1" Command="{Binding NumberCommand, ConverterParameter='8'}" />
<Button Content="9" Grid.Column="2" Grid.Row="1" Command="{Binding NumberCommand, ConverterParameter='9'}" />
<Button Content="*" Grid.Column="3" Grid.Row="1" Style="{StaticResource OperatorStyle}" Command="{Binding OperationCommand, ConverterParameter='*'}" />
<!-- ... more buttons ... -->
<Button Content="0" Grid.Column="0" Grid.Row="4" Grid.ColumnSpan="2" Style="{StaticResource ZeroButton}" Command="{Binding NumberCommand, ConverterParameter='0'}" />
<Button Content="." Grid.Column="2" Grid.Row="4" Command="{Binding DecimalCommand}" />
<Button Content="=" Grid.Column="3" Grid.Row="4" Style="{StaticResource EqualsStyle}" Command="{Binding EqualsCommand}" />
</Grid>
</Grid>
C# Logic (CalculatorWindow.xaml.cs
):
The code-behind handles button clicks, manages state, and performs calculations. Below is a simplified example of the number input logic:
public partial class CalculatorWindow : Window
{
private string _currentInput = "0";
private double _firstOperand = 0;
private double? _secondOperand = null;
private string _currentOperation = "";
private bool _isNewEntry = true;
public CalculatorWindow()
{
InitializeComponent();
DisplayValue.Text = _currentInput;
}
private void NumberButton_Click(object sender, RoutedEventArgs e)
{
string digit = (sender as Button).Content.ToString();
if (_isNewEntry)
{
_currentInput = digit;
_isNewEntry = false;
}
else
{
if (_currentInput == "0")
{
_currentInput = digit;
}
else
{
_currentInput += digit;
}
}
DisplayValue.Text = _currentInput;
}
private void OperationButton_Click(object sender, RoutedEventArgs e)
{
if (!string.IsNullOrEmpty(_currentOperation) && !_isNewEntry)
{
Calculate();
}
_firstOperand = double.Parse(_currentInput);
_currentOperation = (sender as Button).Content.ToString();
_isNewEntry = true;
}
private void EqualsButton_Click(object sender, RoutedEventArgs e)
{
Calculate();
_currentOperation = "";
_isNewEntry = true;
}
private void Calculate()
{
if (_secondOperand == null && !_isNewEntry)
{
_secondOperand = double.Parse(_currentInput);
}
double result = 0;
switch (_currentOperation)
{
case "+":
result = _firstOperand + _secondOperand.Value;
break;
case "-":
result = _firstOperand - _secondOperand.Value;
break;
case "*":
result = _firstOperand * _secondOperand.Value;
break;
case "/":
if (_secondOperand.Value != 0)
{
result = _firstOperand / _secondOperand.Value;
}
else
{
DisplayValue.Text = "Error";
return;
}
break;
default:
result = _secondOperand.Value; // For cases like just pressing equals after a number
break;
}
_currentInput = result.ToString();
DisplayValue.Text = _currentInput;
_firstOperand = result; // For chaining operations
_secondOperand = null;
_isNewEntry = true;
}
private void ClearButton_Click(object sender, RoutedEventArgs e)
{
_currentInput = "0";
_firstOperand = 0;
_secondOperand = null;
_currentOperation = "";
_isNewEntry = true;
DisplayValue.Text = _currentInput;
}
// ... other event handlers for ., +/-, % ...
}
Live Demo:
Experience a simplified, interactive version of the calculator below:
0