Changing a form caption from another class

J

John Dann

In VB.Net I have a Form1 main form that instantiates various class
objects. I want to change eg the title text of Form1 from within one
or more of these classes.

The solution kindly suggested here was to add a constructor into my
subsidiary class's code as in

Public Class MyClass

Public Sub New(ByVal frm as Form1)
F1=frm
End Sub
..... etc

And then in Form1 to instantiate the class as :

Dim oMyClass as New MyClass(Me)

Then in the code inside MyClass I can say:

F1.Text = "New window title"

This works fine when MyClass is instantiated inside a procedure inside
Form1, but I now want to use the oMyClass object with module-level
scope and so need to instantiate it in the form's main declarations
section.

But this is giving an error presumably because the reference to Me
isn't valid until the form's own constructor has run.

Is thee a way around this please?

JGD
 
G

Guest

In the class use:
/////
Public Sub New(ByRef frm as Form)
frm.Text="My Title"
' ... exc
End Sub
\\\\\

In the main program use:
/////
Dim oMyClass as New MyClass(Me)
\\\\\
 
G

Guest

I do it this way:

in your module, create a variable of the type that is your form's class name.
Then in the form's load event, set the variable to "me".

so:
Module Mod1
public MyForm as Form1
End Module

Form Form1
sub form_Load...
Mod1.MuForm=Me
end sub
end form

Kind of like that.
 

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