.NET C#/VB.NET How to access Form Controls from Module
This may sound like stupid question.
How do I do something like this.
Module Misc
Public Sub WriteLog(ByVal txt As String)
Form1.TextBox3.Text += txt + vbNewLine
End Sub
End Module
This is inside the Module separate from Form1 class as you can see right off the bat it's not a class and there is no reference to Form1.. how do I access Form1 without passing reference to make factory modules work like this. This is to be used fo开发者_如何学Pythonr purposes like threading functions and helper function all around the program.
You cannot directly access a class without a reference to an instance of that class. Your best bet might be something like this:
Module Module1
Private m_MyForm As Form1
Public ReadOnly Property MyForm() As Form1
Get
If IsNothing(m_MyForm) Then m_MyForm = New Form1
Return m_MyForm
End Get
End Property
Public Sub WriteLog(ByVal txt As String)
MyForm.TextBox3.Text += txt + vbNewLine
End Sub
End Module
Now anywhere in your application you can access Form1 using Module1.MyForm.
Note: This limits you to having only one instance of Form1 which will persist until the application exits.
精彩评论