View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Peter T Peter T is offline
external usenet poster
 
Posts: 5,600
Default Question for Peter T - Copy Paste controls at runtime

''I assume you have addressed how to add new event code to the pasted
controls. ''


I first read that as you asking me but tracking down the thread I see I
posed it to the OP
http://tinyurl.com/ddy3e

Indeed you'd need to add and set withevents (w/e) classes for each control
type, with the class objects added to a module or global level collection
(could be an array). In the form's initialize event set for existing pages &
controls.

After pasting the page with the newly 'copied' controls, add w/e classes for
the new controls with whatever other properties (eg page name/no, control
id, etc) with similar code as used in the init event.

The event code might adopt a Select Case approach for both for the
control name & page index.

Bear in mind not all controls expose the full set of events in a w/e class
as
are available in a userform.

I recall the shifting controls problem and thought I found a way to prevent
that, not sure now. Otherwise store the left & top properties of controls on
the page to be copied in a 2D array and reapply same (in array order) to the
new controls. If controls are in a frame would only need to reset the
frame's position.

Revert back if you get stuck.

Regards,
Peter T

"Geoff" wrote in message
...
I would appreciate further comment re your solution to copy pasting

controls
and your code:

Private Sub CommandButton1_Click()
Dim newPage As Page
Dim nPages As Long
With Me.MultiPage1
nPages = .Count
Set newPage = .Pages.Add("Page" & (nPages + 1), _
"Address " & (nPages + 1), nPages)
.Pages(1).Controls.Copy
End With
newPage.Paste
End Sub

''I assume you have addressed how to add new event code to the pasted
controls. ''

I have 2 questions
1. Using the above, I find each control is pasted to the left of the
original position. They are alll drawn relative to each other just

shifted
to the left. How would you correct this?
2. How do you sub-class the event code for each control collection?

I would be very grateful if you could expand a little on your previous
comments

Geoff

Code as follows:

For a cmdbutton on a page I have in the form code:
Private Sub cmdDeleteChanges_Click()
'''clear controls
ClearControls (MultiSetasides.Value)
'''set focus on first empty control
FirstControl (MultiSetasides.Value)
End Sub
Then in a standard module I have:
Sub FirstControl(k As Byte)

Dim i As Byte

'''ignore main page
If Not frmMulti.MultiSetasides.Value = 0 Then
Set ctrl = frmMulti.MultiSetasides.Pages(k).Controls
For i = 0 To 9
If (TypeOf ctrl.Item(i) Is msForms.TextBox) Then
If Trim(ctrl.Item(i).Text) = "" Then Exit For
ElseIf (TypeOf ctrl.Item(i) Is msForms.OptionButton) Then
If ctrl.Item(i).Value = False And ctrl.Item(i + 1).Value =
False Then Exit For
End If
Next i
ctrl.Item(i).SetFocus
End If
End Sub

Sub ClearControls(k As Byte)
Dim i As Byte
'''ignore main page
If Not frmMulti.MultiSetasides.Value = 0 Then
Set ctrl = frmMulti.MultiSetasides.Pages(k).Controls
For i = 1 To 9
If (TypeOf ctrl.Item(i) Is msForms.TextBox) Then
ctrl.Item(i).Text = ""
ElseIf (TypeOf ctrl.Item(i) Is msForms.OptionButton) Then
ctrl.Item(i).Value = False
End If
Next i
End If
End Sub