473,544 Members | 1,778 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Instance name of main form

Jon
My main form opens up another form, and from this other form, I'd like to access things in the main
form. The problem is that although I know the name of the class of the main form (FormMain) I don't
know the name of the instance of it since it was generated by the VS C# Express 2005 designer. In
program.cs, I notice that there is the line:
Application.Run (new FormMain());
I guess I could replace this with:
FormMain fMain = new FormMain()
Application.Run (fMain);
although since it was generated by VS, I'm a bit hesitant of any knock-on effects.


Aug 22 '07 #1
3 3919
1. you can manage all of this by singletone controller object.
2. Application.Ope nForms from the CLR, Read the remarks :

//
// Summary:
// Gets a collection of open forms owned by the application.
//
// Returns:
// A System.Windows. Forms.FormColle ction containing all the
currently open forms
// owned by this application.

Hope it will help you.

Sincerely
Yaron Karni
http://dotnetbible.blogspot.com/
"Jon" wrote:
My main form opens up another form, and from this other form, I'd like to access things in the main
form. The problem is that although I know the name of the class of the main form (FormMain) I don't
know the name of the instance of it since it was generated by the VS C# Express 2005 designer. In
program.cs, I notice that there is the line:
Application.Run (new FormMain());
I guess I could replace this with:
FormMain fMain = new FormMain()
Application.Run (fMain);
although since it was generated by VS, I'm a bit hesitant of any knock-on effects.


Aug 22 '07 #2
Jon wrote:
My main form opens up another form, and from this other form, I'd like to access things in the main
form. The problem is that although I know the name of the class of the main form (FormMain) I don't
know the name of the instance of it since it was generated by the VS C# Express 2005 designer. In
program.cs, I notice that there is the line:
Application.Run (new FormMain());
I guess I could replace this with:
FormMain fMain = new FormMain()
Application.Run (fMain);
although since it was generated by VS, I'm a bit hesitant of any knock-on effects.
What you propose is fine.

That, and the rest of this post, assumes you want a specific,
easy-to-access, direct way to get at your form instance. In many cases,
you may find that the Application.Ope nForms property is sufficient. You
would have to get the collection and then enumerate that list looking
for the form you want. It's not quite as convenient to _use_ as the
above techniques, but it certainly would be easier to implement
initially, since all of that work has already been done for you (in
other words, the "initial implementation" is zero effort on your part :) ).

I don't think making a singleton as suggested by the other reply is
strictly necessary, but you might prefer it. Even creating a singleton
class wrapper for your form though, you will need to modify the Program
class as you've suggested, since the call to the Application.Run ()
method would need to use the singleton class to retrieve the form instance.

An alternative that wouldn't require changing the Program class (not
that I think there's anything wrong with doing so) would be to create a
singleton-like design in your FormMain class itself. It wouldn't be a
true singleton, because you'd have to provide a public constructor that
the Program class can use. But you could still provide a static
accessor like a singleton has, and even throw an exception in the
constructor if someone tries to instantiate more than one, to enforce
the singleton nature:

class MainForm : Form
{
static MainForm _instance;

static public MainForm Instance
{
get { return _instance; }
}

public MainForm()
{
if (_instance != null)
{
throw new NotSupportedExc eption("Only one instance of
MainForm allowed");
}

_instance = this;
}
}

And if you don't really want the form to be a singleton, for whatever
reason, you could instead change the above as follows:

class MainForm : Form
{
static List<MainForm_i nstances = new List<MainForm>( );

static public MainForm[] Instances
{
get { return _instances.ToAr ray(); }
}

public MainForm()
{
_instances.Add( this);
}

protected override void Dispose(bool fDisposing)
{
if (fDisposing)
{
_instances.Remo ve(this);
}

base.Dispose(fD isposing);
}
}

This lets you get a complete list of the instances. A couple of notes
on the above:

1) I think the Dispose() method is correct, but don't take my word
for it. :) I don't have to write Dispose() methods often enough for me
to be sure that's an appropriate way to handle the end of the object's
lifetime.

2) Using the above, it would be critical that you do properly
dispose of unused forms. In particular, if you used the design with
forms that are used as dialogs (they are shown with ShowDialog()), the
form will never be disposed and release unless you do so explicitly,
because the instance list retains a reference to it.

You should be writing correct code that disposes dialog forms anyway, so
#2 shouldn't be an issue. It's just that the design short-cuts the
finalizer fail-safe built into C#, because the form instance will always
be reachable and thus not eligible for garbage collection and finalizing.

Of course, depending on how you use it, that could be a good thing. You
could easily build on top of the above pattern a dialog form cache that
retrieves already-instantiated instances of your dialog forms. One
reason you might do that is if you want previous user entries in the
dialog form to be retained, but don't want to have to write the code to
do that explicitly.

Anyway, have I sufficiently confused the matter yet? :)

Pete
Aug 22 '07 #3
Jon
Thanks to everyone who replied to my question, and especially to Peter for the detailed options. I
think I will use the method I suggested, and see how it goes.

Jon

"Peter Duniho" <Np*********@Nn OwSlPiAnMk.comw rote in message
news:13******** *****@corp.supe rnews.com...
Jon wrote:
My main form opens up another form, and from this other form, I'd like to access things in the
main
form. The problem is that although I know the name of the class of the main form (FormMain) I
don't
know the name of the instance of it since it was generated by the VS C# Express 2005 designer. In
program.cs, I notice that there is the line:
Application.Run (new FormMain());
I guess I could replace this with:
FormMain fMain = new FormMain()
Application.Run (fMain);
although since it was generated by VS, I'm a bit hesitant of any knock-on effects.
What you propose is fine.

