Visual Basic .NET dynamic loading class (1)

xiaoxiao2021-03-06  44

Visual Basic .NET dynamic Loading class · · Microsoft Abstract: Ideally, we should know which operations need to complete this software system before writing software systems. But the actual situation is not the case, so our system should be adjustable. It is the ability to reflect this adaptability to dynamically integrate new features. For example, in a Windows Form Application, this ability refers to loading compiled original application 绦 芫 芫 芫 蟠 蟠 蟠 ǖ 拇 芰 芰 α α? / P> This operation is almost not in Microsoft Visual Basic® 6.0. Yes, but it is quite easy in Visual Basic .NET. With the functionality of the System.Reflection namespace, the program only needs to know the location of the assembly to request dynamically load this assembly. The class in the assembly can then be loaded and the class is instantiated. Since the Windows form is also a kind, this means that the form can be dynamically loaded, displayed, and the form does not exist even when compiling the original program. A typical solution assumes that we have a multi-document interface (MDI) application that requires the ability to load any form as a child form. Then the application should be able to do the following:

Gets a list of forms that can be loaded, usually not in the .NET configuration file. Display this list and allow the user to select a form from it. Instantiate and display the user selected form. Let's write such an app to see how to complete the dynamic load. Step 1: Create a project and the MDI form to start a new Windows Form. Name it as Formsonthefly. In the empty form form1 included in the new project, change its ismdicontAiner property to True. Thus, the form becomes an MDI parent window. Changing the size of the form, making the form's length and wide dimension of approximately twice the default value. Drag a panel control onto the form and set its Dock property to connect it to the top of the form. Change the size of the panel to make it a height of about 0.5 inches. Drag a combo box onto the panel. Name it CBOFORMS and set it to DropDownList. Finally, drag a button onto the panel. Name it BtnloadForm, and then set it to load form. At this point, FORM1 should be shown in Figure 1. Figure 1: MDI Forms step 2 after adding all controls: Generate a class to store data for available forms We will get information that may be loaded from an XML-based configuration file. However, in order to facilitate displaying the available forms in the combo box and obtain information about the selected form, an object collection should also be established. Each object in the collection will store information for available forms. Objects should have the following properties:

Description: Displays the form description in the combo box. Location: The file name of the DLL where the form is located. TYPE: The name of the .NET type (for example, MyProject.form1). Such a collection can be bind to the list box. To return to the reference to the selected object from the list box, you still need an attribute, 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:

转载请注明原文地址:https://www.9cbs.com/read-79065.html

New Post(0)