按鈕控件的運(yùn)用
(1)為窗體Form1添加一個(gè)計(jì)數(shù)器nCounter,并添加3個(gè)按鈕控件,分別完成遞增計(jì)數(shù)器、遞減計(jì)數(shù)器、通過消息框提示計(jì)數(shù)器的值的功能,并添加1個(gè)Label控件來顯示每次運(yùn)算后的計(jì)數(shù)器值。完成的窗體界面如圖9-14所示。
(2)設(shè)置窗體和各控件的屬性,如表9-2所示。
(3)切換到代碼窗口,創(chuàng)建事件過程:
private int nCounter;
//第一次加載時(shí),進(jìn)行計(jì)數(shù)器和lblResult的初始化
private void Form1_Load(object sender, EventArgs e)
{
this.nCounter = 50;
this.ShowCounter();
}
//進(jìn)行遞增操作,并提示新值
private void btnInc_Click(object sender, EventArgs e)
{
this.nCounter++;
this.ShowCounter();
}
//進(jìn)行遞減操作,并提示新值
private void btnDes_Click(object sender, EventArgs e)
{
this.nCounter--;
this.ShowCounter();
}
//通過MessageBox提示當(dāng)前的值
private void btnMsg_Click(object sender, EventArgs e)
{
string strMsg = "當(dāng)前計(jì)數(shù)器=" + this.nCounter.ToString("D8");
MessageBox.Show(strMsg, "提示");
//顯示計(jì)數(shù)器值到Label控件lblResult
private void ShowCounter()
{
string strMsg = this.nCounter.ToString("D8");
this.lblResult.Text = strMsg;
}
點(diǎn)擊加載更多評(píng)論>>