Skip to main content

Past Blast

Featured Products

Windows Mobile Developer Controls
Windows Mobile Developer Controls
Stay in touch using the DEVBUSS RSS feeds.
 

News

Windows Mobile Developer Controls
Sapphire Soltuions

Simulating Dynamic Control Creation in eVB

Written by Robert Levy  [author's bio]  [read 38641 times]
Edited by Derek

Download the code

Page 1  Page 2 

' Handle the click event of all checkboxes here.
' We know which control this came from based on
' the passed parameter.
Private Sub Check_Click(index As Integer)

' [ We will implement this later ]

End Sub

Private Sub Form_Load()
' Populate the fake control array
Set Check(0) = Check0
Set Check(1) = Check1
Set Check(2) = Check2
Set Check(3) = Check3
Set Check(4) = Check4
Set Check(5) = Check5
Set Check(6) = Check6
Set Check(7) = Check7
' [Place additional Form_Load code here]
End Sub

Next, let's add code to Form_Load that will ask the user how many checkbox fields should be displayed and creates an array of the value associated with each of these fields.

' Ask how many fields to display
fields = CInt(InputBox("How many controls?", _
"Dynamic Control Simulation", 20))
' Resize the 'values' array to hold
' a value for each field
ReDim values(fields)

We will be altering the visible, caption, and value property of the textboxes through a sub routine called "UpdateView", so add the following to your Form_Load and Scroll_Change methods:

' Update the UI
UpdateView

Now, we must implement UpdateView. Given that we know the number of controls on the form and the position of the scroll bar, we can easily pull information from the right place in our "values" array and configure the checkboxes accordingly.

Of course, the user may have scrolled to a point where not all of controls have associated values. When this happens, an "index out of bounds" error occurs when we look in the "values" array. The trick is to catch this error and make the unused controls invisible:

Private Sub UpdateView()
' For each viewable field in record mode
Dim f As Integer
Dim val As Boolean
For f = 0 To 7
' See if there is a value for this field number.
' If not, an "index out of bounds" error will
' be generated.
On Error Resume Next
val = values(Scroll.Value + f)
' If there is no field here
If Err.Number <> 0 Then
' hide the checkbox
Check(f).Visible = False
' If there is a field
Else
' Display its value on the form
Check(f).Value = val
' Give it an appropriate caption
Check(f).Caption = "Control #" & _
CStr(Scroll.Value + f)
' Make sure its visible
Check(f).Visible = True
End If
Next
End Sub

Lastly, we will need to update the "values" array when the user changes one of the controls. This is easily done by implementing our Check_Change method with a single line of code:

values(Scroll.Value + index) = Check(index).Value

This should be sufficient to get you started. There are obviously many things you could do to improve upon this, but I'll leave that to your imagination and the kind people in the deVBuzz forums.

Previous Page