reference a vb.net windows form control from a module sub

  • Thread starter Thread starter Mark D
  • Start date Start date
M

Mark D

Hi

Relative vb.net newbie here... I have a Windows Form
application with a few subroutines in a separate module.
From one of the subroutines, I want to get the value of a
label or text box on the form. As it is, I get
a "Reference to a non-shared member requires an object
reference." error message. I can't seem to figure out how
to tell the subroutine how to find and use the labels or
text boxes on the form.

I'm sure there's an simple command I need to include,
just can't seem to find it, can someone help?

Mark D
LandAmerica Data Center - Dallas
 
You have to create an instance of the form. In VB6, if you defined a form
in the IDE and called it "frmOne", you could access it from a module using
"frmOne.TextBox1.Text" etc. In .NET, you should create an instance of the
form - Dim mvForm As New frmOne - then you can use "mvForm.TextBox1.Text"

Here's a basic outline of a "standard" form and module project - just
remember to set the startup point to Sub Main, and not your form;

Module Blah
Public Sub Main()
Public myForm As New FrmOne
Application.Run(myForm)
End Sub
Public Sub MyCommonModuleSub()
myForm.Button.Text = "Code ran"
End Sub
End Module

Class FrmOne
/// Form initialisation, component code, etc ///
Private Sub Button_Click(pSender As Object, pArgs As EventArgs) Handles
Button.Click()
MyCommonModuleSub()
' button text on the form will now be changed
End Sub
End Class
____________________________________
The Grim Reaper
 
Back
Top