form only open one time???

A

aaaa

I there,

I have the folowing problem:

In Form1 i have a button that open a new form (Form2). If a clik several
times in that button, i open several form's of Form2.
I don't want this!!! if a form2 is open it can not create a open a new form.
How can a do this??

PS: the showdialog() method can not be used.

Thanks in advance,

Tiago Costa
 
T

Tim Haughton

Declare an instance of Form2 as a field in Form1.

public class Form1
{
private Form2 form2;

//.......Rest of class


Then the event handler....

private void ClickHandler( object sender, ClickEventArgs e )
{
if ( form2 == null ) form2 = new Form2();
form2.Show();
}

That should do it.

--
Regards,

Tim Haughton

Agitek
http://agitek.co.uk
http://blogitek.com/timhaughton
 
A

aaaa

that helps, now i have the folowing problem:

1. Open the Form2
2. Close the Form2
3. Try to open the Form2, gives me this error: "Cannot access a disposed
object named "frmClientes"

How can i fix it???

thanks in advance,

Tiago Costa
 
B

Bruce Wood

I'm assuming that you're showing Form2 using "Show", not "ShowDialog",
so Form1 doesn't know when Form2 closes.

You should probably write Form2 as a Singleton. Look into the SIngleton
coding pattern. Here is a rough outline:

public class Form2 : System.Windows.Form
{
private static Form2 singleton = null;

private Form2()
{
... do constructor stuff ...
}

public static Form2 Instance
{
get
{
if (Form2.singleton == null)
{
Form2.singleton = new Form2();
}
return Form2.singleton;
}
}

protected override void OnClose()
{
Form2.singleton = null;
}
}

Notice that the constructor is "private"! The only way to get an
instance of Form2 is to invoke Form2.Instance, which gives you back the
one that's already there. So, in Form1, when you want to show Form2:

public class Form1 : System.Windows.Form
{
private void btnForm2_Click(object sender, System.EventArgs ea)
{
Form2 f2 = Form2.Instance;
... do any set-up for showing f2 ...
f2.Show();
}
}

of course, for the simplest case, you could just say:

private void btnForm2_Click(object sender, System.EventArgs ea)
{
Form2.Instance.Show();
}

and that would work, too.
 
A

aaaa

It works :)

thanks Bruce!!!

Now i have another problem relation to this one.

I Have 2 buttons (to open Form2 and Form3).
If i:

1. Open Form2
2. Open Form3
1. Clik on the Button to "open" Form2, i hould like that this form that is
alredy open to pass to the front of Form3. I try with the TopMost Proprety
but it didn't work :(

can anyone help me.

Thanks in advance to all!!!

Tiago Costa
 
B

Bruce Wood

Try this:

private void btnForm2_Click(object sender, System.EventArgs ea)
{
Form2 f2 = Form2.Instance;
if (f2.Visible)
{
f2.Activate();
}
else
{
... do any set-up for showing f2 ...
f2.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

Top