I'm doing a program for a class, and when I run it and type something in the txtSSN control that's not valid, it freezes up and crashes. I can't figure it out because I have another very similar project that works just fine.
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 (Ex开发者_运维问答ception 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
}
}
Judging by the comments and the code it sounds as if you don't have the event handler frmVitalStatistics_Load
connected to the load event of the form. This would cause a null pointer exception which would be consistent the error you are seeing.
As per my comments to the OP, if frmVitalStatistics_Load
isn't running, it may not be hooked up right as an event handler.
I have been unable to reproduce the error you are seeing with the code you posted. There is likely something in your frmVitalStatistics.Designer.cs file that is different than what I came up with.
As others have said, it could be an event that is missing or perhaps an extra event hooked up that isn't needed.
These are the only events I have hooked up in the form.
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);
Check your frmVitalStatistics.Designer.cs and see if you have any others or if any of these are missing.
One question... Is it freezing AS you are typing or after you click Submit?
精彩评论