Call class from form

How can I convert this function to a class to call from multiple forms.


Thank you


  Private Function IsChildInMemory(ByVal pChildName As String) As Boolean
        'A small method that checks to see if a particular form is currently displayed as a child form
        Dim frmChild As Form
        For Each frmChild In Me.MdiParent.MdiChildren

            If TypeName(frmChild).ToUpper = pChildName.ToUpper Then
                frmChild.Activate()
                Return True
            End If
        Next
        Return False
    End Function
running32Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

eventprostrategiesCommented:
Public Function IsChildInMemory(....)

make it a method of the MDIParent form.  So the children can call Me.MDIParent.IsChildInMemory(form1) ... if that's what you want to be able to do.
eventprostrategiesCommented:
to be exact ... Me.MDIParent.IsChildInMemory(-) might not work

dim parent as MyMDI = Ctype(Me.MDIParent, MyMDI)
parent.IsChildInMemory

however you do it ... the MDIParent form should be the best place for that method as all mdiChildren should have a reference to the MDIParent so they can all call the method easily.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
running32Author Commented:
So if I add this function to the mdi parent then I could call it from all the sub forms?

If so how would I cal it from a sub form?
Exploring ASP.NET Core: Fundamentals

Learn to build web apps and services, IoT apps, and mobile backends by covering the fundamentals of ASP.NET Core and  exploring the core foundations for app libraries.

brian78waCommented:
public class Class1
Private Function IsChildInMemory(ByVal frmChild as string, ByVal pChildName As String) As Boolean
        'A small method that checks to see if a particular form is currently displayed as a child form
        Dim frmChild As Form
        For Each frmChild In Me.MdiParent.MdiChildren

            If TypeName(frmChild).ToUpper = pChildName.ToUpper Then
                   Return True
            End If
        Next
        Return False
    End Function
end class
then from each form

if Class1.IsChildInMemory(formname, childname)  then  frmChild.Activate()

brian78waCommented:
oops function needs to be public in the class
brian78waCommented:
And actually now that i think about that wouldnt work because it would never get to a second drmChild because it is returned the first time thru

so you could say

public class Class1
public Function IsChildInMemory(ByVal frmChild as string, ByVal pChildName As String) As Boolean
        'A small method that checks to see if a particular form is currently displayed as a child form
       
            If TypeName(frmChild).ToUpper = pChildName.ToUpper Then
                   Return True
            End If
              Return False
    End Function
end class

then from each form

 Dim frmChild As Form
   For Each frmChild In Me.MdiParent.MdiChildren

             if Class1.IsChildInMemory(formname, childname)  then  frmChild.Activate()
  Next

eventprostrategiesCommented:
[quote]  So if I add this function to the mdi parent then I could call it from all the sub forms? [/quote]

yep.  if the function is public and a member of your MDIParent form then ...

[in mdi parent]
dim f as frmChild1()
f.MdiParent = Me
f.Show

[in child form]
dim parent as MyMDI = Ctype(Me.MDIParent, MyMDI) '// if the name of your MDI form class were 'MyMDI'
dim bChildInMem as boolean = parent.IsChildInMemory(someform)
Éric MoreauSenior .Net ConsultantCommented:
Why would that function would need to be placed elsewhere then in the MDI container form? Since only the MDIParent should loop through its child, it makes sens to me to be in this class.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.