很不錯的C#教學網站
目的 : 簡易型計算機
軟體 : Microsoft Visual C# 2008 Express Edition
專案 : Windows Form
Code :
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 TestCalculator
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
try
{
double number1 = double.Parse(textBox1.Text); // Parse 為強制轉型
double number2 = double.Parse(textBox2.Text);
string op = comboBox1.Text;
double result = 0;
switch (op)
{
// 在 C# 裡必須在 case 中加上 break
case "+": result = number1 + number2; break;
case "-": result = number1 - number2; break;
case "*": result = number1 * number2; break;
case "/": result = number1 / number2; break;
default: throw new Exception("op = " + op + " Error!");
}
textBox3.Text = result.ToString();
}
catch ( Exception exception ) // 例外處理
{
MessageBox.Show( exception.ToString());
}
}
}
}
執行結果 :
工具箱 :
Button --- Text , Name , TabIndex
ComboBox --- Text , Items , TabIndex
TextBox --- Text , Name , TabIndex
Label --- Text
留言列表