實(shí)驗(yàn)內(nèi)容:Windows 窗體程序
一、實(shí)驗(yàn)?zāi)康募耙?/h2>
- (1) 掌握控件 Label、Button、TextBox 的常用屬性、方法、事件的基本應(yīng)用;
- (2) 掌握控件 RadioButton、CheckBox 的常用屬性、方法、事件的基本應(yīng)用通過簡(jiǎn)單程 序;
- (3) 掌握框-架類控件 GroupBox、Panel、TabControl 的基本應(yīng)用;
- (4) 掌握控件 ListBox、CombBox 、ListView 的常用屬性、方法、事件的基本應(yīng)用;
- (5) 掌握 Windows 應(yīng)用程序的結(jié)構(gòu);
- (6)掌握 WinForm 的設(shè)計(jì)
- (7)進(jìn)一步學(xué)習(xí)掌握查找與修改編譯錯(cuò)誤的方法;
二、實(shí)驗(yàn)環(huán)境
Microsoft Visual Studio 2008
三、實(shí)驗(yàn)內(nèi)容與步驟
一、設(shè)計(jì)簡(jiǎn)單的計(jì)算器
3.1、實(shí)驗(yàn)內(nèi)容
- 設(shè)計(jì)一個(gè)簡(jiǎn)單的計(jì)算器,使其可以進(jìn)行加、減、乘、除計(jì)算,程序運(yùn)行狀態(tài)如圖所 示。(教材第 4 章 113 頁(yè) 3.1 題)
如下所示
3.2、實(shí)驗(yàn)步驟
1、實(shí)驗(yàn)界面的屬性設(shè)置如下:
- (1)三個(gè)Lable的Text屬性:第一個(gè)數(shù)、第二個(gè)數(shù)、運(yùn)算結(jié)果。
- (2)四個(gè)RadButton的Text屬性和Name屬性:+和radAdd、-和radSub、×和radMul、÷和radDiv。
- (3)一個(gè)Button的Name和Text屬性:btnOk和運(yùn)算。
2、實(shí)驗(yàn)程序的設(shè)計(jì)代碼如下:
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 實(shí)驗(yàn)二_1_
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnOk_Click(object sender, EventArgs e)
{
double a, b;
a = Convert.ToDouble(txtDigit1.Text);
b = Convert.ToDouble(txtDigit2.Text);
if (radAdd.Checked)
txtShow.Text =Convert.ToString(a + b);
if (radSub.Checked)
txtShow.Text = Convert.ToString(a - b);
if (radMul.Checked)
txtShow.Text = Convert.ToString(a * b);
if (radDiv.Checked)
{
if (b==0)
MessageBox.Show("除數(shù)不能為零!");
else
txtShow.Text = Convert.ToString(a / b);
}
}
}
}
- 3、實(shí)驗(yàn)的運(yùn)行結(jié)果如下
-
3.1、簡(jiǎn)單的計(jì)算器的除
-
3.2、簡(jiǎn)單的計(jì)算器的乘
-
3.3、簡(jiǎn)單的計(jì)算器的減
- 3.4、簡(jiǎn)單的計(jì)算器的加
4、實(shí)驗(yàn)實(shí)現(xiàn)了加減乘除的基本運(yùn)算,并且可以判斷除數(shù)不能為零的情況。
二、設(shè)計(jì)選購(gòu)計(jì)算器配置的應(yīng)用程序
3.1、實(shí)驗(yàn)內(nèi)容
- 設(shè)計(jì)一個(gè)選購(gòu)計(jì)算器配置的應(yīng)用程序,如下圖所示。當(dāng)用戶選定了基本配置并且 單擊“確定”按鈕后,在右邊的列表框中顯示所選擇的信息。(教材第 4 章 113 頁(yè) 3.2 題)
如下所示
3.2、實(shí)驗(yàn)步驟
- 1、實(shí)驗(yàn)的界面設(shè)計(jì)如下:
容器 | Name屬性 | Item屬性 | DropDownHeight屬性 | Text屬性 |
---|---|---|---|---|
comboBox | cboCollection | 聯(lián)想 方正 Dell 兼容機(jī) | Simple | |
groupBox1 | CPU | |||
groupBox2 | 內(nèi)存 | |||
groupBox3 | 其他設(shè)備 | |||
基本控件 | Name屬性 | Text屬性 | ||
RadioButton1 | radPentiumD | Pentium D | ||
RadioButton2 | radPentiumM | Pentium M | ||
RadioButton3 | radXeon | Xeon | ||
RadioButton4 | rad256MB | 256MB | ||
RadioButton5 | Rad512MB | 512MB | ||
CheckBox1 | chkPrinter | 打印機(jī) | ||
CheckBox2 | chkMode | Mode | ||
CheckBox3 | chkNetConfiguration | 網(wǎng)絡(luò)適配器 | ||
ListBox | lstShow | |||
Button | btnOk | 確定 |
- 2、實(shí)驗(yàn)的程序設(shè)計(jì)代碼如下:
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 實(shí)驗(yàn)二_2_
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnOk_Click(object sender, EventArgs e)
{
lstShow.Items.Clear();
string a = cboCollection.SelectedItem.ToString();
lstShow.Items.Add(a);
if (radPentiumD.Checked)
lstShow.Items.Add(radPentiumD.Text);
if (radPentiumM.Checked)
lstShow.Items.Add(radPentiumM.Text);
if (radXeon.Checked)
lstShow.Items.Add(radXeon.Text);
if (rad256MB.Checked)
lstShow.Items.Add(rad256MB.Text);
if (rad512MB.Checked)
lstShow.Items.Add(rad512MB.Text);
if (chkPrinter.Checked)
lstShow.Items.Add(chkPrinter.Text);
if (chkMode.Checked)
lstShow.Items.Add(chkMode.Text);
if (chkNetConfiguration.Checked)
lstShow.Items.Add(chkNetConfiguration.Text);
}
}
}
- 3、實(shí)驗(yàn)的運(yùn)行效果如下:
-
3.1
-
3.2
-
3.3
- 4、lstShow.Items.Clear();語(yǔ)句實(shí)現(xiàn)了每一次操作,將原有的ListBox的Item集合清除的效果。
四、實(shí)驗(yàn)總結(jié)
- 1、通過C#的Windows掌握了Button、TextBox、RadioButton、CheckBox GroupBox等基本控件和容器的基本屬性。
- 2、掌握了ListBox的Item的Add方法添加項(xiàng)目等基本用法。
- 3、掌握了Convert的類進(jìn)行數(shù)據(jù)的轉(zhuǎn)換方法的應(yīng)用。
- 4、實(shí)驗(yàn)過程中出現(xiàn)了
if (b=0)
MessageBox.Show("除數(shù)不能為零!");
基本錯(cuò)誤,編譯不成功,才知道應(yīng)該為文章來源:http://www.zghlxwxcb.cn/news/detail-442181.html
b==0(b=0是賦值語(yǔ)句,而b==0是判斷b是否等于零)。
文章來源地址http://www.zghlxwxcb.cn/news/detail-442181.html
到了這里,關(guān)于C#面向?qū)ο蟪绦蛟O(shè)計(jì)課程實(shí)驗(yàn)二: 實(shí)驗(yàn)名稱:Windows 窗體程序的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!