Sample Code

Hi, I have this form in VB6 that is pretty useful and I'm tryng to migrate it to VB2005.

This form receive a query as parameter as well as arrays of width colums and aligments.

The query fills a Grid, and, for each column, a TextBox is created and will be used as a filter. Each TextBox is set with the arrays of width and aligment.

Since control arrays are no longer supported in VB2005, I'll probably use IList or Control Collection however I can't find any code sample of how tu use them properly.

Can anyone help me with this one

Thank you very much



Answer this question

Sample Code

  • capitolino

    Option Explicit On
    Option Strict Off
    Public Class FormsControlCollection
    Inherits System.Collections.CollectionBase

    ' --------------------------------------------------------------------------
    ' Module Name: FormsControlCollection
    ' Comments:
    ' Author: Chris Van Duin - BlueScope Steel Lab Services.
    ' Created: October 2004.
    ' Revisions: None.
    ' Local variables used:
    ' HostForm:
    ' aControl:
    ' --------------------------------------------------------------------------

    #Region "Local variables"

    Private WithEvents aControl As System.Windows.Forms.Control
    Private ReadOnly HostForm As Object

    Public Event Leave(ByVal Item As System.Object, ByVal Args As System.EventArgs)
    Public Event TextChanged(ByVal Item As System.Object, ByVal Args As System.EventArgs)
    Public Event MouseHover(ByVal Item As System.Object, ByVal Args As System.EventArgs)

    Private Const mNAME As String = "colFormsControlArray"

    #End Region

    #Region "Constructors"

    ' --------------------------------------------------------------------------
    ' Name: New
    ' Comments:
    ' Written By: Chris Van Duin - 20th October, 2003.
    ' Modifications: None.
    ' Parameters:
    ' Host:
    ' Returns: Nothing.
    ' Affected Parameters: None.
    ' Local variables used: None.
    ' --------------------------------------------------------------------------
    Public Sub New(ByVal Host As Object)

    HostForm = Host

    End Sub

    #End Region

    #Region "Properties"

    ' --------------------------------------------------------------------------
    ' Name: Item
    ' Syntax: Debug.Print X.Item(3)
    ' Written By: Chris Van Duin - 20th October, 2003.
    ' Modifications: None.
    ' Parameters: Index.
    ' Returns: Nothing.
    ' Affected Parameters: None.
    ' Local variables used: None.
    ' --------------------------------------------------------------------------
    Default Public ReadOnly Property Item(ByVal Index As Integer) As Object

    Get
    Return Me.List.Item(Index)
    End Get

    End Property

    #End Region

    #Region "Actions"

    ' --------------------------------------------------------------------------
    ' Name: aControl_Leave
    ' Comments:
    ' Written By: Chris Van Duin - 24th February, 2004.
    ' Modifications: None.
    ' Parameters:
    ' EventSender: Ignored.
    ' EventArgs: Ignored.
    ' Returns: Nothing.
    ' Affected Parameters: None.
    ' Local variables used: None.
    ' --------------------------------------------------------------------------
    Private Sub aControl_Leave(ByVal EventSender As System.Object, _
    ByVal EventArgs As System.EventArgs) Handles aControl.Leave

    RaiseEvent Leave(EventSender, EventArgs)

    End Sub

    ' --------------------------------------------------------------------------
    ' Name: aControl_MouseOver
    ' Comments:
    ' Written By: Chris Van Duin - 6th March, 2005.
    ' Modifications: None.
    ' Parameters:
    ' EventSender: Ignored.
    ' EventArgs: Ignored.
    ' Returns: Nothing.
    ' Affected Parameters: None.
    ' Local variables used: None.
    ' --------------------------------------------------------------------------
    Private Sub aControl_MouseOver(ByVal EventSender As System.Object, _
    ByVal EventArgs As System.EventArgs) Handles aControl.MouseHover

    RaiseEvent MouseHover(EventSender, EventArgs)

    End Sub

    ' --------------------------------------------------------------------------
    ' Name: aControl_TextChanged
    ' Comments:
    ' Written By: Chris Van Duin - 24th February, 2004.
    ' Modifications: None.
    ' Parameters:
    ' EventSender: Ignored.
    ' EventArgs: Ignored.
    ' Returns: Nothing.
    ' Affected Parameters: None.
    ' Local variables used: None.
    ' --------------------------------------------------------------------------
    Private Sub aControl_TextChanged(ByVal EventSender As System.Object, _
    ByVal EventArgs As System.EventArgs) Handles aControl.TextChanged

    RaiseEvent TextChanged(EventSender, EventArgs)

    End Sub

    #End Region

    #Region "Methods"

    ' --------------------------------------------------------------------------
    ' Name: AddElement
    ' Comments:
    ' Written By: Chris Van Duin - 20th October, 2003.
    ' Modifications: None.
    ' Parameters: None.
    ' Returns: GeneralComments.
    ' Affected Parameters: None.
    ' Local variables used:
    ' anElement:
    ' --------------------------------------------------------------------------
    Public Sub AddElement(ByVal myObject As System.Windows.Forms.Control)

    aControl = myObject
    AddHandler aControl.Leave, AddressOf aControl_Leave
    AddHandler aControl.TextChanged, AddressOf aControl_TextChanged
    AddHandler aControl.MouseHover, AddressOf aControl_MouseOver
    Me.List.Add(aControl)
    HostForm.Controls.Add(aControl)
    aControl.Tag = Me.Count

    End Sub

    ' --------------------------------------------------------------------------
    ' Name: Remove
    ' Comments: Remove the last element added to the array from
    ' the host form controls collection. Note the use
    ' of the default property in accessing the array.
    ' Written By: Chris Van Duin - 20th October, 2003.
    ' Modifications: None.
    ' Parameters: None.
    ' Returns: Nothing.
    ' Affected Parameters: None.
    ' Local variables used: None.
    ' --------------------------------------------------------------------------
    Public Sub Remove()

    ' Check to be sure there is an element to remove.
    If Me.Count > 0 Then
    HostForm.Controls.Remove(Me(Me.Count - 1))
    Me.List.RemoveAt(Me.Count - 1)
    End If

    End Sub

    #End Region

    End Class

    ' ------------------------------------------------------------------------------------
    ' Name: SetupControlArray
    ' Comments:
    ' Written By: Chris Van Duin
    ' Modifications: None.
    ' Parameters: None.
    ' Returns: Nothing.
    ' Affected Parameters: None.
    ' Local variables used:
    ' intCounter: Loop counter.
    ' CurrentLocation: System.Drawing.Point
    ' myControl: Windows.Forms.Label
    ' ------------------------------------------------------------------------------------
    Private Sub SetupControlArray()

    Dim intCounter As Integer
    Dim CurrentLocation As System.Drawing.Point
    Dim myControl As Windows.Forms.Label

    Try
    CurrentLocation = New System.Drawing.Point(74, 4)
    MyCtlSizingsArray = New FormsControlCollection(Me)
    With MyCtlSizingsArray
    For intCounter = 0 To mNUM_CONTROLS - 1
    myControl = New Windows.Forms.Label
    CurrentLocation.X += 36
    With myControl
    .Location = New System.Drawing.Point(CurrentLocation.X, _
    CurrentLocation.Y)
    .Name = "Control" & intCounter
    .Size = New System.Drawing.Size(33, 16)
    .TabIndex = 200 + intCounter
    .BackColor = System.Drawing.Color.FromArgb(CType(192, Byte), _
    CType(192, Byte), CType(255, Byte))
    .TextAlign = ContentAlignment.MiddleCenter
    .Font = New System.Drawing.Font("Microsoft Sans Serif", 8.0!, _
    System.Drawing.FontStyle.Regular)
    .Visible = False
    End With
    .AddElement(myControl)
    Me.Controls.Add(myControl)
    Next
    End With

    For intCounter = mNUM_CONTROLS To 1 Step -1
    Me.Controls.Item(Me.Controls.Count - intCounter).BringToFront()
    Next

    Catch ex As Exception
    Call HandleError(SetSQLConnection(), gNodeName, gAppName, _
    gAppVersion, Me.Name, "SetupControlArray", ex)
    End Try

    End Sub


  • SHBS

    if this is in reference to your textbox array thread....okay...you can still progmatically add the textbox to your collection as you add them to your form during runtime!

  • AGDD

    I forgot to mention that the query (parameter) varies every time, so each TextBox is created in run-time
  • SfinXx

    you have a couple of routes you can take:

    1. use a groupbox, panel or other collection control for your parent control

    2. use a custom collection class

    3. as you suggest use an existing IList or array or base collection

    HTH



  • JinFan

    I forgot to mention that the query (parameter) varies every time, so each TextBox is created in run-time
  • Sample Code