Skip to content

How Do I Implement Control Arrays (without or without dynamic loading)

Benjamin edited this page Aug 14, 2020 · 1 revision

Control Arrays

In VB6, each control had an Index field. WPF does not have that. Instead, we append the index to the control name like this:

txtMyTextBox_3

From there, we use import our extension class:

using static VBExtension;

And, then, we have an effective API we can use, including extension methods for the form itself:

  • int controlIndex(string Name | Control c) - Returns the index parsed from the control name.
  • FrameworkElement Window.getControlByIndex(this Window Frm, string name, int Idx) - Returns the control by string base name and int control index
    • Returns null if element doesn't exist.
    • e.g., frmMyForm.getControlByIndex("txtMyTextBox", 3) - returns the control named txtMyTextBox_3
    • Augment this with a local function for VB6 syntax:
      • e.g., public TextBox txtMyTextBox(int n) { return this.getControlByIndex("txtMyTextBox", n); }
  • FrameworkElement Window.loadControlByIndex(this Window Frm, Type type, string Name, int Idx = -1) - Create a new element of the control array. Replaces Load txtMyTextBox(3).
    • e.g., `this.loadControlByIndex(typeof(TextBox), "txtMyTextBox", 3);
  • void unloadControlByIndex(this Window Frm, string Name, int Idx = -1) - Unloads and removes control from window
    • e.g., `this.unloadControlByIndex("txtMyTextBox", 3);
  • void Window.unloadControls(this Window Frm, string Name, int baseIndex = -1
  • int Window.controlUbound(this Window Frm, string Name) - Returns ubound of controls

Using these, you can have dynamic loading & unloading of what are effectively VB6 Control arrays. You can access them directly using their name, or you can route them through the VB6 syntax help function (above).

Clone this wiki locally