FormatException输入字符串的格式不正确

来源:百度知道 编辑:UC知道 时间:2024/06/25 08:44:00
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace 例7_1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void btnGrade_Click(object sender, EventArgs e)
{

string name;
int score;
name = txtName.Text;
score = int.Parse(txtScore.Text);此行出错
StudentInformation stu = new StudentInformation();
stu.name = name;
stu.Score = score;
stu.Grade();
}
}
}

把 int.Parse改为Convert.ToInt32试试
try
{
string name;
int score;
name = txtName.Text;
score = Convert.ToInt32(txtScore.Text);
StudentInformation stu = new StudentInformation();
stu.name = name;
stu.Score = score;
stu.Grade();
}
catch (Exception e1) { }