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 事件编写事件处理程序,只要用户更改控件的值,就激发该事件。)

今天的分享就到这里,欢迎大家在下方留言评论。

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