forms collection?

M

Martin

VB2005

Hi!

I know there is an OpenForms collection, but is there also a collection of
all forms defined in an app?
I have built a "shortcut" bar and now need to translate a string variable
"Form1" to an object name Form1.

I thought I'd simply loop through a collection until the string value equals
the form.name. But I cannot find an applicable collection....

Ideas anyone?

Tia,
Martin
 
M

Martin

Hi Cor,

Thanks for your response. I have been looking at the My.Forms object, but I
didn't see how I could use it, since it doesn't seem to have an enumerator.

I have the form name in a string variable and I thought to make a function
something like this:

Private Function OpenForm(ByVal FormName as String) as Boolean
For Cntr=0 to My.Forms.Count - 1
If My.Forms(Cntr).Name = FormName then
My.Forms(Cntr).Show()
Exit Function
End If
End Function

In other words, I need to convert a form name in a string to an object....

Tia,
Martin
 
H

Herfried K. Wagner [MVP]

Martin said:
I know there is an OpenForms collection, but is there also a collection of
all forms defined in an app?
I have built a "shortcut" bar and now need to translate a string variable
"Form1" to an object name Form1.

You may want to use 'Activator.CreateInstance' to instantiate the forms.
 
M

Martin

Hi Herfried,

Thanks for your reply. This sounds like the function I need, but I can't get
it to work...

Dim FormName as string = "frmPatient"
Activator.CreateInstance(FormName, System.Windows.Forms.Form)

This is one of the many things I tried... Unfortunately the Help file is not
very "Helpful"...

What am I doing wrong?

Tia,
Martin
 
M

Martin

Hi Herfried,

I got it! No thanks to VS2005 help system, but your very informative website
dotnet.mvps.org

This is the code that made it work (maybe someone else would be interested
as well):

Private Sub ShowFormByName(ByVal FormName As String)
Dim frm As Form
Dim tpe As Type
tpe = Type.GetType("appEspital." & FormName)
frm = DirectCast(Activator.CreateInstance(tpe), Form)

frm.MdiParent = Me
frm.Show()

End Sub

Thanks again,
Martin

Martin said:
Hi Herfried,

Thanks for your reply. This sounds like the function I need, but I can't
get it to work...

Dim FormName as string = "frmPatient"
Activator.CreateInstance(FormName, System.Windows.Forms.Form)

This is one of the many things I tried... Unfortunately the Help file is
not very "Helpful"...

What am I doing wrong?

Tia,
Martin
 

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

Similar Threads


Top