---
tags: 視窗程式設計
---
# 課堂作業 3-2:請將長度單位轉換程式改寫成函式版本
請同學依照上一個章節「[單位轉換程式:建立函式(方法)](/nWkMNY-eTRi_lhQty_pUiQ)」將新版的長度單位轉換程式修改完成。
再將新版的程式,Commit後,上傳到 GitHub。
## 課堂作業 3-2:參考答案
:::spoiler 點開可以看到參考答案,不過建議你先試試看,再看參考答案。
```csharp=
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace LengthCalculator
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
// 全域變數
string strInput; // 字串型態的strInput變數
double douOutput; // double浮點數型態的douOutput變數
private void txtCM_KeyUp(object sender, EventArgs e)
{
strInput = txtCM.Text; // 將txtCM文字框的值放入strInput變數
// 判斷式,如果能夠以double.TryParse成功轉型,那才做數值的計算
if (double.TryParse(strInput, out douOutput) == true)
{
caculateAnswer(0, douOutput);
}
else
{
// 如果無法轉型,則是在說明文字中顯示錯誤訊息,並且將txtCM文字框清除
txtInfo.Text = "請輸入數字";
txtCM.Text = "";
}
}
private void txtM_KeyUp(object sender, EventArgs e)
{
strInput = txtM.Text; // 將txtCM文字框的值放入strInput變數
// 判斷式,如果能夠以double.TryParse成功轉型,那才做數值的計算
if (double.TryParse(strInput, out douOutput) == true)
{
caculateAnswer(1, douOutput * 100);
}
else
{
// 如果無法轉型,則是在說明文字中顯示錯誤訊息,並且將txtCM文字框清除
txtInfo.Text = "請輸入數字";
txtM.Text = "";
}
}
private void txtKM_KeyUp(object sender, EventArgs e)
{
strInput = txtKM.Text; // txtKM
// 判斷式,如果能夠以double.TryParse成功轉型,那才做數值的計算
if (double.TryParse(strInput, out douOutput) == true)
{
caculateAnswer(2, douOutput * 10000);
}
else
{
// 如果無法轉型,則是在說明文字中顯示錯誤訊息,並且將txtCM文字框清除
txtInfo.Text = "請輸入數字";
txtKM.Text = "";
}
}
private void txtIn_KeyUp(object sender, EventArgs e)
{
strInput = txtIn.Text; // txtIn
// 判斷式,如果能夠以double.TryParse成功轉型,那才做數值的計算
if (double.TryParse(strInput, out douOutput) == true)
{
caculateAnswer(3, douOutput * 2.54);
}
else
{
// 如果無法轉型,則是在說明文字中顯示錯誤訊息,並且將txtCM文字框清除
txtInfo.Text = "請輸入數字";
txtIn.Text = "";
}
}
private void txtFt_KeyUp(object sender, EventArgs e)
{
strInput = txtFt.Text; // txtFt
// 判斷式,如果能夠以double.TryParse成功轉型,那才做數值的計算
if (double.TryParse(strInput, out douOutput) == true)
{
caculateAnswer(4, douOutput * 30.48);
}
else
{
// 如果無法轉型,則是在說明文字中顯示錯誤訊息,並且將txtCM文字框清除
txtInfo.Text = "請輸入數字";
txtFt.Text = "";
}
}
private void txtYard_KeyUp(object sender, EventArgs e)
{
strInput = txtYard.Text; // txtFt
// 判斷式,如果能夠以double.TryParse成功轉型,那才做數值的計算
if (double.TryParse(strInput, out douOutput) == true)
{
caculateAnswer(5, douOutput * 91.44);
}
else
{
// 如果無法轉型,則是在說明文字中顯示錯誤訊息,並且將txtCM文字框清除
txtInfo.Text = "請輸入數字";
txtYard.Text = "";
}
}
private void btnAllClear_Click(object sender, EventArgs e)
{
txtCM.Text = "";
txtM.Text = "";
txtKM.Text = "";
txtIn.Text = "";
txtFt.Text = "";
txtYard.Text = "";
txtInfo.Text = "";
}
// 設計一個單位轉換計算的函式,沒有回傳值,設計兩個參數,1.類別參數、2.數值參數
private void caculateAnswer(int _kind, double _value)
{
if (_kind != 0)
txtCM.Text = string.Format("{0:0.##########}", _value);
if (_kind != 1)
txtM.Text = string.Format("{0:0.##########}", _value / 100);
if (_kind != 2)
txtKM.Text = string.Format("{0:0.##########}", _value / 100000);
if (_kind != 3)
txtIn.Text = string.Format("{0:0.##########}", _value / 2.54);
if (_kind != 4)
txtFt.Text = string.Format("{0:0.##########}", _value / 30.48);
if (_kind != 5)
txtYard.Text = string.Format("{0:0.##########}", _value / 91.44);
}
}
}
```
另一種設計的方法
```csharp=
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace LengthCalculator
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
txtCM.KeyUp += textBox_KeyUp;
txtM.KeyUp += textBox_KeyUp;
txtKM.KeyUp += textBox_KeyUp;
txtIn.KeyUp += textBox_KeyUp;
txtFt.KeyUp += textBox_KeyUp;
txtYard.KeyUp += textBox_KeyUp;
}
// 全域變數
string strInput; // 字串型態的strInput變數
double douOutput; // double浮點數型態的douOutput變數
private void textBox_KeyUp(object sender, EventArgs e)
{
TextBox textBox = sender as TextBox;
strInput = textBox.Text; // 將txtCM文字框的值放入strInput變數
// 判斷式,如果能夠以double.TryParse成功轉型,那才做數值的計算
if (double.TryParse(strInput, out douOutput) == true)
{
if (textBox == txtCM)
caculateAnswer(0, douOutput);
if (textBox == txtM)
caculateAnswer(1, douOutput * 100);
if (textBox == txtKM)
caculateAnswer(2, douOutput * 10000);
if (textBox == txtIn)
caculateAnswer(3, douOutput * 2.54);
if (textBox == txtFt)
caculateAnswer(4, douOutput * 30.48);
if (textBox == txtYard)
caculateAnswer(5, douOutput * 91.44);
}
else
{
// 如果無法轉型,則是在說明文字中顯示錯誤訊息,並且將txtCM文字框清除
txtInfo.Text = "請輸入數字";
textBox.Text = "";
}
}
private void btnAllClear_Click(object sender, EventArgs e)
{
txtCM.Text = "";
txtM.Text = "";
txtKM.Text = "";
txtIn.Text = "";
txtFt.Text = "";
txtYard.Text = "";
txtInfo.Text = "";
}
// 設計一個單位轉換計算的函式,沒有回傳值,設計兩個參數,1.類別參數、2.數值參數
private void caculateAnswer(int _kind, double _value)
{
if (_kind != 0)
txtCM.Text = string.Format("{0:0.##########}", _value);
if (_kind != 1)
txtM.Text = string.Format("{0:0.##########}", _value / 100);
if (_kind != 2)
txtKM.Text = string.Format("{0:0.##########}", _value / 100000);
if (_kind != 3)
txtIn.Text = string.Format("{0:0.##########}", _value / 2.54);
if (_kind != 4)
txtFt.Text = string.Format("{0:0.##########}", _value / 30.48);
if (_kind != 5)
txtYard.Text = string.Format("{0:0.##########}", _value / 91.44);
}
}
}
```