Dynamic load classes in Visual Basic. NET (i)

Source: Internet
Author: User
Tags new features reference
visual| Dynamic | Load dynamically loaded classes in Visual Basic. NET

· ·· Microsoft

Absrtact: Ideally, we should know what the software system needs to do before writing a software system. But this is not the case, so our system should be adjustable.

The best embodiment of this adaptability is the ability to dynamically integrate new features. For example, in a Windows forms application, this ability refers to loading the original application of the Ǖ mi of the central oyster in the courtyard of the peach and the JI of the thumb scar α?/p>

This operation is almost impossible in Microsoft visual basic®6.0, but it is fairly easy in visual Basic. NET. With the functionality of the System.Reflection namespace, the program needs to know the location of the assembly to request dynamic loading of the assembly. You can then load the classes in the assembly and instantiate the class. Because Windows forms are also a class, this means that forms can be loaded, displayed, and used dynamically, even if the form does not exist when the original program is compiled.

A typical scenario
Suppose we have a multiple document interface (MDI) application that requires the ability to load any form as a subform. Then the application should be able to do the following:

Gets a list of forms that can be loaded, typically not in a. NET configuration file.
Displays the list and allows the user to select a form from it.
Instantiate and display the form selected by the user.
Let's write an application to see how to do dynamic loading.

Step 1: Create the project and the MDI form
Start a new Windows forms project. Name it formsonthefly. In the Form1 of the empty form contained in the new project, change its IsMdiContainer property to True. In this way, the form becomes an MDI parent form. Change the size of the form so that the length and width of the form are approximately twice times the size of the default value.

Drag a Panel control onto the form, and then set its Dock property so that it is connected to the top of the form. Change the size of the panel so that it is about 0.5 inches in height.

Drag a combo box onto the panel. Name it cboforms, and then set its DropDownStyle to DropDownList.

Finally, drag a button onto the panel. Name it btnloadform, and then set its Text property to the Load Form.

At this point, the Form1 should be as shown in Figure 1.



Figure 1: MDI forms in design mode after all controls have been added

Step 2: Generate a class that holds the data for the available forms
We will get information about the forms that might be loaded from an xml-based configuration file. However, to make it easier to display the available forms in a combo box and get information about the selected form, you should also set up an object collection. Each object in the collection holds information about a form that is available. The object should have the following properties:

Description: The form description that appears in the combo box.
Location: The file name of the DLL in which the form resides.
Type: The name of the. NET type of the form (for example, Myproject.form1).
Such a collection can be data bound to a list box. To return a reference to the selected object from the list box, you also need an attribute, which we call Reference.

To create a class, select Project | Add Class (Project | add Class), and then name the class Dynamicclass.vb. Add the following code to the class:

Public Class DynamicClass
Dim Mslocation as String
Dim Mstype as String
Dim Msdescription as String

Public Sub New (ByVal slocation as String, _
ByVal Sdescription as String, _
ByVal Stype as String)
Me.location = slocation
Me.description = sdescription
Me.type = Stype
End Sub

Public Property Location () as String
Get
Return mslocation
End Get
Set (ByVal Value as String)
Mslocation = Value
End Set
End Property

Public Property Type () as String
Get
Return Mstype
End Get
Set (ByVal Value as String)
Mstype = Value
End Set
End Property

Public Property Description () as String
Get
Return msdescription
End Get
Set (ByVal Value as String)
Msdescription = Value
End Set
End Property

Public ReadOnly Property Reference () as Object
Get
Return Me
End Get
End Property
End Class




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.