How to get a reference to main form in a Windows Form 2.0 Application?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi

How to get a reference to main form in a Windows Form 2.0 Application?

I'm making a a library and I need a reference to the main form of the
application that is using that library.


TIA

JL
--
Lic. Jorge Luis De Armas García
Chief Software Architect
DATYS Tecnología y Sistemas
email: (e-mail address removed)
(e-mail address removed)
(e-mail address removed)
(e-mail address removed)
 
How to get a reference to main form in a Windows Form 2.0 Application?

I'm making a a library and I need a reference to the main form of the
application that is using that library.

Have them supply it to you?

-- Alan
 
Hi
Have them supply it to you?

It could be, but I'm thinking like in Delphi (VCL) that we have property
MainForm in Application object.

Is there something like that?

TIA

JL

--
Lic. Jorge Luis De Armas García
Chief Software Architect
DATYS Tecnología y Sistemas
email: (e-mail address removed)
(e-mail address removed)
(e-mail address removed)
(e-mail address removed)
 
How to get a reference to main form in a Windows Form 2.0 Application?

I'm making a a library and I need a reference to the main form of the
application that is using that library.

I just spent a few minutes trying to answer this - and it may not be
possible.

* The Application class has nothing like a MainForm property.

* The Application.Run(Form) method doesn't log its parameter anywhere
public; it adds an event handler to the MainForm's Closed event.
There's no way (that I know of) to examine a form's Closed event
handlers to see which has a handler in the Application class; you can
only treat the event as a delegate within the Form class itself.

* The Application.OpenForms collection seems to be in form creation
order. In most cases, the first form will be the main form - but you
can't count on this. If the Main() procedure creates (and shows) a
form before calling Application.Run, the main form will not be first
in the OpenForms collection.
 
It could be, but I'm thinking like in Delphi (VCL) that we have property
MainForm in Application object.

Is there something like that?

There may be something like that if you're using VB.NET, I'm not sure. If
it were me, I would just have them supply the mainform reference in the
constructor, init routine, or property or whatever. It's very trivial.

-- Alan
 
Jon said:
I just spent a few minutes trying to answer this - and it may not be
possible.

I spoke too soon - forgot to Google!

Form MainForm =
(Form)Control.FromHandle(Process.GetCurrentProcess().MainWindowHandle);
 
Jon Shemitz said:
Jon Shemitz wrote:
Form MainForm =
(Form)Control.FromHandle(Process.GetCurrentProcess().MainWindowHandle);

But what control would a library (dll) have access to? It is separate from
the exe.

-- Alan
 
Alan said:
But what control would a library (dll) have access to? It is separate from
the exe.

So? It's running in the same process, and using the same
System.Windows.Forms code. In fact, this library

using System;
using System.Diagnostics;
using System.Windows.Forms;

namespace Get
{
public static class Main
{
public static Form Form
{
get { return
(Form)Control.FromHandle(Process.GetCurrentProcess().MainWindowHandle);
}
}
}
}

works just as well as code in a form event handler ....
 
Jon

I had been expecting something like Delphi has in VCL but this is valid too.
Thanks

JL

--
Lic. Jorge Luis De Armas García
Chief Software Architect
DATYS Tecnología y Sistemas
email: (e-mail address removed)
(e-mail address removed)
(e-mail address removed)
(e-mail address removed)
 
I had been expecting something like Delphi has in VCL but this is valid too.

Yeah, the overall architecture of WinForms is a lot like VCL, but
there are a lot of differences in the details.
 
Back
Top