By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,327 Members | 849 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,327 IT Pros & Developers. It's quick & easy.

call method on passed form

P: n/a
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

Mar 30 '06 #1
Share this Question
Share on Google+
9 Replies


P: n/a
Hi,

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

HTH,

Regards,

Cerebrus.

Mar 30 '06 #2

P: n/a
..Just use the type you created for your form instead of the "Form" type
provided by the framework.

--
Patrice

"Tim" <Ci************@gmail.com> a écrit dans le message de news:
11*********************@i40g2000cwc.googlegroups.c om...
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

Mar 30 '06 #3

P: n/a
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?!

Mar 30 '06 #4

P: n/a
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!

Mar 30 '06 #5

P: n/a
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...

--
Patrice

"Tim" <Ci************@gmail.com> a écrit dans le message de news:
11**********************@j33g2000cwa.googlegroups. com...
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!

Mar 30 '06 #6

P: n/a
"Tim" <Ci************@gmail.com> schrieb:
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)
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>
Mar 30 '06 #7

P: n/a
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

Mar 30 '06 #8

P: n/a
"Cerebrus" <zo*****@sify.com> wrote in message
news:11*********************@i39g2000cwa.googlegro ups.com...
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.
Mar 30 '06 #9

P: n/a
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...

--
Patrice

"Tim" <Ci************@gmail.com> a écrit dans le message de news:
11**********************@u72g2000cwu.googlegroups. com...
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

Mar 30 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.