In Visual Basic 6.0, Control Arrays were a useful feature that allowed programmers to group a series of controls into an array structure, created and initialized by VB6. Event handling code for events raised by these controls could be written in a single event handler procedure for each event, and the index of the array element that triggered the event would be passed as a parameter to the handler procedure.
Visual Basic .NET and C# do not have the concept of Control Arrays as a native structure, maintained by the language itself. In order to convert this feature, the VBUC declares a new Array containing the controls that were included in the original VB6 Control Array and then adds extra coding to the Form Designer File to initialize those controls. The following example shows a VB6 application that has a Control Array called “Buttons”, which contains VB6 CommandButton objects, and how it is converted to C#.
Figure 1. VB6 Application showing the CommandButton controls contained in the “Buttons” array
The C# version of this form will contain the following code in the Form1.Designer.cs file, which declares all the Buttons contained in the original VB6 Control Array:
private System.Windows.Forms.Button _Buttons_4; private System.Windows.Forms.Button _Buttons_3; private System.Windows.Forms.Button _Buttons_2; private System.Windows.Forms.Button _Buttons_1; private System.Windows.Forms.Button _Buttons_0; public System.Windows.Forms.Button[] Buttons = new System.Windows.Forms.Button[5];
The VBUC will also include a new method that will explicitly add all these Button controls to the “Buttons” array:
void InitializeButtons() { this.Buttons[4] = _Buttons_4; this.Buttons[3] = _Buttons_3; this.Buttons[2] = _Buttons_2; this.Buttons[1] = _Buttons_1; this.Buttons[0] = _Buttons_0; }
This InitializeButtons method will be called right after InitializeComponent is called from the Form’s constructor. InitializeComponent will create the actual instances of the controls.
public Form1():base() { Additional code removed for clarity purposes //This call is required by the Windows Form Designer. InitializeComponent(); InitializeButton(); }
As mentioned before, in VB6 a single method could handle a specific event for all the members of the Control Array. In this example, an event handler is used to respond to the Click event of all the VB6 CommandButton controls contained in the “Buttons” Control Array:
Private Sub Buttons_Click(Index As Integer) Select Case Index Case 0 MsgBox ("Buttons(0) was clicked") Case 1 MsgBox ("Buttons(1) was clicked") Case 2 MsgBox ("Buttons(2) was clicked") Case 3 MsgBox ("Buttons(3) was clicked") Case 4 MsgBox ("Buttons(4) was clicked") End Select End Sub
In order to convert this Event Handler, the VBUC adds an “Index” variable, which corresponds to the actual index of the Button that raises the event. This completes the migration of the “Buttons” Control Array:
private void Buttons_Click( Object eventSender, EventArgs eventArgs) { int Index = Array.IndexOf(Buttons, eventSender); switch(Index) { case 0 : MessageBox.Show("Buttons(0) was clicked", …); break; case 1 : MessageBox.Show("Buttons(1) was clicked", …); break; case 2 : MessageBox.Show("Buttons(2) was clicked", …); break; case 3 : MessageBox.Show("Buttons(3) was clicked", …); break; case 4 : MessageBox.Show("Buttons(4) was clicked", …); break; } }
Figure 2. C# Application showing the Button controls contained in the “Buttons” array and responding to the Click event
8834 N Capital of Texas Hwy, Ste 302
Austin, TX 78759
Call us: +1 (425) 609-8458
info@wearegap.com