c# Windows窗體應用程序設計綜合實例 ----數學測驗器

c# Windows窗體應用程序設計綜合實例 ----數學測驗器

在前面已經介紹了基本的幾個控件的應用,並且也更新了一個小遊戲的設計。步驟比較多,只要理清楚每個步驟和每塊代碼添加的作用,就可以順利地完成。有什麼問題歡迎大家討論私信。

-------------------------------------------------------
那麼開始今天的正題

新建項目

1.打開vs2017,新建一個窗體項目,命名爲“數學測驗器”。將窗體size屬性更改爲 500 , 400 。(大小可以根據自己的需求調整)
2.將“FormBorderStyle”屬性的值更改爲“Fixed3D”,並“MaximizeBox”屬性設置爲“False”。

創建“剩餘時間框”

1.從“工具箱” 中添加一個 Label 控件,然後將其“(Name)” 屬性的值設置爲“timeLabel” 。
2.將“AutoSize”屬性更改爲“False”。將“BorderStyle”屬性更改爲“FixedSingle”以在框的周圍繪製線條。將“Size”屬性設置爲“200, 30”。
3.在“屬性” 窗口中,選擇“Text” 屬性,然後選擇 Backspace 鍵以清除此屬性的值。
選擇“Font” 屬性旁邊的加號 (+ ),然後將“Size” 屬性的值更改爲“15.75” 。
4. 從“工具箱” 中再添加一個Label控件,然後將其字號設置爲“15.75” 。將“Text”屬性設置爲“剩餘時間”。(後面提到的字號都是在Font屬性的+裏面修改)

添加加法的控件

1.從“工具箱” 中添加一個“Label”控件,然後將其“Text” 屬性設置爲“?” (問號)。
將“AutoSize”屬性設置爲“False”。
將“Size”屬性設置爲“60, 50”。
將字號設置爲“18”。
將“TextAlign”屬性設置爲“MiddleCenter”。
將“Location”屬性設置爲“50, 75”。
將“(Name)”屬性設置爲“plusLeftLabel”。
將該Label控件複製粘貼三次,更改如下屬性:將第二個 Label的“Text”屬性值設置爲 + (加號);第三個Label的“(Name)”屬性值設置爲“plusRightLabel”; 第四個 標籤的“Text”屬性值設置爲 = (等於號)。
從“工具箱” 添加一個 NumericUpDown 控件,將其字號設置爲“18” ,並將其寬度設置爲“100” ;將此“NumericUpDown”控件與加法題的各 Label 控件排成一行;“NumericUpDown”控件的“(Name)” 屬性值更改爲“sum” 。
效果圖如下:
效果圖

添加其他運算的控件

複製加法題的全部五個控件(四個 Label 控件和一個 NumericUpDown 控件),然後粘貼這些控件。分別更改以下屬性:

減法

將第二個Label的“Text”屬性值更改爲“-”
將第一個問號Label命名爲“minusLeftLabel”
將第二個問號Label命名爲“minusRightLabel”
將“NumericUpDown”控件命名爲“difference”

乘法

第一個Label命名爲“timesLeftLabel”
第二個Label的“Text”屬性更改爲“×”(乘號)
第三個Label命名爲“timesRightLabel”,然後將 NumericUpDown 控件命名爲“product”

除法

第一個Label命名爲“dividedLeftLabel”
第二個Label的“Text”屬性更改爲“÷”(除號)
第三個Label命名爲“dividedRightLabel”
NumericUpDown 控件命名爲“quotient”

設置 Tab 鍵索引順序

從“工具箱” 添加一個 Button 控件,然後將其“(Name)” 屬性設置爲“startButton” 。
“Text”屬性設置爲“開始測驗”。將字號設置爲“14”。
將“AutoSize”屬性設置爲“True”,這可使此按鈕自動調整大小以適合文本。
“startButton”控件的“TabIndex”屬性值設置爲“1”。
“TabIndex” 屬性用於設置測驗對象選擇 Tab 鍵時的控件順序。
NumericUpDown sum 控件的“TabIndex”屬性值設置爲“2”,將 difference 控件的此屬性值設置爲“3”,將 product 控件的此屬性值設置爲“4”,將 quotient 控件的此屬性值設置爲“5”。
效果圖如下:
效果圖

