call method on passed form

T

Tim

hi all

If I pass a form to a sub as a parameter:
eg

call mysub(me)

how can I call methods on that form?

eg
sub mysub(FRM as system.windows.forms.form)
x=FRM.getxvalue()
end sub

I can get generic stuff like FRM.controls, or FRM.name, but nothing
that I have created my self. Any clues?

thanks in advance
 
P

Patrice

..Just use the type you created for your form instead of the "Form" type
provided by the framework.
 
C

Cerebrus

Hi,

If the methods in the called form are marked "Public Shared", then they
can be called by other forms.

HTH,

Regards,

Cerebrus.
 
T

Tim

can you explain a bit more?

I don't actually now the location of the calling form.
All the info I have is the form passed as a parameter?!
 
T

Tim

actually I got there!

answer:

instead of dimming FRM as a windows.form, I left the "as" part off
(leaving it as a variant?!)
I wasn't then restricted to picking items from the windows.form list, I
could then reference my own sub.

thanks for you help!
 
P

Patrice

I wouldn't do that either. Here you are not restricting at all what you are
able to pass to your sub (this is "As Object").

I meant that when you create a form, you are actually creating a new class
(see the Class statement at the top of your form file). This is this class
name that you should use as the type of the argument for this sub :
- you'll be able to have intellisense providing the new members you added
- you won't be able to pass by mistake something else (such as a form that
wouldn't provide these members)

Previously the problem was that you were passing a "System.Windows.Form"
that doesn't have the members you added. The class *you* created is the one
that have these new members, not the built in Form class provided by the
framework...
 
H

Herfried K. Wagner [MVP]

Tim said:
If I pass a form to a sub as a parameter:
eg

call mysub(me)

how can I call methods on that form?

eg
sub mysub(FRM as system.windows.forms.form)
x=FRM.getxvalue()
end sub

I can get generic stuff like FRM.controls, or FRM.name, but nothing
that I have created my self. Any clues?

\\\
Public Sub MySub(ByVal frm As Form1)
x = frm.GetXValue()
End Sub
....
MySub(Me)
///
 
T

Tim

slight problem is...

I have no idea of the calling exe or dll.
I have no idea of the type of form, where it is or where it comes from.
The only single solitary thing I have is the form object passed as a
parameter to an unrelated sub in an unrelated exe.
I have no other reference at all to the calling exe or form.

Tim
 
P

Phill W.

If the methods in the called form are marked "Public Shared", then they
can be called by other forms.

Why "Public /Shared/", specifically?
You can call Public methods on a Form provided you have a reference
to an instance of it (as in the OP's case). Explicitly making the method
Shared in unnecessary.

Public Class SubForm
Inherits Form

Public Function GetXValue() As ...
.. . .

Public Class MainForm
Inherits Form

Public Sub MySub( ByVal oForm As SubForm )
Debug.Writeline( oForm.GetXValue() )
End Sub
.. . .

Regards,
Phill W.
 
P

Patrice

If you don't know at all about the form type how do you know it will provide
methods such as "GetXValue" ?

Clearly it won't work with a from that doesn't provide this method.

I'm still not sure about what you are doing but generally this is done in a
way such as :
- you create a class MyForm that inherits from Form and you add the required
functionnality
- the "sub" uses a MyForm type form as an argument

Now if someone calls your sub, he have to use a MyForm (or a MyForm child
class) object. Inheriting from MyForm will guarantee the form has the
methods required by tour sub...

The caller is not the problem. The argument itself is not really a problem
as it is passed to you. A possible problem is to be able to ensure as soon
as possible that the form has the required methods added to it (it seems to
me that your sub requires the form to have additional methods). Would be
cleaner...
 

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