This is how you can access a form from a module

Eloff

Freshman
Joined
Mar 20, 2003
Messages
35
Location
Canada
The Form1.ActiveForm thing works fairly well, but if you minimize the form while it is trying to do something with it, Form1.ActiveForm isn't set and an exception occurs. A better way may be:

Put this in the module:

Code:
Public MyForm As System.Windows.Forms.Form

Public Sub init(ByRef thisForm As System.Windows.Forms.Form)
MyForm = thisForm
End Sub

And call init() from the windows form designer generated code:

Code:
Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call
init(Me)
End Sub

Put that in all your forms and MyForm will always reference the last constructed form (the active form!).

I'm pretty new to this so I'm not sure if this is a good or a bad idea, but it seems to work great for me!

Dan
 
If you use as windows.forms.form you will not ba able to access functions or variables that are inside the form class.
 
Eloff, is this in response to something?

Just something I wanted to share.

If you use as windows.forms.form you will not ba able to access functions or variables that are inside the form class.

I've never tried to do that so it doesn't bother me, I just needed a way to access the controls on the form, add new ones or manipulate existing ones.
 
Declaring a public variable that contains the object and reference to the form will only consume resources and memory, and what will happen when you get more than one form on your project opened at the same time??? you'll need to declare multiple variables each one???.
I'll rather declare it when I need it and dispose it when the form is no longer needed, if you use this you'll never free the memory used.

Regards
 
Back
Top