That, and the rest of this post, assumes you want a specific,
easy-to-access, direct way to get at your form instance. In many cases,
you may find that the Application.Ope nForms property is sufficient. You
would have to get the collection and then enumerate that list looking
for the form you want. It's not quite as convenient to _use_ as the
above techniques, but it certainly would be easier to implement
initially, since all of that work has already been done for you (in
other words, the "initial implementation" is zero effort on your part :) ).

I don't think making a singleton as suggested by the other reply is
strictly necessary, but you might prefer it. Even creating a singleton
class wrapper for your form though, you will need to modify the Program
class as you've suggested, since the call to the Application.Run ()
method would need to use the singleton class to retrieve the form instance.

An alternative that wouldn't require changing the Program class (not
that I think there's anything wrong with doing so) would be to create a
singleton-like design in your FormMain class itself. It wouldn't be a
true singleton, because you'd have to provide a public constructor that
the Program class can use. But you could still provide a static
accessor like a singleton has, and even throw an exception in the
constructor if someone tries to instantiate more than one, to enforce
the singleton nature:

class MainForm : Form
{
static MainForm _instance;

static public MainForm Instance
{
get { return _instance; }
}

public MainForm()
{
if (_instance != null)
{
throw new NotSupportedExc eption("Only one instance of
MainForm allowed");
}

_instance = this;
}
}

And if you don't really want the form to be a singleton, for whatever
reason, you could instead change the above as follows:

class MainForm : Form
{
static List<MainForm_i nstances = new List<MainForm>( );

static public MainForm[] Instances
{
get { return _instances.ToAr ray(); }
}

public MainForm()
{
_instances.Add( this);
}

protected override void Dispose(bool fDisposing)
{
if (fDisposing)
{
_instances.Remo ve(this);
}

base.Dispose(fD isposing);
}
}

This lets you get a complete list of the instances. A couple of notes
on the above:

1) I think the Dispose() method is correct, but don't take my word
for it. :) I don't have to write Dispose() methods often enough for me
to be sure that's an appropriate way to handle the end of the object's
lifetime.

2) Using the above, it would be critical that you do properly
dispose of unused forms. In particular, if you used the design with
forms that are used as dialogs (they are shown with ShowDialog()), the
form will never be disposed and release unless you do so explicitly,
because the instance list retains a reference to it.

You should be writing correct code that disposes dialog forms anyway, so
#2 shouldn't be an issue. It's just that the design short-cuts the
finalizer fail-safe built into C#, because the form instance will always
be reachable and thus not eligible for garbage collection and finalizing.

Of course, depending on how you use it, that could be a good thing. You
could easily build on top of the above pattern a dialog form cache that
retrieves already-instantiated instances of your dialog forms. One
reason you might do that is if you want previous user entries in the
dialog form to be retained, but don't want to have to write the code to
do that explicitly.

Anyway, have I sufficiently confused the matter yet? :)

Pete
Aug 23 '07 #4

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

Similar topics

3
1698
by: Giulio Mastrosanti | last post by:
Which is the instance name of the starting form of an application? I want to access his controls and variables from another form but can't find the way... Which is the property I can use and must i cast it in some way? Thanx, Giulio
3
2079
by: serge calderara | last post by:
Dear all, I have a vb.net application which start with a sub main procedure. inside this sub main procedure I create a and instance from an assembly x like as follow: sub main() ..... Application.Run(New frmLogin) end sub
6
4640
by: Dmitry Karneyev | last post by:
Hi! I guess this question have been asked a lot of times, but please be tolerant and if you have any ideas share it. The question is: how to make availibale only one instance of application and if second one is loaded it must be close and give focus to first one. Thanks for any advice! Dmitry
2
12001
by: Bill D | last post by:
In a simple Windows forms application, the main form is called Form1. Within this form's class, I refer to the the running instance of this main class as "this" as in this.Text = "NEW TEXT" I want to do something like change this Text on the Form1 window from within another class. Trying Form1.Text = "TEXT FROM CLASS" or...
18
5640
by: Steve Barnett | last post by:
I want to ensure that there is only ever one instance of my app running on a single PC at any time. I understand that I can achieve this by using a mutex and, if I can't take ownership of the mutex, I know there is another instance of my program running. Problem is, how do I get the "new" instance to communicate with the "old" instance? The...
5
1996
by: scole954387 | last post by:
Hi, I'm creating a MDI application and running into the common problem of more than one instance of the child form. I've read several articles and postings on the Singleton approach, but I can't get it to work. What I have: One form with linklabels and another form with a webbrowser. When the linklabel is clicked the webbrowser...
3
7185
by: Mark Jerde | last post by:
VS 2005. When I google "CSharp single instance form" the returned pages usually use a Mutex or the VB.NET runtime library. http://en.csharp-online.net/Application_Architecture_in_Windows_Forms_2.0%E2%80%94Single-Instance_Detection_and_Management http://www.codeproject.com/csharp/SingleInstanceApplication.asp...
20
10602
by: Boki Digtal | last post by:
Hi All, How to limit the c# program can only run one instance at the same time ? Thank you! Best regards, Boki.
3
1623
by: pOwner | last post by:
I'm trying to create a pointer to a class in form1.h button1 event handler. It should be simple... but no. This example is as simple as I can make to illustrate the problem: Form1.h (produced with the designer containing a single button) *********************************************************************************** #pragma once ...
0
7414
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7359
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...
0
7757
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7360
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...
0
7699
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5288
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...
0
3398
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1833
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
1
982
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.