为什么不遵循模式输入?

时间:2019-03-21 14:38:57

标签: c# regex exception get trim

我正在制作Windows Forms应用程序以请求联系信息。我创建了一个类来修剪输入文本,并检查TextBox是否为空。我还为emailphonenumber分配了模式。但是,当我运行该应用程序时,这些似乎都没有起作用。

表格

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();
    }
}

代码中没有显示任何错误,程序运行流畅,但是我没有得到想要的输出。在编码方面,我是一个绝对的菜鸟,并且了解代码可能存在逻辑错误。无论是一个错误还是多个错误,或者代码只是未完成,请随时让我知道。

1 个答案:

答案 0 :(得分:0)

您不会将修剪后的文本写回到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;
}