C# 窗體程序入門 之計算器


      之前一直在java的B/S,這次被要求做一個C/S,其中客戶端主要是界面和socket通信。因為沒有使用過C#和Visual Studio的控件編程,先來個HelloWorld.

 

我的環境是visual studio 2010,其他版本例如2008,2012 都可以

1.啟動vs2010,新建->項目,得到下圖:

2. 選擇visual c# ->windows->Windows 窗體應用程序

3.選擇項目路徑(不一定跟我的一樣),名稱盡量能讓自己和別人明白。

 

 

4.窗體Form1 就是我們即將要編輯的位置

 

 

5.目前只用到button,textbox 控件,所以選擇工具箱里的Button並拖到Form1編輯框里,TextBox也一樣。

 

 

6.按鈕布局如下圖,頂上是TextBox控件,下面是20個button控件,還有一個鏈接控件,計划實現加減乘除,平方,開方,log, ln的功能

 

 

 

 

 

7.如下圖,單擊其中一個button按鈕,在右下方的屬性里找到text並改名,其他的屬性暫時不要動,以后可以慢慢改。

8.修改后的Form1編輯框如下圖左邊所示。

 

下面就是代碼的編寫了:

9.雙擊Form1 窗體后,進入Form1.cs*界面(說白了是剛才窗體的代碼編輯框,也是Form1窗體功能實現的界面)

10.下圖是初始程序,我們要做的是往里面加代碼。

 

 

完整代碼:

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

 

namespace jisuangji

{    

     public partial  class Form1 : Form   

  {   

               double a = 0;     

            double b = 0;         

        bool c = false ;        

          string d;        

 

     public Form1()        

         {       

                InitializeComponent();            

          }

 

          private void Form1_Load(object sender, EventArgs e)     

             {

              }

      

          private void button9_Click(object sender, EventArgs e)     // button9_Click是對應我的1鍵,如果單純復制代碼,但是跟我的窗體布局不同,一樣會報錯

           {                                // 算法沒問題,只要單擊相應的鍵,進入填入相應的算法即可

           if ( c ==true )        

                 {           

                 textBox1 .Text ="";            

                  c=false ;                 

                     }                    

             textBox1.Text+="1";     

            }

 

          private void button10_Click(object sender, EventArgs e)    // button10_Click是對應我的2鍵

              {           

              if(c==true )            

               {             

                    textBox1.Text="";          

                      c=false ;               

                      }           

               textBox1.Text+="2";      

            }

 

         private void button11_Click(object sender, EventArgs e)        //對應我的3鍵

             {       

                if (c==true )       

                  {             

                    textBox1 .Text ="";           

                      c=false ;                 

                     }         

                textBox1 .Text +="3";          

               }

 

          private void button5_Click(object sender, EventArgs e)    //對應我的4鍵

             {       

                if(c==true )         

                {           

                    textBox1.Text="";         

                      c=false ;              

                      }           

              textBox1.Text+="4";     

              }

 

        private void button6_Click(object sender, EventArgs e)       //對應我的5鍵

            {        

                 if(c==true )      

                    {              

                   textBox1.Text="";          

                      c=false ;           

                        }                

              textBox1.Text+="5";      

             }

 

          private void button7_Click(object sender, EventArgs e)    //對應我的6鍵

             {       

                if(c==true )          

               {               

                textBox1.Text="";   

                        c=false ;               

                      }          

             textBox1.Text+="6";       

            }

 

          private void button1_Click(object sender, EventArgs e)  //對應我的7鍵

               {       

                if(c==true )        

                 {            

                   textBox1.Text="";        

                    c=false ;

                      }                   

                   textBox1.Text+="7";        

           }

 

        private void button2_Click(object sender, EventArgs e)   //對應我的8鍵

              {            

             if(c==true )        

                 {                

                textBox1.Text="";          

                     c=false ;           

                      }        

                  textBox1.Text+="8";    

              }

 

        private void button3_Click(object sender, EventArgs e)    //對應我的9鍵

