views:

203

answers:

2

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

A: 

I think we need more info on the "graph.getNodoNome" and "randome stuff" parts. If you comment out those lines, it runs just fine. I pretty much guarantee the message box is not the problem.

I would bet it’s the getNodoNome method or the random stuff that is getting you.

1) Comment them both out, and see if it "crashes." 2) Uncomment the first "graph.getNodoNome" and see if it crashes 3) Comment out "graph..." uncomment "randome stuff" and see if it crashes

If you're not getting error messages, you need to take baby steps.

RepDetec
graph.getNodoNome(string N) finds and returns an Object, if not found it returns null. If i comment the "MessageBox.Show("Data is wrong?", "Error");" and i make the IF condition true, there are no errors and the resetTextBoxes() works fine.
Ricardo
Is graph from a third party library, or is it code you wrote? If a third party library, which one?
Nick
My own code. Thanks for all the help people
Ricardo
Try this, instead of using "return." Just have an "else" statement after the "if (graph.getNodoNome(textBox_nome.Text.Trim()) != null)." Put the "random stuff" in that else statement. I wonder if the immediate return is doing something strange with the message box.Also, I'd like to see the code for:GetNodoNome code.
RepDetec
+1  A: 

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
Dearmash
Thank you for your response.Right now I'm a bit low on time, but I'll check this as soon as i can. I think it might be it
Ricardo