473,763 Members | 5,412 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 8200
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******** ******@tk2msftn gp13.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 "frmCliente s"

How can i fix it???

thanks in advance,

Tiago Costa
"Tim Haughton" <ti*********@gm ail.com> escreveu na mensagem
news:LY******** ***********@fe0 2.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******** ******@tk2msftn gp13.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.singleto n == 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.EventArg s 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.EventArg s 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*******@cana da.com> escreveu na mensagem
news:11******** **************@ g49g2000cwa.goo glegroups.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.singleto n == 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.EventArg s 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.EventArg s ea)
{
Form2.Instance. Show();
}

and that would work, too.

Nov 17 '05 #5
Try this:

private void btnForm2_Click( object sender, System.EventArg s 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
4082
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 Oranges. The user selects from a drop down menu, between 1-10 of each and clicks submit. The resulting page will display a NEW form, with rows and a list of fields for the amount of each items selected.
4
2190
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 and close forms as easily as in VB6??
4
4997
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 their establishment's hours of operation I want the form elements lower in the form to adjust so that an event can only happen when the place is open. I have two fields for the hours: These are both select fields with values between 0-23
3
6825
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 frmTrialInfo. Each Trial can have three Classes. The third form is frmClassInfo. These forms are used for update and adding new records. The user displays an event for update (frmEventAdd). Then clicks on a button to display the Trials for that event...
6
2883
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 and the coresponding 'Id' from the lookup table is to be inserted into the field of the new record. I have two simple tables. "tblPerson" is the data table. The lookup
3
2820
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 following strategy: 1) Create a table with the fields FormName, OpenStatus, FormUser 2) When a user tries to open the form, the code in the Form Load event checks the table for the OpenStatus value (True/False). 3) If the status is True, then...
2
2951
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. Public Function Openmyform() If IsNull(Forms!eforms!lstPreInterview) Then MsgBox "Sorry. You need to select a record!" Exit Function Else
19
19043
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 code to run. Thus, I just want to unhide the form. My thought is eveyrtime I go to load a form I could scan through all the open forms in my project and determine whether the one I'm getting ready to load is open or not.
5
3311
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 the forms all at once, I'm opening them as needed. I have a main form with multiple records; and then I have a pop-up form that the user opens with button. The pop-up form contains one record relating to the current record in the main form (but...
5
7652
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 click again on that same button than it will open same form again in seperate window. But instead of that already open form comes.and so i couldn't open that form multiple times. Already open form must be remain as it is even if I click on that button...
0
9386
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10144
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9937
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8821
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7366
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6642
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5270
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3917
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3522
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.