StrataFrame Forum

Calling Other Forms

http://forum.strataframe.net/Topic12894.aspx

By Mike Thomas - 12/3/2007

I saw this code snippet in the CRM Sample.  Is the recommended method for calling other forms?  can you explain the activator object?

Private Sub LaunchForm(ByVal FormType As System.Type)

'-- Establish Locals

Dim loForm As Form

'-- Create the form

loForm = CType(Activator.CreateInstance(FormType), Form)

'-- Set the MDI parent

loForm.MdiParent = Me

'-- Show the form

loForm.Show()

End Sub

#End Region

#Region " Handled Events "

''' <summary>

''' Launch the customers maintenance form

''' </summary>

'''<param name="sender"></param>

''' <param name="e"></param>

''' <remarks></remarks>

Private Sub cmdCustomers_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCustomers.Click

Me.LaunchForm(GetType(CustomerMaintenance))

End Sub

By Peter Jones - 12/3/2007

Hi Mike,

This thread may help:

http://forum.strataframe.net/Topic9345-14-1.aspx?Highlight=open+form

Cheers, Peter

By Trent L. Taylor - 12/3/2007

This is a good approach and is really dependent upon what you are trying to accomplish.  In any case, you should always create a single point from which all of your forms are shown or displayed.  This makes it MUCH easier to verify security, provide additional launching logic, and ensure that all of the forms and dialogs are launched the same way.

The Activator class is a function of reflection, which is another relatively lengthy discussion.  If you will notice, every object or control ultimately inherits from the Object class.  This has a method called GetType() which is the entry point into reflecting over an object to learn which properties, methods, events, attributes, etc. are available on that object.  .NET is a strong-typed language and as such you have to conform to the strong-typed environment (or at least you should as you can fudge a little in VB.NET by turning off Option Strict....but don't do that Smile).  The Activator class allows you to create an instance by simply referencing the Type of that object.  The Type must have a constructor that allows the object to be created is really the only requirement.  It will then return a new instance of that object...since you generally know the base level of the type of object, you can then cast (CType, DirectCast) that object into a variable that is typed as such....for example a form.  Any dialog in the system, even if inheriting the SF StandardForm, inherits off of the System.Windows.Forms.Form class so you could safely type those objects as a form without error.  You can also type objects as an interface so that you can safely call methods or properties on that interface, but obviously your class must implement that interface otherwise you will receive an error.

This is a majorly condensed explanation and is something that we go over in depth in our training class.  We have a class the last week of February which we will be make available for purchase online within the next week or so.  If you can, this class will answer these types of questions and many more.