Visual Basic Development Bookmark and Share   
 Home > Visual Basic General > Public Module Updating Form Object Problem
 

Public Module Updating Form Object Problem

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
ManagerJohn  11 hours 53 minutes ago
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.
Andrew B. Painter  9 hours 51 minutes ago
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.
Acamar  10 hours 5 minutes ago
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.
Andrew B. Painter  9 hours 51 minutes ago

You can use google to search for other answers

Custom Search

More Threads

• Boolean shortcuts allowed?
• How to control mutilple displays from VB.NET
• Setting a vb.net form to behave as child window of an existing application.
• help in vb2008 express edition
• how do I simply cast from Double to Decimal?
• An unhandled exception of type 'System.StackOverflowException' occurred in mscorlib.dll?
• FindInFiles cause an System.UnauthorizedAccessException was unhandled
• WYSIWYG with RichTextBox
• Referencing field in table of Access Database in Visual Basic Express
• Embedding bitmap in XML