System.FormatException:'输入字符串的格式不正确。使用文本框

时间:2019-08-21 15:05:21

标签: c#

Score Calculator

代码看起来应该可以工作,但是正在获取System.FormatException:'输入字符串的格式不正确。'错误出现在第23、24和25行。还有另一种方法可以将转换指向文本框吗?

    decimal Score = Convert.ToDecimal(txtScore.Text);
    decimal TotalScore = Convert.ToDecimal(txtScoreTotal.Text);
    decimal ScoreCount = Convert.ToDecimal(txtScoreCount.Text);

我尝试了相同代码的不同变体使它无法正常工作

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 Extra_4_2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.KeyPreview = true;
        }

    private void BtnAdd_Click(object sender, EventArgs e)
    {
        decimal Score = Convert.ToDecimal(txtScore.Text);
        decimal TotalScore = Convert.ToDecimal(txtScoreTotal.Text);
        decimal ScoreCount = Convert.ToDecimal(txtScoreCount.Text);

        TotalScore += Score;
        ScoreCount++;
    }

    private void BtnClearScores_Click(object sender, EventArgs e)
    {
        txtScore.Text = "";
        txtScoreTotal.Text = "";
        txtScoreCount.Text = "";
        txtAverage.Text = "";
    }

    private void Form1_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Escape)
            Application.Exit();
    }
}
}

0 个答案:

没有答案