Button b9 ;. Button bDiv ;. Button b4 ;. Button b5 ;. Button b6 ;. Button bMul ;. Button b1 ;. Button b2 ;. Button b3 ;. Button bSub ;. Button b0 ;. Button bEq ;. Button button15 ;. Button bAdd ;. Button bCE ;. Button bC ;. Like this: Like Loading You may also like. Loading Comments Email Required Name Required Website. Text ; lbResult. ToString ; break ; case "-" : txtResult. Point , byte ; this. Point 69 , ; this. Size 73 , 66 ; this. Point 69 , 72 ; this. Point 66 , 56 ; this.
Size 0 , 18 ; this. Size 73 , ; this. SizeF 6F , 13F ; this. Font ; this. Button b7 ; private System. TextBox txtResult ; private System. Label lbResult ; private System. Button b8 ; private System. Button b9 ; private System. Button bDiv ; private System. Button b4 ; private System. Button b5 ; private System. Button b6 ; private System. Button bMul ; private System. Button b1 ; private System. Button b2 ; private System. Button b3 ; private System. Step 6 Double click the button n1 to go to its event.
It tells you that it get executed when the button whose name n1 is clicked by the user. Write down the code to have it like this:. C Calculator Windows Forms. Next Recommended Reading. Net Core 6. Create A. Understanding Thread Starvation in. NET Core Applications. Exploring Numeric Streams In Java. What Is The Metaverse. Select a button. Its properties will be displayed in the properties menu off to the side.
In the Appearance section, you can edit an item's appearance. Use the Text field to change the text on a button. Have some fun picking different BackColors and types of font.
Scroll down to the Design section in the Properties menu and edit the name field for each button. This isn't required, but it will make coding much easier. Name each button something you can easily keep track of. Eg: Name the 1 button "one," that way you can keep track of it in the code later. Click Save All and move on to step 3! Double click a button. This will bring you to the "Form1.
This is where all of your code will be written. Notice a section has been pre-written for you based off of the "Click" event. This section of code will be executed when that specific button is clicked.
For the sake of this project, we will call these Event Handlers. Double click all of your buttons so you have an event handler for each button use the tabs to switch back and forth. Click Save All and move on to step 4. This project requires three different types of variables, char, double, and String.
The value 3. String - A string is not a data type, it is an Object that handles chains of characters. We call these chains strings. Ex: 'c' is a character, "ccc" is a character string.
Declare 5 new variables above the Form1 method as shown in the image above. This will create the needed variables to store and process the input from the user. I do not initialize the operation variable because a value will be placed into it later depending on which operation the user chooses.
I do initialize the strings to be empty using string. Empty however just to be sure that the string will be completely empty before it is used. Click the Save All button and move on to the next step when you are ready. Thinks are looking good. The shell is set up and ready to go. But in order for a user to interact with the program, we need to store the data given from the user clicking the buttons.
This will add a zero to the input character string. One click will have the string contain one zero like so: "0". Clicking the zero button again will add another making the string value "00", and so on. Repeat step 5. Change the "0" value to match which button handler you are in. Don't want to add a 0 to the string when a 1 is pressed. Empty; This code will store the contents of the input string into the operand1 string, and set the operation character accordingly.
The input string gets cleared at the end so it can be ready to record the next value from the user. Click the Save All button and move on to step 6. Users would probably like to see what they are inputting into the calculator. This step adds that feature. Add the line this.
This will add the input to the TextBox that was dragged to the screen earlier. Repeat this step for every numerical input handler. Next, add the line this. This is essential because it clears the TextBox before the input string is added to it. Forgetting this step will leave the user with a mess on the display feel free to try it out by running the code without this step. Run the program and see what happens! Click Save All and move on to step 7. From the last step, you have probably noticed that we can't clear the input off the screen.
This step will fix that issue! Add the following lines of code in the clear button event handler. Empty; this. Empty; Code Explained: We have seen all of these lines before, but here is a quick refresher. The first line clears the TextBox, and the three lines after empty our input, operand1, and operand2 strings. This will effectively clear everything.
Run the program and see what happens. It should function similarly to the video. Now time to do what a calculator does best, calculate output! Insert the following code into the equals button event handler.
Try not to just paste it, try and understand what is happening. TryParse operand1, out num1 ; double. Next we create some more doubles num1 and num2. These will store the numerical values that have been stored in operand1 and operand2.
The next lines double.
0コメント