为什么输入不遵循模式?

Why arent the inputs following the patterns?

我正在制作一个 Windows 表单应用来请求联系信息。我创建了一个 class 到 trim 输入文本并检查 TextBox 是否为空。我还为 emailphonenumber 分配了模式。但是,当我 运行 应用程序时,其中 none 似乎在工作。

表格

string quantity = string.Empty;
string name = string.Empty;
string emailpattern = string.Empty;
string phonepattern = string.Empty;

const int ContactWindow = 90;
const int SuggestedContactOffset = 3;

#region Constructors

public frmRequestContact()
{
    InitializeComponent();
}

#endregion


#region Event Handlers

private void frmRequestContact_Load(object sender, EventArgs e)
{
    //initialize contact date control
    dtpContactDate.MaxDate = DateTime.Today.AddDays(ContactWindow);
    dtpContactDate.MinDate = DateTime.Today;
    dtpContactDate.Value = DateTime.Today.AddDays(SuggestedContactOffset);

    //initialize contact method control
    rbEmail.Checked = true;
}   

private void btnContact_Click(object sender, EventArgs e)
{
    // Input variables
    string quantity = string.Empty;
    string name = string.Empty;
    string emailpattern = @"^[A-Z0-9._%+-]+@[A-Z0-9.-]+.[A-Z]{2,}$";
    string phonepattern = @"^(\+\d{1,2}\s)?\(?\d{3}\)?[\s.-]\d{3}[\s.-]\d{4}$";

    // Contact Date
    DateTime contactDate = DateTime.MinValue;

    // Contact Method
    ContactMethod method = ContactMethod.Unassigned;

    // Gather inputs
    if (GetContactDate(ref contactDate) 
        && GetContactMethod(ref method) 
        && GetEmail(ref emailpattern) 
        && GetName(ref name) 
        && GetQuantity(ref quantity) 
        && GetPhone(ref phonepattern))
    {
        // Submit contact request and close the form
        string format = "Your contact request has been entered.\n\n" 
            + "Quantity: {0}\n" 
            + "Name: {1}\n" 
            + "Email: {2}\n" 
            + "Phone: {3}\n" 
            + "Contact Date: {4:D}\n" 
            + "Contact Method: {5}\n";

        string msg = string.Format(format, quantity, name, emailpattern, phonepattern, contactDate, method);
        MessageBox.Show(msg, Application.ProductName);

        Close();
    }
}

private void TextBox_Leave(object sender, EventArgs e)
{
    TextBox tb = (TextBox)sender;
    Input.TrimText(tb.Text);
}

#endregion


#region Input

private bool GetQuantity(ref string quantity)
{
    bool success = true;
    try
    {
        Input.TrimText(txtQuantity.Text);
        if (Input.IsTextEmpty(txtQuantity.Text))
            throw new InputRequiredException();
        quantity = txtQuantity.Text;

        return success;
    }
    catch (Exception error)
    {
        string remediation = "Enter quantity.";
        Input.ShowError(error, remediation);
        Input.SelectText(txtQuantity);
    }
    return success;
}

private bool GetName(ref string name)
{
    bool success = true;
    try
    {
        Input.TrimText(txtName.Text);//removing whitespace
        if (Input.IsTextEmpty(txtName.Text))//checking if the input is empty, if so throw new exception
            throw new InputRequiredException();
        name = txtName.Text;
        success = true;
    }
    catch (Exception error)
    {
        string remediation = "Enter name of individual to contact.";
        Input.ShowError(error, remediation);
        Input.SelectText(txtName);
    }
    return success;
}

private bool GetEmail(ref string emailpattern)
{
    bool success = true;
    try
    {
        Input.TrimText(txtEmail.Text); //removing whitespace
        if (Input.IsTextEmpty(txtEmail.Text))
            throw new InputRequiredException();
        emailpattern = txtEmail.Text;
        success = true;
    }
    catch (Exception error)
    {
        string remediation = "Enter a valid email.";
        Input.ShowError(error, remediation);
        Input.SelectText(txtEmail);
    }
    return success;
}

bool GetPhone(ref string phonepattern)
{
    bool success = true;
    try
    {
        Input.TrimText(txtPhone.Text);
        if (Input.IsTextEmpty(txtPhone.Text))
            throw new InputRequiredException();
        phonepattern = txtPhone.Text;
        success = true;
    }
    catch(Exception error)
    {
        string remediation = "Enter a valid phone number.";
        Input.ShowError(error, remediation);
        Input.SelectText(txtPhone);
    }
    try
    {
        int Phone = Convert.ToInt32(txtPhone.Text);
    }
    catch (Exception error)
    {
        string remediation = "Enter a valid phone number.";
        Input.ShowError(error, remediation);
        Input.SelectText(txtPhone);

    }
        return success;
}

bool GetContactDate(ref DateTime contactDate)
{
    contactDate = dtpContactDate.Value;
    return true;
}

bool GetContactMethod(ref ContactMethod method)
{
    //find selected option
    if (rbEmail.Checked)
        method = ContactMethod.Email;
    else if (rbPhone.Checked)
        method = ContactMethod.Phone;
    else if (rbEither.Checked)
        method = ContactMethod.Either;
    else //no option selected!
    {
        Showerror("Select a contact method");
        return true;
    }

    return true;
}

void Showerror(string msg)
{
    MessageBox.Show(msg, "Request Contact", MessageBoxButtons.OK, MessageBoxIcon.Error);
}

#endregion

输入

class Input
{
    static public string TrimText(string A)
    {
        return A.Trim();
    }

    internal static bool IsTextEmpty(string A)
    {
        if (string.IsNullOrEmpty(A))
        {
            return true;
        }

        else
        {
            return false;
        }
    }

    internal static void ShowError(object error, string remediation)
    {           
    }

    static public void SelectText(TextBox textBox1)
    {
         textBox1.SelectAll();
    }
}

代码和程序中没有错误显示 运行 很顺利,但我没有得到所需的输出。在编码方面我是一个彻头彻尾的菜鸟,并且理解代码可能存在逻辑错误。无论是一个错误还是多个错误,或者如果代码只是未完成,请随时告诉我。

您没有将修剪后的文本写回到 TextBox

private void TextBox_Leave(object sender, EventArgs e)
{
    TextBox tb = (TextBox)sender;
    // Input.TrimText(tb.Text); // <-- your code
    tb .Text = Input.TrimText(tb.Text);
}

关于emailpattern。目前,您只需将 txtEmail.Text 的值分配给它,然后将其显示在 MessageBox 中。在下面的代码中,我删除了所有东西,只留下了你用 emailpattern 做某事的部分。

private void btnContact_Click(object sender, EventArgs e)
{
    // Input variables   
    string emailpattern = @"^[A-Z0-9._%+-]+@[A-Z0-9.-]+.[A-Z]{2,}$";    

    if (GetEmail(ref emailpattern))
    {        
        string msg = string.Format(format, quantity, name, emailpattern, phonepattern, contactDate, method);
        MessageBox.Show(msg, Application.ProductName);
        Close();
    }
}

private bool GetEmail(ref string emailpattern)
{  
    emailpattern = txtEmail.Text;
    success = true;    
    return success;
}