Daniel Lip
Daniel Lip

Reputation: 11321

Why the new Form is close on the second time i click OK and the textBox is empty?

private void button2_Click(object sender, EventArgs e)
        {
            cl = new ChangeLink();
            cl.StartPosition = FormStartPosition.CenterParent;
            DialogResult dr = cl.ShowDialog(this);
            if (dr == DialogResult.Ok)
            {
                if (cl.getText() == "")
                {
                    MessageBox.Show("The TextBox Cannot Be Empty");
                    cl.ShowDialog(this);
                    return;
                }
                else
                {
                    label4.Text = cl.getText();
                    cl.Close();
                }
            }
            else if (dr == DialogResult.Cancel)
            { 
                cl.Close();
            }

cl is a new Form i mgetting the text from. Now im check that if cl.getText() is empty "" its throwing a message to the user and when the user click ok on the messagebox i want it to return and show the new Form dialog again. When i click once on the OK button of the new Form and it show the messaeBox message and then show me again the dialogresult box of the new Form but then when i click on OK again and the textBox is still empty it doesnt show the messageBox again just close the new Form and set label4 text to be empty.

I want that each time the user click OK and the textBox is empty it will keep show the new Form textBox dialog untill the user click Cancel or put something in the textBox and then click OK.

Upvotes: 1

Views: 271

Answers (1)

Mark Hall
Mark Hall

Reputation: 54532

It wiil be a lot cleaner if you do the checking in your second Form. You will need to add code to your Form2's OK Button's Click Event. Make sure you remove the default DialogResult from the OK Button Property's.

private void button2_Click(object sender, EventArgs e)
{
    if(!string.IsNullOrEmpty(textBox1.Text))
        DialogResult = DialogResult.OK;
}

Upvotes: 2

Related Questions