對於任何人在未來尋找這個,這裏是使用Robin代碼的完整解決方案(謝謝!),但是在運行時創建的頁面放入一個類。我只是將相關的代碼粘貼到這個問題上,用戶也可以調用CopyPage過程來在運行時添加頁面。現在用戶也可以在頁面2(索引1)和n之間左右移動它們。
在我的主要模塊:
Public arrLeftButton() As New CButton
Public arrRightButton() As New CButton
在我的CButton類模塊:
Option Explicit
Public WithEvents CopyButton As MSForms.CommandButton
Public WithEvents DeleteButton As MSForms.CommandButton
Public WithEvents MoveLeft As MSForms.CommandButton
Public WithEvents MoveRight As MSForms.CommandButton
Private Sub MoveLeft_Click()
Dim pag As MSForms.Page
Dim lngPageCount As Long
Set pag = UFmodproject.MultiPage1.SelectedItem
lngPageCount = UFmodproject.MultiPage1.Pages.Count
If pag.Index > 1 Then
pag.Index = pag.Index - 1
End If
End Sub
Private Sub MoveRight_Click()
Dim pag As MSForms.Page
Dim lngPageCount As Long
Set pag = UFmodproject.MultiPage1.SelectedItem
lngPageCount = UFmodproject.MultiPage1.Pages.Count
If pag.Index < lngPageCount - 1 Then
pag.Index = pag.Index + 1
End If
End Sub
而且我UserForm_Initialize:
Private Sub userform_initialize()
ReDim Preserve arrLeftButton(1 To 1)
ReDim Preserve arrRightButton(1 To 1)
Set arrLeftButton(1).MoveLeft = MultiPage1.Pages(1).Controls("MoveLeft1")
Set arrRightButton(1).MoveRight = MultiPage1.Pages(1).Controls("MoveRight1")
For i = 2 To GetINIString("Project", "NumberOfShipmentTypes", strINIPATH)
Call FormControls.CopyPage
Next
End Sub
然而,在其他標準模塊,所以可以從其他地方也被稱爲:
Sub CopyPage()
Dim l As Double, r As Double
Dim Ctrl As Control
Dim newCtrl As Object
Dim pCount As Long
pCount = UFmodproject.MultiPage1.Pages.Count
'[...add pages and copy all controls]
For Each newCtrl In UFmodproject.MultiPage1.Pages(pCount).Controls
If Left(newCtrl.Name, Len(newCtrl.Name) - 1) = "MoveLeft" Then
ReDim Preserve arrLeftButton(1 To pCount)
Set arrLeftButton(pCount).MoveLeft = newCtrl
End If
If Left(newCtrl.Name, Len(newCtrl.Name) - 1) = "MoveRight" Then
ReDim Preserve arrRightButton(1 To pCount)
Set arrRightButton(pCount).MoveRight = newCtrl
End If
Next
End Sub
非常感謝羅賓,完美。所以Page.Index確實存在,我只需要將頁面聲明爲一個單獨的對象,對嗎?由於我的頁面是在運行時創建的,所以我不得不修改代碼並放入一個類中。我將在解決問題的過程中發佈解決方案,希望這是適當的過程。謝謝。 –
嗨,很高興它有幫助。如果您有解決問題的代碼更新,您可以發佈自己問題的答案。這可能是一個更好的選擇,而不是改變問題。 –
@RobinMackenzie不錯:) –