对于任何人在未来寻找这个,这里是使用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不错:) –