我正在开发一个小的Excel-VBA GUI /表单,供用户从/到.ini文件读取和写入数据。 UserForm有一个MultiPage,用户在运行时创建页面,每个页面代表一个ini部分。此外,这些部分也在主部分中编入索引以进行进一步处理:此时我循环遍历MultiPage页面以创建该索引。问题是,用户需要能够更改此索引的顺序。现在,是否可以在运行时在MultiPage中移动页面?
我正在考虑一些事情Me.MultiPage1.Pages(i).Index = i + 1
但显然这不起作用。或者,有没有办法我可以传递一个:=或类似于Multipage.Pages.Add来解决它? 如果这些都不起作用,我想我将使用MoveUp / Down按钮创建一个单独的ListBox控件。打开任何更好的解决方案。
答案 0 :(得分:2)
答案 1 :(得分:1)
对于将来寻找此问题的人来说,这是使用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