WPF calculator
A C# WPF calculator with keyboard shortcuts, stored value handling, and a clean desktop interaction model.
- Language
- C#
Why I built a calculator project
This started as a university assignment, but I treated it as a chance to get serious with desktop UI logic in C#.
At first glance, calculator projects look basic. In practice, they are good training for event-driven programming because every button, keypress, and state transition has to behave predictably.
I built this with WPF and focused on input handling, operation state management, and keyboard support.
What is implemented
The app supports standard arithmetic operations:
- Addition
- Subtraction
- Multiplication
- Division
It also supports:
- Number buttons (
0-9) - Keyboard operation shortcuts (
+,-,*,/,Enter,Escape,Backspace,Delete) - Stored value area that can be cleared with a double click
- UI state changes that show or hide result and stored sections
In the code, this is managed with methods like:
startCondition()for reset and initial setupoperationMaker(char operate)to capture selected operationbtEnter_Click(...)for evaluationtbInput_KeyDown(...)for keyboard-driven controls
How I structured the logic
I used a single operation char as state to decide which calculation to run when the user presses Enter.
Flow:
- User enters first number.
- User selects operation (
+,-,*,/). - First number is stored in
tbStored. - User enters second number.
- Enter triggers calculation in switch-case and writes result.
For a small app, this pattern is enough and keeps behavior readable.
I also added helper methods to control UI visibility:
Stored(bool action)Result(bool action)
This prevented repeated UI code in each event handler.
Input handling details that mattered
A calculator becomes frustrating quickly if input behavior is inconsistent, so I paid attention to controls:
- Numeric text filtering with regex (
PreviewTextInput) - Backspace handling for one-character removal
- Delete to clear current input
- Escape to reset application state
- Enter to evaluate expression
Those small details are what made the app feel usable instead of only "technically done."
Bugs and edge cases I had to think about
1. Re-clicking operation buttons
Without guard logic, clicking an operation twice could overwrite state in unexpected ways. I handled this in operationMaker to avoid losing stored values accidentally.
2. Empty input states
Backspace on empty input can throw errors if unchecked. I added conditions and user feedback to avoid that crash path.
3. State reset consistency
Reset had to clear fields, hide result sections, and set focus correctly. The startCondition() method centralized this behavior.
What I learned from this project
This project improved my confidence with:
- WPF event-driven architecture
- Separating UI state helpers from operation logic
- Mapping keyboard events to button behavior
- Debugging user interaction edge cases instead of only math logic
It also taught me that desktop UX quality depends on "small" things users notice immediately: focus, keyboard behavior, and predictable reset behavior.
What I would improve now
If I revisited this calculator today, I would:
- Use MVVM instead of code-behind-heavy architecture
- Add robust validation for divide-by-zero and malformed input
- Improve operation chaining (for longer expressions)
- Add test coverage for logic extraction layer
- Polish visual design and accessibility contrast
I would also separate pure calculation logic from UI code so behavior can be tested without rendering the window.
Why this remains relevant in my portfolio
Even though it is a classic assignment type, this project demonstrates fundamentals that transfer to larger systems:
- Input validation
- Stateful interaction design
- Defensive event handling
- Predictable UI behavior under edge cases
If you can build a desktop calculator cleanly, you are practicing the same discipline needed for forms, dashboards, and internal business tools.
SEO relevance
For anyone looking for a WPF calculator project in C# with keyboard support and practical event handling, this project is a useful reference.
It is especially relevant for students learning desktop development and developers moving from console apps to UI-driven C# applications.
Repository
You can browse the full source code here: