Results 1 to 10 of 10

Thread: How can I access a form from a module?

  1. #1

    Thread Starter
    Member
    Join Date
    Nov 2002
    Location
    Netherlands
    Posts
    32

    Question 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...

  2. #2
    Registered User
    Join Date
    Nov 2002
    Location
    Växjö, Sweden
    Posts
    314
    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)

  3. #3

    Thread Starter
    Member
    Join Date
    Nov 2002
    Location
    Netherlands
    Posts
    32
    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.

  4. #4
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    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/

  5. #5

    Thread Starter
    Member
    Join Date
    Nov 2002
    Location
    Netherlands
    Posts
    32
    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.

  6. #6
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    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)
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  7. #7

    Thread Starter
    Member
    Join Date
    Nov 2002
    Location
    Netherlands
    Posts
    32
    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.

    ???

  8. #8
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    Dim balls As New frmMain

    balls.Show()
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  9. #9
    Registered User
    Join Date
    Nov 2002
    Location
    Växjö, Sweden
    Posts
    314
    Dim frm As New frmMain
    Application.Run(frm)

  10. #10

    Thread Starter
    Member
    Join Date
    Nov 2002
    Location
    Netherlands
    Posts
    32
    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
  •  



Click Here to Expand Forum to Full Width