Reputation: 7025
I read on this answer that Windows Calc do not use a Label not a Text Box to show the inputed numbers. As I'm new on programming I would like to know how can I use a Label to work like Windows Calc's Label on the calc I'm developing.
Upvotes: 0
Views: 290
Reputation: 17327
Put the Label
control on the form and then put buttons for the numbers and, the operators. When a number is pressed, simply update the Label
's Text
property by appending the number at the end. (The initial value of the label should be 0
.) When an operator is pressed, perform the operation on the value (take the label's Text
property and convert it to a number using the Convert
class). If the operation needs two numbers, you may have to store the first number and clear the label for a new number entry. Once you have two input numbers, just perform the operation on them and show the result.
If you also want to display the operation (similar to how Calculator does it), put another label on top of the first one and just use its Text
property to show/clear the operation as needed.
If you want to be able to press number keys on the keyboard, add keyboard event handlers (KeyPress
event) to the form that hosts the label and then update the label as needed when a numeric or operator key is pressed. Make sure to set the KeyPreview
property to true
so that your form will receive all keyboard input first - this way you can direct number keys to your label.
Upvotes: 2