我正在尝试编写一个简单的GUI应用程序,它将在单击检索按钮时从Customer类加载客户数据。然后,单击“保存”按钮时,此数据将存储在Customer对象中。
我通过测试发现我的clear和retrieve按钮按预期工作,但是当我按下保存按钮时收到以下错误消息:
您的应用程序中发生了未处理的异常。如果单击“继续”,应用程序将忽略此错误并尝试继续。如果单击“退出”,应用程序将立即关闭。
字符串未被识别为有效的DateTime。从索引4开始有一个未知单词。
我在下面粘贴了我的代码。如果您能提供任何澄清,我将不胜感激。提前谢谢!
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace simple_GUI
{
public partial class Form1 : Form
{
Customer myCustomer = new Customer(); //Declare an instance of the customer class within my Windows form
public Form1()
{
InitializeComponent();
}
private void saveButton_Click(object sender, EventArgs e)
{
myCustomer.Id = Convert.ToInt32(customerIDTextBox.Text); //How to handle exceptions within my conversions?
myCustomer.Name = fullNameTextBox.Text;
myCustomer.Address = addressTextBox.Text;
myCustomer.Email = emailTextBox.Text;
myCustomer.Phone = Convert.ToDouble(phoneNumberTextBox.Text);
myCustomer.AddDate = Convert.ToDateTime(addressTextBox.Text);
myCustomer.TotalTransactions = Convert.ToInt32(totalTransactionsTextBox.Text);
}
private void retrieveButton_Click(object sender, EventArgs e)
{
customerIDTextBox.Text = Convert.ToString(myCustomer.Id);
fullNameTextBox.Text = myCustomer.Name;
addressTextBox.Text = myCustomer.Address;
emailTextBox.Text = myCustomer.Email;
phoneNumberTextBox.Text = Convert.ToString(myCustomer.Phone);
addDateTextBox.Text = Convert.ToString(myCustomer.AddDate);
totalTransactionsTextBox.Text = Convert.ToString(myCustomer.TotalTransactions);
}
private void clearButton_Click(object sender, EventArgs e)
{
customerIDTextBox.Text = "";
fullNameTextBox.Text = "";
addressTextBox.Text = "";
emailTextBox.Text = "";
phoneNumberTextBox.Text = "";
addDateTextBox.Text = "";
totalTransactionsTextBox.Text = Convert.ToString("");
}
}
}
答案 0 :(得分:1)
你能尝试演员并做一些不合作的事吗?
private bool ValidInteger(string value)
{
int notUsed=0;
return Int32.TryParse(value, out notUsed);
}
我删除了function关键字。代码现在编译。
答案 1 :(得分:0)
您可以通过添加一些代码来验证输入,例如使用AddDate:
try{
myCustomer.AddDate = Convert.ToDateTime(addressTextBox.Text);
}
catch
{
MessageBox.Show("Add Date is not valid");
return;
}
如果可能,请使用DatePicker控件输入日期。
答案 2 :(得分:0)
您可以在设置值转换之前检查是否可以转换,使用类型的TryParse()
方法,您可以进行正确的转换,尝试这样的事情
private void saveButton_Click(object sender, EventArgs e)
{
// check if the Id is a integer
int convertedId;
if (!int.TryParse(customerIDTextBox.Text, out convertedId))
{
MessageBox.Show("The ID is not a integer value");
return;
}
myCustomer.Id = convertedId;
myCustomer.Name = fullNameTextBox.Text;
myCustomer.Address = addressTextBox.Text;
myCustomer.Email = emailTextBox.Text;
myCustomer.Phone = phoneNumberTextBox.Text;
// check if the DateTime is a valid dateTeime
DateTime convertedDate;
if (!DateTime.TryParseExact(addressTextBox.Text,"MM/dd/yyyy", null, System.Globalization.DateTimeStyles.AssumeLocal, out convertedDate))
{
MessageBox.Show("The filed is not a valid date");
return;
}
myCustomer.AddDate = convertedDate;
myCustomer.TotalTransactions = Convert.ToInt32(totalTransactionsTextBox.Text);
}
答案 3 :(得分:0)
我建议你在文本框中使用正确的命名。您的代码显示,当您保存日期时,您使用的是addressTextBox.Text:
myCustomer.AddDate = Convert.ToDateTime(addressTextBox.Text);
我认为它应该是这样的:
myCustomer.AddDate = DateTime.Parse(dateTextBox.Text,"MM/dd/yyyy");
当用户选择日期时,最好使用DateTimePicker控件而不是TextBox。希望这有帮助!