locked
Public Module Updating Form Object Problem RRS feed

  • Question

  • All;

    I have a simple form with one button and one label.  Inside my form I have the following:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Label1.Text = "Button1"
    End Sub

    Work just fine as one would expect.

    I then add a public module to the application as:

    Module FillIt

        Public Sub clickedit()
            Form1.Label1.Text = "Module Clicked"
        End Sub

    End Module

    I change the button_click on form1 to say:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            FillIt.clickedit()
    End Sub

    Again, it works correctly.

    Now, I add a second module called start as below:

    Module start

        Public form1 As New Form1

        Public Sub main()
            Form1.ShowDialog()
        End Sub

    End Module


    I change the project to have its startup object be start.  when I run the app and click the button, nothing happens.  A breakpoint shows that the module is accessed and the line of code to change the label's text is executed, but the form1 label's text does not change.  It is as if I cannot "push" the change from the module to the form.

    Any thoughts as to what I am missing??

    Thanks.

    John Ferry
    Tuesday, December 1, 2009 7:31 PM

Answers

  • You really shouldn't be modifying any instanced object from a Module, either, even where the Instance is declared in the Module.

    So as Acamar says, first make this change:

    Module start
        Public MyForm1 As New Form1
        Public Sub main()
            MyForm1.ShowDialog()
        End Sub
    End Module

    Then make this change:

    Public Module FillIt
       Public Function ClickedIt() As System.String
          Return "Module Clicked"
       End Function
    End Module

    Then in your Form1 Class you should:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Me.Label1.Text = FillIt.clickedit()
    End Sub


    Alternatively:

    Public Module FillIt
       Public Sub ClickedIt(ByRef targetlabelinstance as System.Windows.Forms.Label)
          targetlabelinstance.Text = "Module Clicked"
       End Function
    End Module

    Along with:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            FillIt.clickedit(Me.Label1)
    End Sub



    It never hurts to try. In a worst case scenario, you'll learn from it.
    • Marked as answer by ManagerJohn Wednesday, December 2, 2009 2:57 AM
    Tuesday, December 1, 2009 9:33 PM

All replies

  • You should not use the class name as a name for an instance of the class.  Change Form1 to myForm1 in the start module and in the ClickEdit sub and the code will work properly.
    Tuesday, December 1, 2009 9:19 PM
  • You really shouldn't be modifying any instanced object from a Module, either, even where the Instance is declared in the Module.

    So as Acamar says, first make this change:

    Module start
        Public MyForm1 As New Form1
        Public Sub main()
            MyForm1.ShowDialog()
        End Sub
    End Module

    Then make this change:

    Public Module FillIt
       Public Function ClickedIt() As System.String
          Return "Module Clicked"
       End Function
    End Module

    Then in your Form1 Class you should:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Me.Label1.Text = FillIt.clickedit()
    End Sub


    Alternatively:

    Public Module FillIt
       Public Sub ClickedIt(ByRef targetlabelinstance as System.Windows.Forms.Label)
          targetlabelinstance.Text = "Module Clicked"
       End Function
    End Module

    Along with:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            FillIt.clickedit(Me.Label1)
    End Sub



    It never hurts to try. In a worst case scenario, you'll learn from it.
    • Marked as answer by ManagerJohn Wednesday, December 2, 2009 2:57 AM
    Tuesday, December 1, 2009 9:33 PM