一個(gè)簡(jiǎn)單的計(jì)算器的例子,在這個(gè)小程序中我們需要用到的組件有:
Button:點(diǎn)擊計(jì)算
TextBox:輸出要運(yùn)算的數(shù)
RadioButton:選擇運(yùn)算類型
GroupBox:綁定RadioButton
首先我們?cè)诮缑嫔贤弦陨系目丶?,得到如下界面?

這時(shí)候監(jiān)聽(tīng)計(jì)算按鈕的點(diǎn)擊事件:
private void button1_Click(object sender, EventArgs e) { double op1, op2, result; if (textBox1.Text == ""||textBox2.Text=="" ) {//判斷是否兩個(gè)框框都輸入了數(shù)據(jù) MessageBox.Show(this,"輸入錯(cuò)誤","msg",MessageBoxButtons.OK, MessageBoxIcon.Information);//有空余項(xiàng)沒(méi)輸入數(shù)據(jù)彈出提示框 return; } op1 = double.Parse(textBox1.Text);//得到兩個(gè)框框的值并轉(zhuǎn)化為long類型 op2 = double.Parse(textBox2.Text); if (radioButton1.Checked) {//加法 result = op1 + op2; } else if (radioButton2.Checked){//減法 result = op1 - op2; } else if (radioButton3.Checked){//乘法 result = op1 * op2; } else {//除法 result = op1 / op2; } textBox3.Text = result.ToString();//設(shè)置textBox3的值 }我們看一下測(cè)試的結(jié)果:
加法:
乘法:
好了,上面基本就是一個(gè)簡(jiǎn)單的計(jì)算器的例子了!
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。
|
新聞熱點(diǎn)
疑難解答
圖片精選