locked
Can't read a textbox on frmMain from a module. RRS feed

  • Question

  • If trying to read the contents of a textbox on frmMain for use in a Module:

    If My.Computer.FileSystem.DirectoryExists(frmMain.txtPrefix.Text & strSrcFldName) Then...

    The line does not produce an error at design time nor crash the program when run, but when executed, the result is always False and when I put a Breakpoint & watch on "frmMain.txtPrefix.Text", VS reports the value as:

    error BC30469: Reference to a non-shared member requires an object reference.

    Clearly, I've referenced the objects. The code did not cause an error when it was on the original form (I'm moving it to a Public Sub in a Module so it can be reused.) I'm not sure what to do to fix this. I remember long ago being able to reference the contents of remote forms this way.

    TIA

    Tuesday, January 8, 2019 6:30 PM

Answers

  • Hey! After some online searching, I found the answer buried in another post Here's the solution:

    Add "ByRef frm As frmMain" to the Sub in your module (rename "frmMain" as necessary.)

    When calling your subs, simply pass "Me" (your form) to the procedure.

    It should look like this:

    ' Inside your module:
    
    Public Sub MyProcedure(ByRef frm As frmMain)
        If My.Computer.FileSystem.DirectoryExists(frm.txtPrefix.Text & strSrcFldName) Then
            ...
        End If
    End Sub
    
    
    ' Then in your form, call the procedure with:
    Call MyProcedure(Me)

    .
    This basically passes the entire form (Me) to your procedure and stored in "frm".

    Hope this helps someone.

    Tuesday, January 8, 2019 6:55 PM

All replies

  • Hey! After some online searching, I found the answer buried in another post Here's the solution:

    Add "ByRef frm As frmMain" to the Sub in your module (rename "frmMain" as necessary.)

    When calling your subs, simply pass "Me" (your form) to the procedure.

    It should look like this:

    ' Inside your module:
    
    Public Sub MyProcedure(ByRef frm As frmMain)
        If My.Computer.FileSystem.DirectoryExists(frm.txtPrefix.Text & strSrcFldName) Then
            ...
        End If
    End Sub
    
    
    ' Then in your form, call the procedure with:
    Call MyProcedure(Me)

    .
    This basically passes the entire form (Me) to your procedure and stored in "frm".

    Hope this helps someone.

    Tuesday, January 8, 2019 6:55 PM
  • Hi

    Well done on your research. Good way to learn.

    Maybe passing the whole Form is a bit of overkill though.

    Here is an example which just uses the contents of 2 TextBoxes on main Form and passes those to the Module code.

    In the Module, I used a Function so that the result can be passes back to the main Form and displayed in a Label.

    The Button calls the Module code.

    Image (Directory Exists)

    Main Form Code

    Public Class Form1
      Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Label1.Text = MyProcedure(TextBox1.Text, TextBox2.Text).ToString
      End Sub
    End Class

    Module Code

    Module Module1
      Public Function MyProcedure(a As String, b As String) As Boolean
        If My.Computer.FileSystem.DirectoryExists(IO.Path.Combine(a, b)) Then
          Return True
        End If
        Return False
      End Function
    End Module
    


    Regards Les, Livingston, Scotland

    Tuesday, January 8, 2019 7:32 PM
  • Hi

    Well done on your research. Good way to learn.

    Maybe passing the whole Form is a bit of overkill though.

    .
    Thanks for the follow-up. I definitely could have used your format, though I actually access many more objects in my module than just the one, so rather than pass a half dozen individually, it was easier just to pass the entire form (looks cleaner too.) ;)
    Tuesday, January 8, 2019 8:09 PM