Dynamic Queries and Dynamic Allocation of Controls

Download a sample VB program that creates a dynamic query and dynamically allocated controls.

Instructions for Creating the Example:

  1. Start Visual Basic from the Start menu.
  2. Select Standard EXE, and click Open.
  3. We are first going to add some more widgets/controls to our toolbox.  Go to the Project menu then Components.  Select Microsoft Windows Common Controls 6.0 and click OK.

     

  4. On your form, draw two frames .  Inside the first frame, draw four checkboxes .  Inside the second frame, draw one slider bar .  
  5. Also draw one large picture box and inside of it draw one shape .

  1. Name your four checkboxes: chkRed, chkBlue, chkGreen, and chkYellow.
  2. Name your slider bar, sliSize
  3. Name your picture box, picMap
  4. Name your shape, shpObject, and set its shape property to Circle.  Also set its Index property to be 0 and its visible property to False.
  5. At the very top of your code window, add the following code to setup an array containing shape information.  This array will be parallel to an array of shapes that our code will create.

Private Type ShapeObj
    Color As Integer
    X As Integer
    Y As Integer
    Size As Integer
End Type

Dim ShapeArray(200) As ShapeObj

 

  1. Now add the following code to Form_Load to place some circles in our picture box at random locations with random colors and sizes. 

Private Sub Form_Load()
    ' prepare random number generation
    Randomize

    ' we'll load 200 shapes in the picture box to run our dynamic query on
    For i = 1 To 200

        ' load a new shape control
        Load shpObject(i)
        ' make the shape visible
        shpObject(i).Visible = True

        ' position it randomly in the picture box
        shpObject(i).Left = Int(Rnd * picMap.Width)
        shpObject(i).Top = Int(Rnd * picMap.Height)

        ' pick its color randomly
        Dim Color As Integer
        Color = Int(Rnd * 4)
        Select Case Color
            Case 0: shpObject(i).BackColor = vbRed
            Case 1: shpObject(i).BackColor = vbBlue
            Case 2: shpObject(i).BackColor = vbGreen
            Case 3: shpObject(i).BackColor = vbYellow
        End Select

        ' pick its size randomly
        shpObject(i).Width = Int(Rnd * 200) + 50
        shpObject(i).Height = shpObject(i).Width

        ' place shape information in our array of objects
        ShapeArray(i).Color = Color
        ShapeArray(i).Size = shpObject(i).Width
        ShapeArray(i).X = shpObject(i).Left
        ShapeArray(i).Y = shpObject(i).Top

        ' we could just use our shpObject array but this will make
        ' it easy to extend the example to less trivial programs 

    Next i

    ' make sure every object should be visible by checking all checkboxes and setting slider up
    sliSize.Max = 250
    sliSize.Value = sliSize.Max
    chkRed.Value = 1
    chkBlue.Value = 1
    chkGreen.Value = 1
    chkYellow.Value = 1

End Sub

  1. Add this code to Form_Unload to free the memory we allocated when creating shape controls in Form_Load:

Private Sub Form_Unload(Cancel As Integer)
    For i = 1 To 200
        Unload shpObject(i)
    Next i
End Sub

 

  1. Type this new sub-routine in your code window, but outside any other sub-routines.  This sub-routine will perform our dynamic query.  If a shape matches all of the search criteria it will be visible, if not, it will be invisible.

Private Sub runQuery()
' the dynamic query

    ' check all shapes to see if they match the search criteria

    For i = 1 To 200

        ' check the size of the shape
        If ShapeArray(i).Size < sliSize.Value Then


            ' check the color of the shape
            If (chkRed.Value = 0 And ShapeArray(i).Color = 0) Or _
                (chkBlue.Value = 0 And ShapeArray(i).Color = 1) Or _
                (chkGreen.Value = 0 And ShapeArray(i).Color = 2) Or _
                (chkYellow.Value = 0 And ShapeArray(i).Color = 3) Then
                shpObject(i).Visible = False
            Else
                shpObject(i).Visible = True
            End If
        Else
            shpObject(i).Visible = False
        End If
    Next
End Sub

  1. Call the dynamic query every time a search control is used:

Private Sub chkBlue_Click()
    runQuery
End Sub

Private Sub chkGreen_Click()
    runQuery
End Sub

Private Sub chkRed_Click()
    runQuery
End Sub

Private Sub chkYellow_Click()
    runQuery
End Sub


Private Sub sliSize_Scroll()
    runQuery
End Sub