添加代碼

添加隨機加法

後面的代碼最好都手動輸入。

public partial class Form1 : Form
{
int addend1;
int addend2;

Random randomizer = new Random();
}

添加一個名爲 StartTheQuiz() 的方法,此方法使用 Random 對象的 Next() 方法在Label中顯示隨機數。 StartTheQuiz() 
public void StartTheQuiz()
{
    
    addend1 = randomizer.Next(51);
    addend2 = randomizer.Next(51);

   
    plusLeftLabel.Text = addend1.ToString();
    plusRightLabel.Text = addend2.ToString();

    
    sum.Value = 0;
}


現該行爲的 Click 事件處理程序。
添加下面兩個語句。

private void startButton_Click(object sender, EventArgs e)
{
    StartTheQuiz();
    startButton.Enabled = false;           
}

運行後,當第一行出現隨機加法,即設置成功。

添加倒計時計時器

在剛纔的Form代碼添加 int timeLeft;,然後添加一個Timer控件。修改“Interval”屬性爲“1000”。
雙擊Timer控件,添加如下代碼:

private void timer1_Tick(object sender, EventArgs e)
{
    if (timeLeft > 0)
    {
        
        timeLeft = timeLeft - 1;
        timeLabel.Text = timeLeft + " seconds";
    }
    else
    {
        
        timer1.Stop();
        timeLabel.Text = "Time's up!";
        MessageBox.Show("You didn't finish in time.", "Sorry!");
        sum.Value = addend1 + addend2;
        startButton.Enabled = true;
    }
}

在剛纔的StartTheQuiz()方法中添加如下代碼

timeLeft = 30;
    timeLabel.Text = "30 seconds"; 
    timer1.Start();

運行後,計時框開始計時。

添加 CheckTheAnswer() 方法

爲了驗證用戶輸入的結果與正確的結果是否相等,添加以下代碼。

private bool CheckTheAnswer()
{
    if (addend1 + addend2 == sum.Value)
        return true;
    else
        return false;
}

將以下代碼添加到 Timer1_Tick() 方法中的 if else 語句,使計時器在用戶獲得正確答案時停止。

private void timer1_Tick(object sender, EventArgs e)
{
    if (CheckTheAnswer())
    {
        
        timer1.Stop();
        MessageBox.Show("You got all the answers right!",
                        "Congratulations!");
        startButton.Enabled = true;
    }
    else if (timeLeft > 0)
    {
       
       timeLeft--;
        timeLabel.Text = timeLeft + " seconds";
    }
    else
    {
        
        timer1.Stop();
        timeLabel.Text = "Time's up!";
        MessageBox.Show("You didn't finish in time.", "Sorry!");
        sum.Value = addend1 + addend2;
        startButton.Enabled = true;
    }
}

爲 NumericUpDown 控件添加 Enter 事件處理程序

選擇窗體上的第一個“NumericUpDown” 控件(名爲“sum”),然後在“屬性” 對話框中,選擇工具欄上的“事件” 圖標(閃電的形狀)。選擇“Enter” 事件,鍵入“answer_Enter”,再按 Enter 鍵。
=
在“answer_Enter” 事件處理程序的方法中,添加以下代碼:

private void answer_Enter(object sender, EventArgs e)
{
        NumericUpDown answerBox = sender as NumericUpDown;

    if (answerBox != null)
    {
        int lengthOfAnswer = answerBox.Value.ToString().Length;
        answerBox.Select(0, lengthOfAnswer);
    }
}

在“Windows 窗體設計器” 中,選擇 difference“NumericUpDown” 控件。在“屬性”對話框的“事件”頁中,向下滾動到“Enter”事件,選擇行末尾的下拉箭頭,然後選擇剛纔添加的 answer_Enter 事件處理程序。
對 product 和 quotient NumericUpDown 控件重複上述步驟。
運行後,當選擇“NumericUpDown” 控件時,將自動選中現有值,然後在你開始輸入其他值時自動清除現有值。

