建立一個form的專案在
Form1.Designer.cs
把程式碼換上- namespace temp
- {
- partial class Form1
- {
- ///
- /// 設計工具所需的變數。
- ///
- private System.ComponentModel.IContainer components = null;
- ///
- /// 清除任何使用中的資源。
- ///
- /// 如果應該公開 Managed 資源則為 true,否則為 false。
- protected override void Dispose(bool disposing)
- {
- if (disposing && (components != null))
- {
- components.Dispose();
- }
- base.Dispose(disposing);
- }
- #region Windows Form 設計工具產生的程式碼
- ///
- /// 此為設計工具支援所需的方法 - 請勿使用程式碼編輯器修改這個方法的內容。
- ///
- ///
- private void InitializeComponent()
- {
- this.F = new System.Windows.Forms.TextBox();
- this.C = new System.Windows.Forms.TextBox();
- this.label1 = new System.Windows.Forms.Label();
- this.label2 = new System.Windows.Forms.Label();
- this.FtoC = new System.Windows.Forms.Button();
- this.CtoF = new System.Windows.Forms.Button();
- this.SuspendLayout();
- //
- // F
- //
- this.F.Location = new System.Drawing.Point(107, 23);
- this.F.Name = "F";
- this.F.Size = new System.Drawing.Size(100, 22);
- this.F.TabIndex = 0;
- //
- // C
- //
- this.C.Location = new System.Drawing.Point(107, 95);
- this.C.Name = "C";
- this.C.Size = new System.Drawing.Size(100, 22);
- this.C.TabIndex = 1;
- //
- // label1
- //
- this.label1.AutoSize = true;
- this.label1.Location = new System.Drawing.Point(13, 23);
- this.label1.Name = "label1";
- this.label1.Size = new System.Drawing.Size(41, 12);
- this.label1.TabIndex = 2;
- this.label1.Text = "F Temp";
- //
- // label2
- //
- this.label2.AutoSize = true;
- this.label2.Location = new System.Drawing.Point(15, 95);
- this.label2.Name = "label2";
- this.label2.Size = new System.Drawing.Size(43, 12);
- this.label2.TabIndex = 3;
- this.label2.Text = "C Temp";
- //
- // FtoC
- //
- this.FtoC.Location = new System.Drawing.Point(107, 62);
- this.FtoC.Name = "FtoC";
- this.FtoC.Size = new System.Drawing.Size(51, 23);
- this.FtoC.TabIndex = 4;
- this.FtoC.Text = "FtoC";
- this.FtoC.UseVisualStyleBackColor = true;
- this.FtoC.Click += new System.EventHandler(this.FtoC_Click);
- //
- // CtoF
- //
- this.CtoF.Location = new System.Drawing.Point(165, 62);
- this.CtoF.Name = "CtoF";
- this.CtoF.Size = new System.Drawing.Size(54, 23);
- this.CtoF.TabIndex = 5;
- this.CtoF.Text = "CtoF";
- this.CtoF.UseVisualStyleBackColor = true;
- this.CtoF.Click += new System.EventHandler(this.CtoF_Click);
- //
- // Form1
- //
- this.ClientSize = new System.Drawing.Size(292, 266);
- this.Controls.Add(this.CtoF);
- this.Controls.Add(this.FtoC);
- this.Controls.Add(this.label2);
- this.Controls.Add(this.label1);
- this.Controls.Add(this.C);
- this.Controls.Add(this.F);
- this.Name = "Form1";
- this.ResumeLayout(false);
- this.PerformLayout();
- }
- #endregion
- private System.Windows.Forms.TextBox F;
- private System.Windows.Forms.TextBox C;
- private System.Windows.Forms.Label label1;
- private System.Windows.Forms.Label label2;
- private System.Windows.Forms.Button FtoC;
- private System.Windows.Forms.Button CtoF;
- }
- }
複製代碼 在Form1.cs換上下列程式碼- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Text;
- using System.Windows.Forms;
- namespace temp
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- private void FtoC_Click(object sender, EventArgs e)
- {
- double Ftemp = Convert.ToDouble(F.Text);
- double Ctemp = (Ftemp - 32) * 5 / 9;
- C.Text = Ctemp.ToString();
- }
- private void CtoF_Click(object sender, EventArgs e)
- {
- double Ctemp = Convert.ToDouble(C.Text);
- double Ftemp = (Ctemp * 9 / 5) + 32;
- F.Text = Ftemp.ToString();
- }
- }
- }
複製代碼 ... |