Search code examples
c#.netmessagebox

App crash when calling MessageBox


Hello i call up a MessageBox when validating some data, if the data is wrong I message the user with a MessageBox with following code:

private void button1_Click(object sender, EventArgs e)
{
    if (textBox_name.Text.Trim() != "" 
        || textBox_X.Text.Trim() != "" 
        || textBox_Y.Text.Trim() != "")
    {
        if (graph.getNodoNome(textBox_nome.Text.Trim()) != null)
        {
            MessageBox.Show("Data is wrong?", "Error");

            resetTextBoxes();
            return;
        }

    // randome stuff
    }
}

My app crash with this, when i remove the MessageBox works fine. Thanks for the help

EDIT: Pastebay link for the entire method http://pastebay.com/82690


Solution

  • You should mention the type of exception.

    Since I can't assume that, I'll comment on other potential sources of error?

    Honestly also, your

            if (textBox_nome.Text.Trim() != "" 
                || textBox_X.Text.Trim() != "" 
                || textBox_Y.Text.Trim() != "")
    

    should be

            if (textBox_nome.Text.Trim() != "" 
                && textBox_X.Text.Trim() != "" 
                && textBox_Y.Text.Trim() != "")
    

    to make sure all fields are filled in.

    Int.Parse will throw if invalid, I suggest (in pseudo C#):

    int x;
    if(!int.TryParse(text, out x)) x = 0; // some default value