Closing a dynamically created form from click event

E

eljainc

Hello,

I have the following code:


Form myForm = new Form();
myForm.Width = 700;
myForm.Height = 500;

.....and then a button:

Button closeButton = new Button();
closeButton.Parent = myForm;
close Button.Text = "Close";
closeButton.Click += new EventHandler(copyButton_Click);
closeButton.AutoSize = true;

myForm.ShowDialog();


In my copyButton_Click delegate, I want to close this form. However I
don't know
how to access the myForm object as it is outside of the scope:

private void copyButton_Click(object sender, EventArgs e)
{
this.Close()
}


This will close the main form, not the local form myForm created
dynamically.
How do I close myForm in the click handler?

Thanks
Mike
 
P

Peter Duniho

eljainc said:
Hello,

I have the following code:


Form myForm = new Form();
myForm.Width = 700;
myForm.Height = 500;

.....and then a button:

Button closeButton = new Button();
closeButton.Parent = myForm;
close Button.Text = "Close";
closeButton.Click += new EventHandler(copyButton_Click);
closeButton.AutoSize = true;

myForm.ShowDialog();


In my copyButton_Click delegate, I want to close this form.

Why do you use the word "copy" for the method name of an event handler
for a button named "close" and for which the operation is apparently to
close?
However I
don't know
how to access the myForm object as it is outside of the scope:

private void copyButton_Click(object sender, EventArgs e)
{
this.Close()
}


This will close the main form, not the local form myForm created
dynamically.
How do I close myForm in the click handler?

Typically, one would put the event handler for a button found in a form
as a method in that form class, not some other form class. So the most
obvious answer is for you to put your event handler in the form you want
to close. Of course, to do that you need to create a Form subclass
instead of just using Form directly.

Alternatively, since the form is the owner of the button being clicked,
and you _do_ have the reference to the button in your event handler, you
can just look at the parent of the button.

Alternatively, since your main form created this second form, you could
store the reference to the second form as a class member in your main
form, so that when the event handler executes, it can simply retrieve
the value stored.

Yet another alternative is to forego the named event handler altogether,
and take advantage of variable capturing in an anonymous method to
accomplish the previous suggestion implicitly. For example:

private void showDialogButton_Click(object sender, EventArgs e)
{
using (Form myForm = new Form())
{
myForm.Width = 700;
myForm.Height = 500;

Button closeButton = new Button();

closeButton.Parent = myForm;
closeButton.Text = "Close";
closeButton.Click += (sender, e) =>
{
myForm.Close();
}
closeButton.AutoSize = true;

myForm.ShowDialog();
}
}

And yet another alternative is for you to simply make the button you
created the AcceptButton or CancelButton for the Form you created
(depending on the semantics you want, for setting the DialogResult of
the Form for the return value of the ShowDialog() method), and setting
the Button's DialogResult property appropriately. For example:

private void showDialogButton_Click(object sender, EventArgs e)
{
using (Form myForm = new Form())
{
myForm.Width = 700;
myForm.Height = 500;

Button closeButton = new Button();

closeButton.Parent = myForm;
closeButton.Text = "Close";
closeButton.DialogResult = DialogResult.OK;
myForm.AcceptButton = closeButton;
closeButton.AutoSize = true;

myForm.ShowDialog();
}
}

I'm sure that's not an exhaustive list. Those are just the techniques
that come to mind at the moment (actually I've thought of two more since
writing that sentence, but I think the above should suffice for now :) ).

In any case, note in all cases that since you are using the ShowDialog()
method to display the form, it is imperative that you dispose the form
after ShowDialog() returns. In both of the above code examples, I use
the "using" statement to accomplish that correctly and conveniently.

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top