C#windows Form Simple Calculator implementation (UP)

Source: Internet
Author: User

Write a blog for the first time to share the code of a simple calculator. As a quasi-programmer, it is necessary to write more code to be able to be clever. Focus on basic knowledge to fly faster, higher and more stable.

The code may be poorly written, imperfect, unsafe, and the more you want to find, the better.

C # writes the calculator with the window, for the novice is how to create the form and implement the button response event! So, first of all, to explore the next window is how to achieve it!

Step 1: Create a new project →c#windows form application → new solution

At this point you will find two names, one is the solution name, and the other is the project name. It's no different for small programs. But the big point of the program is best to distinguish the open. The solution is like a big company, the project name is like the company's department, different departments have different functions, and then organized, the company can operate. In this case, because the program is very small, it is set as a simple calculator.

After the construction, we can see the following interface

After you've finished building your project, you might want to go to the saved folder and see what you've saved.

  Bin folder : Used to save the project post-build assembly. The Bin\Debug folder has an. exe or. dll file that is the final generated application. ( Baidu also has the finer explanation, actually the author after reading is also confused, believed later will slowly in-depth understanding )

  obj folder : Used to save compilation results for each module. In. NET, compilation is done in sub-modules. When the compilation is complete, it is merged into one. DLL or. exe file to the bin directory. Each compilation takes an incremental compilation, that is, recompiling only the changed code. Obj saves the result of compiling each file to speed up the compilation of the code. Whether you are using incremental compilation, you can set it by: Project Properties--Configuration Properties--high-level, incremental compilation.

  proprtties folder : Used to store some content related to the program settings, such as assembly version information, application configuration, etc. generally do not need to manually write.

It's not afraid to open a file from a folder after you know it.

Back to the VS interface. We see a blank Form1.cs design, which allows the programmer to drag the control design interface directly.

Try to grab a control in the toolbox next to it, and then look at the code

Step 2: Try dragging a button

Open the code for Form1.Designer.cs and Form1 and see how it's written.

//The Form1.Designer.cs code is as followsnamespaceEasy Calculator {Partial classForm1 {/// <summary>          ///the required designer variables. /// </summary>          PrivateSystem.ComponentModel.IContainer components =NULL; /// <summary>          ///clean up all the resources that are in use. /// </summary>          /// <param name= "disposing" >true if the managed resource should be disposed, otherwise false. </param>          protected Override voidDispose (BOOLdisposing) {              if(Disposing && (Components! =NULL) ) {components.              Dispose (); }              Base.          Dispose (disposing); }           #regionCode generated by the Windows Forms Designer/// <summary>          ///The Designer supports the required method-do not modify///Use the Code Editor to modify the contents of this method. /// </summary>          Private voidInitializeComponent () { This. button1 =NewSystem.Windows.Forms.Button ();  This.              SuspendLayout (); //               //button1//                This. button1. Location =NewSystem.Drawing.Point ( -, About);  This. button1. Name ="button1";  This. button1. Size =NewSystem.Drawing.Size ( the, at);  This. button1. TabIndex =0;  This. button1. Text ="button1";  This. button1. Usevisualstylebackcolor =true; //               //Form1//                This. Autoscaledimensions =NewSystem.Drawing.SizeF (6F, 12F);  This. AutoScaleMode =System.Windows.Forms.AutoScaleMode.Font;  This. ClientSize =NewSystem.Drawing.Size (284,262);  This. Controls.Add ( This. button1);  This. Name ="Form1";  This. Text ="Form1";  This. ResumeLayout (false); }           #endregion            PrivateSystem.Windows.Forms.Button button1; }  } 
// The Form1.cs code is as follows Form1.cs   namespace Simple Calculator  {      publicpartialclass  form1:form      {          public Form1 ()          {              InitializeComponent ();          }      }  

You can see that class Form1 is decorated by partial, which is the distribution class. Take a look at the Form1.Designer.cs code, which is also a partial modification. The original Form1.Designer.cs code is also part of the Form1 class. (I think of them as written together, in short, what field function properties belong to the Form1 class)

(note)partial is not a keyword, so in other contexts, it can be used as an identity symbol in a program. However, it is used directly before the keyword Class,struct or interface, representing the partial type. Visio Studio uses this feature for standard Windows program modules. If you create an ASP. NET project, a Windows Forms project, or a Windows Presentation foudation (WPF) project from a standard module, the template creates two class files for each Web page, form, or WPF form. We design directly in the FORM1 design or modify the interface, the code will be automatically added to the Form1.Designer.cs inside. In general, you should not modify the code inside, because if you modify the page component, Visual Studio will recompile the generated code, and the modified code will be lost.

Step 3: Add a Click event for the button

Define a function Button_onclick () to be implemented when a button is clicked, then add a click event for the button

namespaceEasy Calculator { Public Partial classForm1:form { PublicForm1 () {InitializeComponent (); Button1. Click+=NewEventHandler (Button1_onclick);//2. Add the event response again        }                  Private voidButton1_onclick (ObjectSender,eventargs args)//1. Write a post-click Method First{Console.WriteLine ("The mouse is clicked."); }      }  }  

At this time press F5 compile to open the program, click the button, then close the program, in the background output can be seen

The instructions were successful.

With these windows knowledge, you can write a simple calculator below.

tip: Little white write a form try not to rely on the window design panel provided by. NET to drag the control directly, but as a learning convenience tool. I try to drag the control to change the layout, change the properties of the control, and then learn how to do it in the auto-generated code, and then I can use my own code to implement my own interface.

C#windows Form Simple Calculator implementation (UP)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.