473,395 Members | 1,774 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

form only open one time???

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
Nov 17 '05 #1
5 8167
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


"aaaa" <aa*@aaa.pt> wrote in message
news:e9**************@tk2msftngp13.phx.gbl...
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

Nov 17 '05 #2
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
"Tim Haughton" <ti*********@gmail.com> escreveu na mensagem
news:LY*******************@fe02.news.easynews.com. ..
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


"aaaa" <aa*@aaa.pt> wrote in message
news:e9**************@tk2msftngp13.phx.gbl...
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


Nov 17 '05 #3
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.

Nov 17 '05 #4
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

"Bruce Wood" <br*******@canada.com> escreveu na mensagem
news:11**********************@g49g2000cwa.googlegr oups.com...
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.

Nov 17 '05 #5
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();
}
}

Nov 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Nick | last post by:
Loop to create an array from a dynamic form. I'm having trouble with an application, and I'll try to explain it as clearly as possible: 1. I have a form with two fields, say Apples and...
4
by: Tonya | last post by:
Hi, Does anyone have any example of how i can manage forms in my application?? I want to be able to reference my form instances that are currently open from other forms. why cant i open...
4
by: Rizyak | last post by:
This message is cross posted in alt.comp.lang.php & comp.lang.javascript I have a form for a user to input an establishment's hours and what time an event is taking place. After the user inputs...
3
by: Susan Bricker | last post by:
Greetings. I have three forms that are open at the same time. They are related and cascading. The first form (frmEventAdd) is the anchor. Each event can have many Trials. The second form is...
6
by: dbuchanan | last post by:
VS2005 I've been reading all the help I can on the topic (MSDN, other) but I can't make sense of this. Desired behavior; The user is to choose from the displayed list of the databound combobox...
3
by: rdemyan via AccessMonster.com | last post by:
My application is split into a front end and back end. Each user has their own copy of the front end. There are a few forms I only want to be open for one user at a time. So I've implemented the...
2
by: jpr | last post by:
I am using moduled to open forms. I run them from a menu that I have created on the toolbar. The source of the forms are queries all with record source to "lstpreinterview" on a form named eforms....
19
by: =?Utf-8?B?R3JlZw==?= | last post by:
How can I tell via code if a Form is already open. Each time my forms load I have some initialization code that runs, but if the form is already open and hidden I don't want that initialization...
5
by: Neil | last post by:
"lyle" <lyle.fairfield@gmail.comwrote in message news:48c3dde7-07bd-48b8-91c3-e157b703f92b@f3g2000hsg.googlegroups.com... Question for you. I'm doing something similar, only, instead of opening...
5
by: billa856 | last post by:
Hi, My project is in MS Access 2002. In that I want to open one form multiple times. I put one button on my main form. Now whenever I click on that button than form will be open. Now when I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.