              {      

                 if(c==true )      

                   {               

                  textBox1.Text="";          

                     c=false ;           

                   }          

                textBox1.Text+="9";    

               }

 

          private void button13_Click(object sender, EventArgs e)   //對應我0的鍵,增加了除數不能為0的判斷

                {     

                  if(c==true )              

                  {               

                   textBox1.Text="";     

                          c=false ;

                        }         

                textBox1.Text += "0";

                     if (d == "/")        

                       {           

                          textBox1.Clear();            

                           MessageBox.Show("除數不能為零", "錯誤提示", MessageBoxButtons.OK,   MessageBoxIcon.Warning);                              }          

               }

 

          private void button4_Click(object sender, EventArgs e)   //對應我的+鍵

                {                      

                c = true;         

                 b = double.Parse(textBox1.Text);       

                  d = "+";       

              }

 

          private void button8_Click(object sender, EventArgs e)   //對應我的-鍵

                {     

                     c = true;       

                   b = double.Parse(textBox1.Text);    

                     d = "-";       

              }

          private void button12_Click(object sender, EventArgs e)   //對應我的*鍵

                {              

                    c = true;       

                   b = double.Parse(textBox1.Text);  

                       d = "*";    

                 }

 

          private void button16_Click(object sender, EventArgs e)    //對應我的/鍵

               {        

                    c = true;      

                     b = double.Parse(textBox1.Text);     

                      d = "/";       

              }

 

          private void button17_Click(object sender, EventArgs e)    //對應我的平方鍵

               {        

                   c = true;        

                   b = double.Parse(textBox1.Text);     

                     d = "x2";  

                 }

 

          private void button18_Click(object sender, EventArgs e)    //對應我的開方鍵

               {          

                 c = true;      

                    b = double.Parse(textBox1.Text);      

                    d = "sqrt";        

            }        

     private void button19_Click(object sender, EventArgs e)   //對應我的log鍵

                {     

                      c = true;   

                        b = double.Parse(textBox1.Text);      

                       d = "log";      

               }

         private void button20_Click(object sender, EventArgs e)      //對應我的ln鍵

             {          

                   c = true;   

                        b = double.Parse(textBox1.Text);    

                        d = "ln";    

               }

      

        private void button15_Click(object sender, EventArgs e)  //對應我的=鍵

                 {           

                  switch (d)         

                       {

                               case "+": a = b + double.Parse(textBox1.Text); break;     

                             case "-": a = b - double.Parse(textBox1.Text); break;    

                               case "*": a = b * double.Parse(textBox1.Text); break;        

                            case "/": a = b / double.Parse(textBox1.Text); break;           

                          case "x2": a = b * double.Parse(textBox1.Text); break;      

                              case "sqrt": a = Math.Sqrt(b ); break;               

                        case "log": a = Math.Log(double.Parse(textBox1.Text),b ); break;      

                              case "ln": a = Math.Log(b, Math.E); break;

                              }                    

              textBox1.Text = a + "";               

                  c = true;   

                   }

 

        private void button14_Click(object sender, EventArgs e)    //對應我的c鍵,實現了清零的功能

               {        

                   textBox1.Text = "";  

                 }

 

          /*     private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)    

               {          

               System.Diagnostics.Process.Start("http://home.cnblogs.com/u/linshuangxi/");       

              }

       */                      

            //我注釋掉是因為這是一個連接到我的博客的地址,首先需要在窗體拖進來一個LinkLabel控件。

                                 //  然后把注釋去掉就可用了

  }    

}

 

11.點擊運行界面,就會出來窗體,偷個懶,我把基本功能效果圖和添加屬性后的圖都放在一起了,如下圖:

后言:

   1.盡量不要復制代碼,自己動手,豐衣足食

   2.第一次設計的話,防止出錯,保持窗體布局跟我的一樣,這樣復制代碼就不會有問題

   3. 剛剛講到按鈕的屬性設計,先把基本功能實現,然后可以在屬性里設計文字大小,顏色,背景等

屬性設計后的效果圖:

 

       參考:http://www.th7.cn/Program/net/201311/158591.shtml


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM