c# kill程序中的正则表达式

本文关键字:正则表达式 程序 kill | 更新日期: 2023-09-27 17:49:46

我正在为一个类做一个程序,当我运行它并在txtsn控件中输入无效的东西时,它会冻结并崩溃。我想不出来,因为我有另一个非常相似的项目,运行得很好。

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

    }

}

c# kill程序中的正则表达式

根据注释和代码判断,听起来好像您没有将事件处理程序frmVitalStatistics_Load连接到表单的加载事件。这将导致空指针异常,这将与您看到的错误一致。

根据我对OP的评论,如果frmVitalStatistics_Load没有运行,它可能没有作为事件处理程序连接起来。

我一直无法重现您所看到的错误与您发布的代码。在你的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,看看你是否有其他的或者是否有这些缺失。

一个问题……是在你打字的时候还是在你点击提交之后死机了?