|
-
Dec 4th, 2002, 05:24 AM
#1
Thread Starter
Member
How can I access a form from a module?
Hey people,
Can anyone please tell me how to access a form from a module. For example, I have a project with a form called frmMain which contains a label called lblName. I also added a module called modMain.
Now I would like to access the label lblName in the modMain to change its Text property with this code:
Code:
frmMain.lblName.Text = "Enter your name:"
This is the way I used to do with VB6. But now I have VB.NET it won't work! The frmMain object is Nothing.
Please help...
-
Dec 4th, 2002, 05:55 AM
#2
Registered User
You have to get a reference to the form or the object somehow. One way is to pass the form or label to the function that is supposed to alter the properties..... example....
Code:
Module Test
Sub changelabel(ByVal lbl As Label)
lbl.Text = "Snabel"
End Sub
End Module
and of course call it like this...
Code:
changelabel(me.lblName)
-
Dec 4th, 2002, 06:31 AM
#3
Thread Starter
Member
OK, but how can I create a reference?
I usually have a module with Sub Main in my apps which initializes stuff like the language and sets label's and menu's text properties to that language.
At the end of the module it calls the app's main form's frmMain.Show() function.
But I don't know how to do this with vb.NET.
-
Dec 4th, 2002, 08:59 AM
#4
yay gay
u must put a sub_main and in that module put all the vars for the forms that will be used..then just reference them all over the project
\m/  \m/
-
Dec 4th, 2002, 09:06 AM
#5
Thread Starter
Member
OK, thanx. I will try that.
So there is no way I can call functions and access properties of an existing form from a module in the same project?
I think that's a bad point of VB.NET.
-
Dec 4th, 2002, 09:42 AM
#6
its not a 'bad point'. It was a bad point of VB6 actually to allow you to do that. You can create a reference to your form by passing it as a parameter to the routines in the module that need it which would be the proper way to do it.
like this
here is your subroutine
Public balls(myform As yourformname)
myform.Somethingfromtheform
End Sub
then call it on your form
balls(Me)
-
Dec 4th, 2002, 10:16 AM
#7
Thread Starter
Member
OK, that's good, but it's still not what I mean. Look:
Since I create applications with VB6 they start with the procedure Sub Main() in a module (modMain).
In this Main() subroutine I load the main form (frmMain) without showing it and I load (for example) a language from a file and update all visible text to that language.
My original VB6 Sub Main() looks some like this:
Code:
Option Explicit
Public strRes(24) As String
Public Sub Main()
Dim i As Integer
Load frmMain
'Just a function to load some text from a file into an array
Call GetLanguage(strRes())
With frmMain
For i = 0 to UBound(strRes)
.labels(i).Caption = strRes(i)
Next i
End With
frmMain.Show()
End Sub
But how should I do this with VB.NET???
It throws a System.NullReferenceException which says: Object reference not set to an instance of an object.
I tried to declare the form in the module:
Code:
Private frm as frmMain
but this doesn't work either.
???
-
Dec 4th, 2002, 11:07 AM
#8
Dim balls As New frmMain
balls.Show()
-
Dec 4th, 2002, 04:52 PM
#9
Registered User
Dim frm As New frmMain
Application.Run(frm)
-
Dec 5th, 2002, 03:19 AM
#10
Thread Starter
Member
Thanx, I didn't know it was that simple...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|