Accessing a Form control from a class

  • Thread starter Thread starter Jon Masterson
  • Start date Start date
J

Jon Masterson

Hi All

I am trying to access a control on my main form from a class. The control
is a Windows Media Player which I do not think I can instantiate in code.
The class needs to pass the file name to play. I am new to VB.Net, but not
to Basic or OOP, so I am sure that I am missing something obvious. I have
tried to access the forms controls using the form name 'form1.' but the
list I get does not contain any controls. I think all the controls should
be accessible.

Any help much appreciated

Jon
 
Where is your class defined and instaniated ?

on the form. I had it instantiated in a separate global module but moved
itto the form. What I find odd is that none of the controls on the form
show up in the drop list that appears after form1.

Jon
 
on the form. I had it instantiated in a separate global module but moved
itto the form. What I find odd is that none of the controls on the form
show up in the drop list that appears after form1.
Sorry - not sure I answered that correctly. The class (InterpreterClass)
is defined in it's own file (interpreter.vb) not in the form code. It is
intantiated in the form code along with the other variables for the form:

Public interpreter As New interpreterClass

Jon
 
OK, this is ONE way . . .

Create a constructor which will take a reference of the type form1. On your
form create a button and a textbox for demonstration purposes. The Textbox
instance is named TextBox1 in this example.

In the button

Dim ic As New InterpreterClass(Me)

When you press the button you pass the reference to the current form using
'Me' and as you can see in the constructor of the class you can then refer
to the parent form

Public Class InterpreterClass

Sub New(ByVal f As Form1)

f.TextBox1.Text = "Ive just been instantiated"

End Sub

End Class
 
Back
Top