【问题标题】:Why arent the inputs following the patterns?为什么输入不遵循模式?
【发布时间】:2019-03-21 14:38:57
【问题描述】:

我正在制作一个 Windows 窗体应用程序来请求联系信息。我创建了一个类来修剪输入文本并检查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();
    }
}

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

【问题讨论】:

  • 有没有一种方法可以生成一个minimal 示例并澄清您的具体问题?整个代码都很好,但如果您可以缩小特定问题/代码部分的范围,对每个人来说都会更快。
  • @Chris 这段代码令人困惑的是我不知道错误在哪里,因为没有任何错误。如果我必须选择一个,我会选择这个。
  • private bool GetEmail(ref string emailpattern) { bool success = true;试试 { Input.TrimText(txtEmail.Text); //删除空格 if (Input.IsTextEmpty(txtEmail.Text)) throw new InputRequiredException(); emailpattern = txtEmail.Text;成功=真; } catch (Exception error) { string remediation = "Enter a valid email."; Input.ShowError(错误,补救); Input.SelectText(txtEmail); } 返回成功;

标签: c# regex exception get trim


【解决方案1】:

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

【讨论】:

  • 同样的结果@mardukar
  • 每次你使用Input.TrimText(...)你都没有把它写回源代码。你都改正了吗?
  • 谢谢。我已经做了。这解决了修剪文本问题。但是它仍然没有检查模式或捕获异常。
  • 我已经扩展了我的答案@XaviorHansa
猜你喜欢
  • 2016-04-26
  • 1970-01-01
  • 2019-07-04
  • 1970-01-01
  • 1970-01-01
  • 2019-05-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多