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

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 3900
1. you can manage all of this by singletone controller object.
2. Application.OpenForms from the CLR, Read the remarks :

//
// Summary:
// Gets a collection of open forms owned by the application.
//
// Returns:
// A System.Windows.Forms.FormCollection 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.OpenForms 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 NotSupportedException("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_instances = new List<MainForm>();

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

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

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

base.Dispose(fDisposing);
}
}

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*********@NnOwSlPiAnMk.comwrote in message
news:13*************@corp.supernews.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.OpenForms 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 NotSupportedException("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_instances = new List<MainForm>();

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

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

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

base.Dispose(fDisposing);
}
}

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
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...
3
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() ..... ...
6
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...
2
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...
18
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...
5
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...
3
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....
20
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
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.