Matthew A. Stoeckervisual Studio Teammicrosoft Corporation January 2002
Summary: This article describes how to create and manage control arrays using Visual Basic? NET and Visual C #? Net.
Directory Introduction Prerequisites Creating a Project Implementation A Collection Open Control Architecture Creating a Public Event Processor Test Project Summary Introduction An array provides a convenient way for control groups that use shared public functions. For example, the control group can be used to display related data, or provide related operations when you click. Visual Basic .NET and C # itself do not support creating control arrays, but you can copy all the features of the control array by programming. This article will guide you a simple component that creates a replication control array function.
Some uses of the array of controls are as follows:
Accessing a control collection with the same name by an index, you can retrieve and set the properties and traverse all controls in the array. The typical syntax of this operation is as follows: 'Visual Basic Pseudo code myControl (myIndex) .myproperty = myValuemyControl (MyIndex 1) .mymethod
// c # pseudo code myControl [myIndex] .myproperty = myValue; myControl [MyIndex 1] .MYMETHOD Hands the events of multiple controls, retrieves and uses the indexes in these events, as shown in the following example: 'Visual Basic Pseudo-Code Private Sub MyControl_Click (Sender As Object, E AS Eventargs) MessageBox.show ("You Have click MyControl No. & _ myControl.index) End Sub
// c # pseudo code private void myControl_click (System.Object sender, system.eventargs e) {messagebox.show ("You have clicked MyControl No. MyControl.index);} Dynamically add or delete controls during runtime, as follows Schedule: 'Visual Basic Pseudo Code DIM I AS IntegerFor i = 1 to 5' Insert the code to create a control and assign a value. Next I
// c # pseudo code for (int i = 1; i <6; i ) {// Insert code to create a control and assign a value for the property. } Visual Basic .NET and C # allow you to copy some such features, for example, using the agent to bind the events in multiple controls to a single event handler. However, if this feature is incorporated into a single dynamic, it may be more convenient to manage components. In this article, we will create components using the following:
Collection of indexes and sort controls. The button collection will be used for demos. Processing an event handler from a derivative button. Allows the codes of the control and its members to be referenced by the index. Dynamically add and delete the code of the control in the form. Prerequisite is familiar with the component and its working principle. I have an understanding of the polymorphism. For more information, see Polymorphism In Components. Learn about Visual Basic .NET or C # .NET syntax. Create a project in this section, we will create and name the project and add a class to your project. This class will encapsulate the code of the array of controls.
Create ButtonArrayProject and ButtonArray Components
On the FILE (File) menu, point to New, then select Project to open the New Project dialog. Select Windows Application (Windows Application) project template from the Visual Basic or Visual C # item list and type buttonarrayProject in the Name box. From the FILE (File) menu, select Save All to save the project. Implementing the Collection ButtonArray class will complete the task of support and organizational control arrays by implementing a collection. A collection is an object that contains a list of indexed object variables and methods for adding, deleting, or operating objects in a collection. In this section, we will create classes inherited from the System.Collections.CollectionBase (Class in the .NET framework, providing a number of features required to set a collection), and implement methods for providing the required functions. Create inheritance class
From the Project menu, select Add Class. The class is named ButtonArray.vb or ButtonArray.cs accordingly. The Code Editor containing the class will open.
In the class declaration, specify that this class is inherited from the System.Collections.CollectionBase class from the .NET framework. 'Visual BasicPublic Class ButtonArray Inherits System.Collections.CollectionBasend Class
// c # public class buttonArray: system.collections.collectionBase {// The code added by the designer is omitted. } The system.collections.collectionBase class provides a number of features needed to collect a collection. These include List objects (objects included in the tracking collection), count attributes (maintain the total number of objects in the current collection) and the REMOVEAT method (delete objects by specific index). These functions are used when implementing the set of controls.
Since all control array components are associated with a form, you need to add a field to retain references to the form. Keep this reference by creating a dedicated read-only field, ensuring that each control array component is only associated with a form.
Add a dedicated read-only field in the component
Add directly to the class declaration: 'Visual BasicPrivate Readonly Hostform as system.windows.Forms.Form
// c # private readonly system.windows.forms.form Hostform; the first method that must be implemented in the collection is AddNewButton. This method will create a new button control and add it to the desired form. You also need to use this method to set the initial properties of the new button.
Implement addNewButton method
In the Code Editor containing the ButtonArray class, type the following code: Public Sub AddNewButton () 'Creating a new instance of the Button class. DIM Abutton as new system.windows.Forms.Button () 'Add the button to the inner list of the collection. Me.List.Add (Abutton) 'Adds the button to the control collection of the form referenced by the HostForm field'. Hostform.controls.Add (Abutton) 'Sets the initial properties of the button object. Abutton.top = count * 25 Abutton.Left = 100 Abutton.tag = me.count abutton.text = "button" & me.count.tostringend Sub // C # public void addnewbutton () {// Create a new instance of the Button class . System.windows.Forms.Button Abutton = new system.windows.forms.button (); // Add the button to the list of the collection. This.List.Add (Abutton); // Add the button to the control collection of the form referenced by the HostForm field //. Hostform.controls.add (Abutton); // Setting the initial properties of the button object. Abutton.top = count * 25; Abutton.Tag = this.count; Abutton.Text = "Button" this.count.tostring ();} This method will:
Create a new button. Add a new button to the internal list and a collection of controls referenced by HostForm. Set the initial attribute, including the index of setting the tag property to the button. You can add code to set the other properties of the control in this section. You must also create a constructor (method for instantiating the component), used to set the value of the HostForm field, and you can add a new button to the form as long as you create a new instance of the control array class. You can complete this task in the following ways.
Create a structural function
Create a class constructor. 'Visual BasicPublic Sub New (Byval Host As System.Windows.Forms.form) Hostform = host me.addnewbutton () End Sub
// c # // Use this constructor to replace the default constructor. Public ButtonArray (System.Windows.Forms.form Host) {hostform = host; this.addnewbutton ();} Constructor requires a parameter, that is, the form of the button array is placed. It specifies the value provided to the HostForm field, then call the class AddNewButton method to add a new button in the form.
An array of public controls You have created a way to create and track controls in an array, and now you need to disclose these controls to other developers. You can use an attribute to do this. You will create a default property (Visual Basic) or an index program (C #) that returns a reference to a specific button based on the index. This also enables you to use the typical MyButtonArray (MyIndex) syntax of the control array by programming.
Create default properties
Add the following code to the component. 'Visual BasicDefault Public ReadOnly Property Item (ByVal Index As Integer) As _ System.Windows.Forms.Button Get Return CType (Me.List.Item (Index), System.Windows.Forms.Button) End GetEnd Property // C # Public system.windows.Forms.Button this [int index] {return (system.windows.forms.button) this.list [index];}}