在调用文本更改事件后,文本框的文本如何? (比如,文本框的默认文本称为文本)

时间:2016-03-29 20:27:40

标签: c# textbox visual-studio-2015

我正在尝试创建一个基本的数学测试,它有两个不同的随机数叫做num1和num2,还有一个叫做结果的文本框,这是用户键入答案的地方。 我创建了一个按钮,基本上检查用户输入的答案是否正确,但是添加" if"像这样的命令,似乎并没有真正检测到用户在文本框中输入的内容:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Math_Lesson_Fella
{
public partial class Form1 : Form
{
    public int function, num1, num2;
    Random numberrndm = new Random();
    Label numb1 = new Label();
    Label numb2 = new Label();
    Label functionlabel = new Label();
    NumericUpDown result = new NumericUpDown();
    Label resultlabel = new Label();
    Button check = new Button();
    public Form1()
    {
        InitializeComponent();
        int function = numberrndm.Next(1, 5);
        int num1 = numberrndm.Next(1, 10);
        int num2 = numberrndm.Next(1, 10);
        //
        // numb1
        //
        numb1.Size = new Size(60, 200);
        numb1.ForeColor = Color.Black;
        numb1.BackColor = Color.Transparent;
        numb1.Visible = true;
        numb1.Text = "" + num1;
        numb1.Location = new Point(30, 100);
        numb1.Font = new Font("David", 70, FontStyle.Bold);
        numb1.AutoSize = false;
        Controls.Add(numb1);
        //
        // numb2
        //
        numb2.Size = new Size(60, 200);
        numb2.ForeColor = Color.Black;
        numb2.BackColor = Color.Transparent;
        numb2.Visible = true;
        numb2.Text = "" + num2;
        numb2.Location = new Point(170, 100);
        numb2.Font = new Font("David", 70, FontStyle.Bold);
        numb2.AutoSize = false;
        Controls.Add(numb2);
        //
        // functionlabel
        //
        functionlabel.BringToFront();
        functionlabel.Size = new Size(70, 100);
        functionlabel.ForeColor = Color.CadetBlue;
        functionlabel.BackColor = Color.Transparent;
        functionlabel.Visible = true;
        functionlabel.Location = new Point(95, 90);
        functionlabel.Font = new Font("David", 80, FontStyle.Bold);
        functionlabel.AutoSize = false;
        Controls.Add(functionlabel);
        if (function == 1)
        {
            functionlabel.Text = "/";
        }
        if (function == 2)
        {
            functionlabel.Text = "*";
        }
        if (function == 3)
        {
            functionlabel.Text = "+";
        }
        if (function == 4)
        {
            functionlabel.Text = "-";
        }
        //
        // resultlabel
        //
        resultlabel.BringToFront();
        resultlabel.Size = new Size(60, 120);
        resultlabel.ForeColor = Color.Black;
        resultlabel.BackColor = Color.Transparent;
        resultlabel.Visible = true;
        resultlabel.Text = "=";
        resultlabel.Location = new Point(230, 90);
        resultlabel.Font = new Font("David", 70, FontStyle.Bold);
        resultlabel.AutoSize = false;
        Controls.Add(resultlabel);
        //
        // result
        //
        result.Minimum = -100;
        result.Maximum = 100;
        result.BringToFront();
        result.TextAlign= HorizontalAlignment.Center;
        result.Size = new Size(110, 110);
        result.ForeColor = Color.Crimson;
        result.BackColor = Color.Beige;
        result.Visible = true;
        result.Text = "";
        result.Location = new Point(300, 95);
        result.Font = new Font("David", 70, FontStyle.Bold);
        result.AutoSize = false;
        Controls.Add(result);
        //
        // check
        //
        check.Size = new Size(150, 100);
        check.ForeColor = Color.Black;
        check.BackColor = Color.BurlyWood;
        check.Visible = true;
        check.Text = "Check";
        check.Location = new Point(150, 300);
        check.Font = new Font("David", 30, FontStyle.Bold);
        check.AutoSize = false;
        Controls.Add(check);
        check.Click += new EventHandler(check_Click);
    }
    private void check_Click(object sender, EventArgs e)
    {
        if (function == 1)
        {
            decimal calculatedValue = num1 / (decimal)num2;

            // Compare values, rounding calculatedValue to the number of decimal places shown in the result control
            if (Math.Round(calculatedValue, result.DecimalPlaces) == result.Value)
            {
                MessageBox.Show("Correct!");
            }
            else
            {
                MessageBox.Show("Wrong! Try again!");
            }
        }
        if (function == 2)
        {
            decimal calculatedValue = num1 * (decimal)num2;

            // Compare values, rounding calculatedValue to the number of decimal places shown in the result control
            if (Math.Round(calculatedValue, result.DecimalPlaces) == result.Value)
            { 
                MessageBox.Show("Correct!");
            }
            else
            {
                MessageBox.Show("Wrong! Try again!");
            }
        }
        if (function == 3)
        {
            decimal calculatedValue = num1 + (decimal)num2;

            // Compare values, rounding calculatedValue to the number of decimal places shown in the result control
            if (Math.Round(calculatedValue, result.DecimalPlaces) == result.Value)
            {
                MessageBox.Show("Correct!");
            }
            else
            {
                MessageBox.Show("Wrong! Try again!");
            }
        }
        if (function == 4)
        {
            decimal calculatedValue = num1 - (decimal)num2;

            // Compare values, rounding calculatedValue to the number of decimal places shown in the result control
            if (Math.Round(calculatedValue, result.DecimalPlaces) == result.Value)
            {
                MessageBox.Show("Correct!");
            }
            else
            {
                MessageBox.Show("Wrong! Try again!");
            }
        }
    }
}

}

1 个答案:

答案 0 :(得分:0)

我建议您使用NumericUpDown Control代替TextBox,这样就可以摆脱文字转换。

然后你只需将你的除法结果四舍五入到NumericUpDown控件中显示的小数位数,这样你就可以直接比较这些值。

这是一个小例子,假设resultNumericUpDown控件取代:

// Calculate value (casting num2 to decimal to avoid truncation of decimal places in result)
decimal calculatedValue = num1 / (decimal)num2;

// Compare values, rounding calculatedValue to the number of decimal places shown in the result control
if(Math.Round(calculatedValue, result.DecimalPlaces) == result.Value) {

}