我正在为一个类做一个程序,当我运行它并在txtSSN控件中键入一些无效的东西时,它会冻结并崩溃。我无法理解,因为我有另一个非常相似的项目,工作正常。
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;
using System.Text.RegularExpressions;
namespace VitalStatistics
{
public partial class frmVitalStatistics : Form
{
#region Declarations
const String AppTitle = "Vital Statistics";
const float hoursOffset = 24.999F;
Regex ssnRegex;
#endregion
#region Constructors
public frmVitalStatistics()
{
InitializeComponent();
}
#endregion
#region Event Handlers
private void frmVitalStatistics_Load(object sender, EventArgs e)
{
// Initialize SSN input control
RegexOptions options = RegexOptions.IgnorePatternWhitespace;
string pattern = @"\A\d{3}-\d{3}-\d{4}\Z";
ssnRegex = new Regex(pattern, options);
// Init. Gender controls
optGender = Gender.Unassigned;
rbFemale.Tag = Gender.Male;
rbMale.Tag = Gender.Female;
// Init dtpBirth controls
dtpBirth.MinDate = DateTime.Today;
dtpBirth.MaxDate = DateTime.Today.AddHours(hoursOffset);
dtpBirth.Value = DateTime.Today;
}
private void btnSubmit_Click(object sender, EventArgs e)
{
string name = String.Empty;
string ssn = String.Empty;
int length = 0;
int weight = 0;
DateTime birthDate = DateTime.MinValue;
Gender gender = Gender.Unassigned;
//Gather inputs
if (GetName(ref name) &&
GetSSN(ref ssn) &&
GetLength(ref length) &&
GetWeight(ref weight) &&
GetGender(ref gender) &&
GetBirthDate(ref birthDate))
{
//submit & close
string format =
"Thank you for submitting your contact information. \n\n" +
"Name: {0}\n" +
"SSN: {1}\n" +
"Length: {2}\n" +
"Weight: {3}\n" +
"Gender: {4}\n" +
"Birth Date & Time: {5:D}\n";
string msg = String.Format(format, name, ssn, length, weight, gender, birthDate);
MessageBox.Show(msg,AppTitle);
Close();
}
}
private Gender optGender;
private void Gender_CheckedChanged(object sender, EventArgs e)
{
RadioButton rb = (RadioButton)sender;
optGender = (rb.Checked ? (Gender)rb.Tag : Gender.Unassigned);
}
#endregion
#region Implementation
bool GetName(ref string name)
{
if (String.IsNullOrWhiteSpace(txtName.Text))
{
txtName.SelectAll();
txtName.Focus();
ShowError("Please enter your name.\n Names cannot consist of whitespace.");
return false;
}
name = txtName.Text.Trim();
return true;
}
bool GetSSN(ref string ssn)
{
txtSSN.Text = txtSSN.Text.Trim();
Match match = ssnRegex.Match(txtSSN.Text);
if (!match.Success)
{
txtSSN.SelectAll();
txtSSN.Focus();
ShowError("Unrecognized format for SSN. Please enter in the following format: 000-000-0000.");
return false;
}
ssn = txtSSN.Text;
return true;
}
bool GetLength(ref int length)
{
int value;
try
{
if (String.IsNullOrWhiteSpace(txtLength.Text))
throw new ArgumentException("Field cannot be empty or contain spaces.");
value = int.Parse(txtLength.Text);
}
catch (Exception ex)
{
// Select text and set focus
txtLength.SelectAll();
txtLength.Focus();
// Set up error Message
string msg = String.Format("{0}", ex);
ShowError(ex.Message);
return false;
}
length = value;
return true;
}
bool GetWeight(ref int weight)
{
int value;
try
{
if (String.IsNullOrWhiteSpace(txtWeight.Text))
throw new ArgumentException("Field cannot be empty or contain spaces.");
value = int.Parse(txtLength.Text);
}
catch (Exception ex)
{
// Select text and set focus
txtWeight.SelectAll();
txtWeight.Focus();
// Set up error Message
string msg = String.Format("{0}", ex);
ShowError(ex.Message);
return false;
}
weight = value;
return true;
}
bool GetGender(ref Gender gender)
{
if (optGender == Gender.Unassigned)
{
ShowError("Select a Gender.");
return false;
}
gender = optGender;
return true;
}
bool GetBirthDate(ref DateTime birthDate)
{
birthDate = dtpBirth.Value;
return true;
}
void ShowError(string msg)
{
MessageBox.Show(msg, AppTitle, MessageBoxButtons.OK, MessageBoxIcon.None);
}
#endregion
}
}
答案 0 :(得分:0)
通过注释和代码来判断,就好像您没有将事件处理程序frmVitalStatistics_Load
连接到表单的load事件一样。这将导致空指针异常,这与您看到的错误一致。
答案 1 :(得分:0)
根据我对OP的评论,如果frmVitalStatistics_Load
未运行,则可能无法将其作为事件处理程序连接。
答案 2 :(得分:0)
我无法使用您发布的代码重现您看到的错误。您的frmVitalStatistics.Designer.cs文件中可能存在与我提出的文件不同的内容。
正如其他人所说,这可能是一个缺失的事件,或者可能是一个不需要的额外事件。
这是我在表单中连接的唯一事件。
this.rbMale.CheckedChanged += new System.EventHandler(this.Gender_CheckedChanged);
this.rbFemale.CheckedChanged += new System.EventHandler(this.Gender_CheckedChanged);
this.btnSubmit.Click += new System.EventHandler(this.btnSubmit_Click);
this.Load += new System.EventHandler(this.frmVitalStatistics_Load);
检查你的frmVitalStatistics.Designer.cs,看看你是否还有其他人,或者是否有任何遗漏。
一个问题......当您输入或点击提交后,它是否会冻结?