473,417 Members | 1,569 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,417 software developers and data experts.

Disabling multiple instances of a MDIChild form.

VM
Hi,

Is it possible to open always only one instance of a MDIChild form?
Say, if form A is already opened, just give it the focus.
I tried declaring theMDIChild forms as private members of the MDI form
(using the MDI menu's script), but when the MDIChild form is closed (x
button pushed), I cannot open again the MDIChild form.

Example:

// aForm is a private member of the MDI form.

if (aForm.Visible)
aForm.Activate();
else
aForm.Show();

// This works nice once, and the form gets the focus once it's already
opened, but if it's closed *once* it doesn't open again.
Nov 17 '05 #1
7 3106
Make the Form constructor private. Then create a static (shared) method
on the form called GetInstance() which returns an internal static
(shared) variable holding an instance of the form

Nov 17 '05 #2
VM,

I would have a factory method/property that would handle this for you,
something like this:

// Stores the instance of the current form.
private static Form form;

public static Form GetForm()
{
// If the form is null, create it here.
if (form == null)
{
// Create it here.
form = new Form();

// Handle the closed event, when closed, set the form to null.
// This syntax is for .NET 2.0. You can just create a delegate and
add that
// to the form for previous versions.
form.Closed += delegate
{
// Set form to null.
form = null;
};
}

// Return the form.
return form;
}

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

// Declare constructor private. This probably will give you designer
problems though, so
// you don't have to do this.
"VM" <vi***********@gmail.com> wrote in message
news:uA**************@TK2MSFTNGP14.phx.gbl...
Hi,

Is it possible to open always only one instance of a MDIChild form?
Say, if form A is already opened, just give it the focus.
I tried declaring theMDIChild forms as private members of the MDI form
(using the MDI menu's script), but when the MDIChild form is closed (x
button pushed), I cannot open again the MDIChild form.

Example:

// aForm is a private member of the MDI form.

if (aForm.Visible)
aForm.Activate();
else
aForm.Show();

// This works nice once, and the form gets the focus once it's already
opened, but if it's closed *once* it doesn't open again.

Nov 17 '05 #3
I believe that setting the form constructor to prviate will mess up the
designer support, so that's probably not a good idea.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<MD******@gmail.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
Make the Form constructor private. Then create a static (shared) method
on the form called GetInstance() which returns an internal static
(shared) variable holding an instance of the form

Nov 17 '05 #4
Apparently, I was mistaken. It works just fine.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:uT**************@TK2MSFTNGP09.phx.gbl...
I believe that setting the form constructor to prviate will mess up the
designer support, so that's probably not a good idea.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<MD******@gmail.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
Make the Form constructor private. Then create a static (shared) method
on the form called GetInstance() which returns an internal static
(shared) variable holding an instance of the form


Nov 17 '05 #5
did you try it? Probably not since it does work. About the factory
method, that works but that does ensure that "always only one instance"
of the form exists. Some developer could join the team and start
instaintiating the form all over the place and never use your factory.
I am not a fan of singletons but if you need to make sure that no other
instances of the form exists then you need to change the constructor's
access modifier.

Nov 17 '05 #6
VM
Thx Matt!
<MD******@gmail.com> escribió en el mensaje
news:11**********************@g44g2000cwa.googlegr oups.com...
Make the Form constructor private. Then create a static (shared) method
on the form called GetInstance() which returns an internal static
(shared) variable holding an instance of the form

Nov 17 '05 #7
VM
Thx Nicholas, I like Matt's approach. It works.

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> escribió
en el mensaje news:uT**************@TK2MSFTNGP09.phx.gbl...
I believe that setting the form constructor to prviate will mess up the
designer support, so that's probably not a good idea.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<MD******@gmail.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
Make the Form constructor private. Then create a static (shared) method
on the form called GetInstance() which returns an internal static
(shared) variable holding an instance of the form


Nov 17 '05 #8

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

Similar topics

5
by: Charles A. Lackman | last post by:
I am working on a application that has many custom made dlls. Actually, for this app each dll is a different form. I am able, with no problem to reference and instantiate them to use their public...
0
by: Supra | last post by:
i got mdichild working. i'm working on similar to mirc chat. when i joined channel #vb the first mdichild is on. then when i wanted to join another channel #visualbasic. then i got second mdichild...
1
by: Job Lot | last post by:
How can I prevent multiple instances of MdiChild form? I have a MdiParent form with a DataGrid showing all the clients in the database. User clicks on a row to open MdiChild form which display...
1
by: Mickey Swanson | last post by:
I have a MDI app wich has several different forms I need to call a method on one MDIchild form from another child form. I have a MDI form called frmMDI. I have a mdichild form called frmMain....
2
by: SpotNet | last post by:
Hello NewsGroup, I have a MDIParent form with a docked left treeview control, a splitter control and a generic docked control on the right. Kind of looks like Windows Explorer, though it's...
0
by: Supra | last post by:
i got mdichild working. i'm working on similar to mirc chat. when i joined channel #vb the first mdichild is on. then when i wanted to join another channel #visualbasic. then i got second mdichild...
3
by: VB4DUMMYPOSTERCHILD | last post by:
Hi, this is my first time posting a question so please bear with me. I am a novice at VB2005 and looking for help utilizing a variable to call open a database. This will allow me to chose which...
0
by: kardiss | last post by:
I am trying to add Meebo's "Pop-out" kinda feature to my application which when a button is pressed in a MdiChild form, it kicks that MdiChild form out of the MdiParent boundary and acts like a SDI...
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...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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,...
0
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...
0
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...

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.