overload or delegate?

  • Thread starter Thread starter dave
  • Start date Start date
D

dave

Hi ,

I am trying to avoid behavior where everytime Button1Click is fired a new
form is exposed.

How can I keep the f object alive in the event so that a new object does not
have to be created?

Could I overload the Button Click event to accept the Form1 object?

void Button1Click(object sender, EventArgs e)

{

Form1 f = new Form1();

f.Show();

f.TextboxGet(this.textBox1.Text);

}



Thanks

D
 
dave said:
I am trying to avoid behavior where everytime Button1Click is fired a new
form is exposed.

How can I keep the f object alive in the event so that a new object does not
have to be created?

You need to have the "child" form as a variable within the "parent"
form, so that the "parent" knows what to refer to when the button is
clicked.
 
Dave,

You should have a field in the class itself for the Form1 instance. If
it is null, then create a new instance, and assign. You also want to hook
up to the Disposed event on the form, in the event that the form is closed.
Then in the event handler for the Disposed event, you would set the field
back to null.

Then, call Show on the instance.
 
Hi ,

I am trying to avoid behavior where everytime Button1Click is fired a new
form is exposed.

How can I keep the f object alive in the event so that a new object does not
have to be created?

Could I overload the Button Click event to accept the Form1 object?

void Button1Click(object sender, EventArgs e)

{

Form1 f = new Form1();

f.Show();

f.TextboxGet(this.textBox1.Text);

}

Thanks

D

I would probably have made the form2 a global variable since there is
only going to be one of them, but I realize that some people just
don't like that.

private void button1_click(object sender, eventargs e)
{
FormCollection fc = System.Windows.Forms.Application.OpenForms;
bool bFrmExists = false;
foreach (Form f in fc)
if (f.GetType() == typeof(Form2))
{
bFrmExists = true;
break;
}
if (!bFrmExists)
{
Form2 frm = new Form2();
frm.Show();
}
}
 

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

Back
Top