添加減法問題

在之前的Form窗口代碼中,加入 int minuend; int subtrahend;

接下來,將修改 StartTheQuiz() 方法,以便爲減法題提供隨機值。

public void StartTheQuiz()
{
   
    
    addend1 = randomizer.Next(51);
    addend2 = randomizer.Next(51);

    
    plusLeftLabel.Text = addend1.ToString();
    plusRightLabel.Text = addend2.ToString();

   
    sum.Value = 0;

       minuend = randomizer.Next(1, 101);
    subtrahend = randomizer.Next(1, minuend);
    minusLeftLabel.Text = minuend.ToString();
    minusRightLabel.Text = subtrahend.ToString();
    difference.Value = 0;

        timeLeft = 30;
    timeLabel.Text = "30 seconds"; 
    timer1.Start();
}

添加之前沒有的即可。
在此示例中,想要選擇最後一個重載,因爲您可以指定最小值和最大值。
修改 CheckTheAnswer() 方法來檢查減法答案是否正確。

private bool CheckTheAnswer()
{
    if ((addend1 + addend2 == sum.Value)
        && (minuend - subtrahend == difference.Value))
        return true;
    else
        return false;
}


將計時器 Tick 事件處理程序的最後部分替換爲下面的代碼,使其在時間用完時填充正確答案。

else
{
    
    timer1.Stop();
    timeLabel.Text = "Time's up!";
    MessageBox.Show("You didn't finish in time.", "Sorry!");
    sum.Value = addend1 + addend2;
    difference.Value = minuend - subtrahend;
    startButton.Enabled = true;
}

添加乘法和除法問題

窗體class Form1 : Form代碼中添加四個整型變量

int multiplicand;
    int multiplier;

     
    int dividend;
    int divisor;

與前面的操作一樣,修改 StartTheQuiz() 方法,以便爲乘法和除法題填入隨機數。
添加以下代碼

 multiplicand = randomizer.Next(2, 11);
    multiplier = randomizer.Next(2, 11);
    timesLeftLabel.Text = multiplicand.ToString();
    timesRightLabel.Text = multiplier.ToString();
    product.Value = 0;

        divisor = randomizer.Next(2, 11);
    int temporaryQuotient = randomizer.Next(2, 11);
    dividend = divisor * temporaryQuotient;
    dividedLeftLabel.Text = dividend.ToString();
    dividedRightLabel.Text = divisor.ToString();
    quotient.Value = 0;


修改 CheckTheAnswer() 方法

private bool CheckTheAnswer()
{
    if ((addend1 + addend2 == sum.Value)
        && (minuend - subtrahend == difference.Value)
        && (multiplicand * multiplier == product.Value)
        && (dividend / divisor == quotient.Value))
        return true;
    else
        return false;
}

由於使用鍵盤無法方便地輸入乘號 (×) 和除號 (÷),因此 C# 和 Visual Basic 接受用星號 (*) 代替乘號,用斜線 (/) 代替除號。
更改計時器 Tick 事件處理程序的最後部分,使其在時間用完時填入正確答案。

else
{
    
    timeLabel.Text = "Time's up!";
    MessageBox.Show("You didn't finish in time.", "Sorry");
    sum.Value = addend1 + addend2;
    difference.Value = minuend - subtrahend;
    product.Value = multiplicand * multiplier;
    quotient.Value = dividend / divisor;
    startButton.Enabled = true;
}

運行結果:必須回答四個問題才能完成測驗。

自定義測試

通過設置“timeLabel”控件的“BackColor”屬性,使其在測驗只剩下 5 秒時變爲紅色 。
timeLabel.BackColor = Color.Red;
當測驗結束時重置顏色。
當測驗參加者在 NumericUpDown 控件中輸入正確答案時,通過播放聲音來進行提示。 (必須爲每個控件的 ValueChanged 事件編寫事件處理程序,只要用戶更改控件的值,就激發該事件。)

今天的分享就到這裏,歡迎大家在下方留言評論。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章