Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Form Closes When it Shouldn't

Tags:

c#

winforms

I have two Forms in my application. They way I call Form 2 is like this:

Form 1:

private void btnTest_Click(object sender, EventArgs e)
{
    DialogResult result = new System.Windows.Forms.DialogResult();
    Add_Link addLink = new Add_Link();
    result=addLink.ShowDialog();

    if (result == System.Windows.Forms.DialogResult.OK)
    {
        //
    }
}

Form 2:

private void btnAdd_Click(object sender, EventArgs e)
{            
    if(validURL(txtSubLink.Text))
    {
        HyperLink add = new HyperLink(txtSubLink.Text,txtSubText.Text,"URL");
        this.build = add;                 
    }
    else
    {
        MessageBox.Show("Valid URL Needed! " + txtSubLink.Text, "ERROR");
    }             
}

My problem is if the user clicks the Add button, the error message shows(because the data is invalid or the textboxes are empty) BUT it closes the form. I only want the user to close the form and pass the data back if the two textboxes contain the proper data. If the two textboxes don't contain the proper data OR is empty, when the user clicks Add, the error message should show, and the Form 2 should remain open, How do I get that to happen...?

like image 581
Sylvia Rosemond Avatar asked Dec 24 '12 20:12

Sylvia Rosemond


1 Answers

I suspect your btnAdd has its DialogResult property set to OK. Unset that, and then add this.DialogResult = DialogResult.OK in your event handler when you're satisfied with the input.

private void btnAdd_Click(object sender, EventArgs e)
{

    if(validURL(txtSubLink.Text))
    {
         HyperLink add = new HyperLink(txtSubLink.Text,txtSubText.Text,"URL");
         this.build = add;
         this.DialogResult = DialogResult.OK;

    }
    else
    {
        MessageBox.Show("Valid URL Needed! " + txtSubLink.Text, "ERROR");
    }

}
like image 129
Jon B Avatar answered Sep 28 '22 02:09

Jon B