How do we access form controls from a module file ?

dominico

Well-known member
Joined
Mar 9, 2005
Messages
57
Programming Experience
3-5
Hi,

Someone please tell me how to access a window form from inside a module file ?

I tried "inherits mywindowform" and also "imports mywindowsform" but none works.

In VB6, all I had to do was using the name of the form and that was it. VB.Net is driving me nuts.

Please advise,

dominico
 
Last edited:
A module does not and can not inherit from anything. You don't inherit from a class to access members of particular instances of the class anyway. You would need a variable of the correct type in the module, and then the form would set that variable to refer to itself, e.g.:
In module:
VB.NET:
Public myGlobalFormVar As MyWindowsForm
In form class:
VB.NET:
myGlobalFormVar = Me
That way, the module can then access the form via the myGlobalFormVar variable. In ALL situations, to do anything to or with an object, you must have an in-scope variable that refers to that object. If you don't have an in-scope variable, you can't "see" the object so you have no way to access it.
 
jmcilhinney said:
A module does not and can not inherit from anything. You don't inherit from a class to access members of particular instances of the class anyway. You would need a variable of the correct type in the module, and then the form would set that variable to refer to itself, e.g.:
In module:
VB.NET:
Public myGlobalFormVar As MyWindowsForm
In form class:
VB.NET:
myGlobalFormVar = Me
That way, the module can then access the form via the myGlobalFormVar variable. In ALL situations, to do anything to or with an object, you must have an in-scope variable that refers to that object. If you don't have an in-scope variable, you can't "see" the object so you have no way to access it.

Hi jmcilhinney buddy, :)

In my window form class, I have to declare as below in order for it to recognize it, right ?

Dim fModifier As Form = Me

thanks.
 
No. As I said, the variable must be declared in the module, because it is from the module that you want the access. The form can already access itself so it doesn't need another reference. Also, the variable should be the type of the actual class, not just of type Form. If your form is defined like this:
VB.NET:
Public Class MyWindowsForm
	Inherits System.Windows.Forms.Form
then your variable should be of type MyWindowsForm, not Form. Otherwise, you would just have to cast it as the correct type to access the members of the derived class anyway.
 
jmcilhinney said:
I believe that the equivalent property in .NET is Handle. Are you sure that that component supports .NET apps?

Thanks alot. It is working now. I thought you wanted me to add that second line to the form class. Sorry.

I don't understand the above question. Do you use yahoo messenger ? I want to chat with you.

dominico
 
From post #2, the first line goes in the module and the second line goes in the form class.

Post #5 was a mistake. Sorry. I had several threads open in several windows and I typed a reply in this thread instead of another. I'd closed the window before I realised so I didn't know which thread I'd posted to.

I'm afraid I don't use IM. Don't get enough work done already without another distraction. :)
 
jmcilhinney said:
From post #2, the first line goes in the module and the second line goes in the form class.

Post #5 was a mistake. Sorry. I had several threads open in several windows and I typed a reply in this thread instead of another. I'd closed the window before I realised so I didn't know which thread I'd posted to.

I'm afraid I don't use IM. Don't get enough work done already without another distraction. :)

Oh okay. no problem :)

jmcilhinney,

How do add the second line ? "myModifier = me" gives me error. How should I declare it ?

When run my app., I got an error below:

An unhandled exception of type 'System.NullReferenceException' occurred in NewGen.exe
Additional information: Object reference not set to an instance of an object.


From the line below:

Module modScripts



Public sIntro As String = Nothing

Public myModifier As frmModifier


Function GetScriptIntro()

sIntro = myModifier.txDealerid.Text <----------------------------------

end function

end module
 
If you simply declare a variable like you have done in your module then it doesn't refer to anything, hence the NullReferenceException. To be able to use the variable it must refer to a specific object. By the form assigning "Me" to the variable, it then refers to that particular object and you can use it. Therefore you must assign the form object to the variable before using the variable. The logical place to do that is either in the Load event handler of the form or in its constructor. Unless you have a particular reason for using the constructor, I'd recommend the Load event handler.
 
jmcilhinney said:
If you simply declare a variable like you have done in your module then it doesn't refer to anything, hence the NullReferenceException. To be able to use the variable it must refer to a specific object. By the form assigning "Me" to the variable, it then refers to that particular object and you can use it. Therefore you must assign the form object to the variable before using the variable. The logical place to do that is either in the Load event handler of the form or in its constructor. Unless you have a particular reason for using the constructor, I'd recommend the Load event handler.


We got it! :)

I added it first to the declaration section that's why it complained. It is now in the load proc.

You are the BEST as always. Thanks again.

dominico
 
Ok, so I made all that work, why can I not access my datagrid? Every other control shows up, but I can't seem to get my datagrid. Is there a property I have set on my datagrid wrong? Thanks!
 
Back
Top