Passing a form object in a function

  • Thread starter Thread starter Sebastian Santacroce
  • Start date Start date
S

Sebastian Santacroce

Hi,
If I want to pass a form (forms I have created) to a
function what would I set the declaration as

for example

Dim p as existingForm
OpenForm (p, existingForm)


Function (P as windows.forms.form, thisForm as *******)
p=NEW thisForm
.....

What do I put in *******?
 
Dim p as existingForm

OpenForm(p)

Function(ByRef theForm as existingForm)
End Function
 
as ExistingForm. If I'm reading this right, you don't even need to pass the
P.
 
Sebastian said:
Hi,
If I want to pass a form (forms I have created) to a
function what would I set the declaration as

for example

Dim p as existingForm
OpenForm (p, existingForm)


Function (P as windows.forms.form, thisForm as *******)
p=NEW thisForm
....

What do I put in *******?

If I read this right, you want to have a function that create an instance of
a type, without knowing a priori exactly what that type will be. Because
..Net doesn't have generics (yet), the only way to do this is using
reflection. Note that the example below will be slower than just using 'p =
new existingForm'.

First, I'd think the design of your program through to decide if you
*really* need this. But if you must do it, this is how:

Imports System.Reflection

Public Sub OpenForm(ByVal p As Windows.Forms.Form, ByVal thisForm As Type)
Dim ci As ConstructorInfo = thisForm.GetConstructor(Type.EmptyTypes)
p = ci.Invoke(Nothing)
' do something with p.
End Sub

Dim p As existingForm
OpenForm(p, Type.GetType("YourNamespace.existingForm"))

Again, only do this if you have absolutely no alternative. It is ugly code,
hell to maintain, not very performant, and not type-safe.
 
* "Sebastian Santacroce said:
If I want to pass a form (forms I have created) to a
function what would I set the declaration as

for example

Dim p as existingForm
OpenForm (p, existingForm)


Function (P as windows.forms.form, thisForm as *******)
p=NEW thisForm
....

What do I put in *******?

I am not sure what you want to do, but maybe you are looking for this:

\\\
Private Function CreateClassByName( _
ByVal PartialAssemblyName As String, _
ByVal QualifiedClassName As String _
) As Object
Return _
Activator.CreateInstance( _
[Assembly].LoadWithPartialName( _
PartialAssemblyName _
).GetType(QualifiedClassName) _
)
End Function
///

Usage:

\\\
Dim c As Control = _
DirectCast( _
CreateClassByName( _
"System.Windows.Forms", _
"System.Windows.Forms.Button" _
), _
Control _
)
With c
.Location = New Point(10, 10)
.Size = New Size(80, 26)
.Text = "Hello World"
End With
Me.Controls.Add(c)
///
 
This is exactly what I was looking for but its not
working.
The
Type.GetType("YourNamespace.existingForm")

is returning "Nothing".
What would I put under "YourNamespace"?
would one always exist.
This solution has only one project and I haven't created
any namespaces but the form is in a solution folder.
Is there something I'm missing?

Thanks.
 
Sebastian Santacroce said:
This is exactly what I was looking for but its not
working.
The
Type.GetType("YourNamespace.existingForm")

is returning "Nothing".
What would I put under "YourNamespace"?
would one always exist.
This solution has only one project and I haven't created
any namespaces but the form is in a solution folder.
Is there something I'm missing?

Open the class view or the object browser.


--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html
 
Sebastian said:
This is exactly what I was looking for but its not
working.
The
Type.GetType("YourNamespace.existingForm")

is returning "Nothing".
What would I put under "YourNamespace"?
would one always exist.

The default root namespace for a VB.NET project is always the project name,
unless specified otherwise in the project properties.
 
Sven said:
Public Sub OpenForm(ByVal p As Windows.Forms.Form, ByVal thisForm As
Type)

Upon re-reading, it occurred to me you probably want the form instance to be
accessible through the passed first parameter once OpenForm has returned. In
which case you should put:
Public Sub OpenForm(ByRef p As Windows.Forms.Form, ByVal thisForm As Type)

Note the ByRef instead of ByVal for the first parameter.
 
So that worked great now.
I need to check if the form has been closed ie, the
variable has been disposed so I can recreate the form.

How would I check if the variable is disposed?
 
Scratch that last dispose question. Figured it out. I used
Canfocus property of form to detect if its set to dispose.
I basically wanted to not have the same form open twice
which works fine now for forms without contructor
parameters.

However, regarding the function you gave I've added an
optional parameter to give to the form contructor an array
of types but it always gives the error: "Object reference
not set to an instance of an object".Heres the function:

Public Sub ShowForm(ByRef objForm As Windows.Forms.Form,
ByVal objFormInstance As Type, Optional ByVal Params As
Object() = Nothing) '

If IsNothing(objForm) OrElse Not objForm.CanFocus
Then
Dim ci As ConstructorInfo =
objFormInstance.GetConstructor(Type.EmptyTypes)
objForm = ci.Invoke(Params)
objForm.Show()
Else
objForm.Activate()
End If

End Sub

This particular form's contructor takes one parameter of
type string and I want to set it to "go"
The Call to the above function is:
Dim formVariable as FormName
Dim ParamArr(0) As Object
ParamArr(0) = "go"

Call ShowForm(formVariable, Type.GetType
("Namespace.FormName"), ParamArr)

Whats causeing that Error? Am I passing the array
incorrectly? or my declaring it wrong?
Thanks,
 
Back
Top