Accessing an instance of a form from another class

D

Duane Roelands

Hello all.

I'm having some difficulty accessing the .text property of a label on my
startup form.

My form is frmStartup, and this is the application's startup object (as
opposed to sub Main() ).

I have a module called "xmlHandler", and in that module, I would like to
change the .text property of the lblStatusMessage label which is on
frmStartup.

Now, if my startup object were sub Main(), this would be simple...

dim myForm as new frmStartup
myForm.lblStatusMessage.text = "foo"

However, because my startup object is frmStartup, I'm not sure what the
reference to the instance of the frmStartup class is. Any guidance would be
appreciated. Thank you in advance.

Duane Roelands
 
P

Paul [Paradise Solutions]

Well, as I understand it, you can't do that because to reference the
form from the module you'd have to instantiate a new reference - which
wouldn't help you much because your visible form would be a different
instance to the one you were setting the value of.
It would probably be better to make the xml module into a class, and let
the form consume it via properties and such.


Paul
 
Y

Yves Zouzouambe

The easiest thing would definately be to have a Sub Main() to do this
work for you. If this is not possible for some reason then I would
have the Load event on the form call a function on your module to send
a reference to itself, then have that function change the label's text
property.
 
F

fd123456

Hi Duane,

You should pass the reference of the label to the xmlHandler method :

In frmStartup:

xmlHandler.SomeFunction(myLabel)

in xmlHandler :

Public Sub SomeFunction(ByRef L as Label)
....do some calculations...
L.Text = result.ToString
End Sub

If you're not calling the function from frmStartup, you can still pass
the Label ByRef along a chain.

HTH,

Michel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top