pass form to function

  • Thread starter Thread starter stanuf
  • Start date Start date
S

stanuf

Hi!

I am new to vb.net and have a question on how to pass a form to a function.
Could someone point me to an example or give me a quick example on how to do
this.

Thank you
Stacy
 
private function fntMyFunction (objForm as object)
'my function getting a form as paramater
end function

than you can call the function like this:
fntMyFunction (frmMyForm)


If you know alreaddy what kidn of form you're going to use you better use:
private function fntMyFunction (objForm as frmMyForm)
-> This way you can use the methods of the form.
 
Ouch. You should never pass something around as an object that has a deeper
base class. Clearly all Windows forms derive from
System.Windows.Forms.Form, so you should have defined the object as that
instead. Something like this:
Sub MyFunction (passedForm as System.Windows.Forms.Form)
' Do Something with passedForm
End Sub

You could also do this, if you have already imported a reference to
System.Windows.Forms

Sub MyFunction(passedForm as Form)
' Do something with passedForm
End Sub

If you are looking for a particular instance of a form, or a particular form
class, you could define the referenced object in that particular class. The
other choice you could have is to check the type of the passedForm object,
and see if it implements the class or interface you are expecting, and then
cast it into that class. All depends on what you're trying to do... just
don't pass it as an object!

Ryan Gregg
 
Hi Stanuf,

A real agresive sample, it set all text on your form to "Stanuf"

Private Sub Load_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim nada As New doCtr(Me)
End Sub
End Class
Public Class doCtr
Public Sub New(ByVal parentCtr As Control)
Dim ctr As Control
For Each ctr In parentCtr.Controls
ctr.Text = "Stanuf"
Dim newdoCtr As _
New doCtr(ctr)
Next
End Sub
End Class

I hope this helps a little bit?

Cor
 
* "stanuf said:
I am new to vb.net and have a question on how to pass a form to a function.
Could someone point me to an example or give me a quick example on how to do
this.

\\\
DoSomething(Me)
..
..
..
Public Function DoSomething(ByVal f As MainForm) As ...
 
Back
Top