Excel-VBA MultiPage:运行时 move/reorder/index 页?
Excel-VBA MultiPage: move/reorder/index pages at runtime?
我正在研究一些 Excel-VBA GUI/Form 供用户读写数据 from/to .ini 文件。 UserForm 有一个 MultiPage,用户在运行时在其中创建页面,每个页面将代表一个 ini 部分。此外,这些部分还在主部分中编入索引以供进一步处理:此时我循环遍历 MultiPage 页面以创建此索引。问题是,用户需要能够更改此索引的顺序。现在,是否可以在运行时在 MultiPage 中移动页面?我在想
的效果
Me.MultiPage1.Pages(i).Index = i + 1
但显然那是行不通的。或者,有没有一种方法可以通过 before:= 或任何类似于 Multipage.Pages.Add 的方法来解决它?
如果 none 有效,我想我会创建一个带有 MoveUp/Down 按钮的单独的 ListBox 控件。打开任何更好的解决方案。
假设您的 UserForm
看起来像这样:
然后您可以包含下面的示例代码来移动 MultiPage
的 Page
项的顺序:
Option Explicit
'moves selected page to left
Private Sub CommandButton1_Click()
Dim pag As MSForms.Page
Dim lngPageCount As Long
' get reference to page
Set pag = Me.MultiPage1.SelectedItem
' get number of pages in multipage
lngPageCount = Me.MultiPage1.Pages.Count
' check if trying to go left beyond first page and put to end
' otherwise decrement pages position in multipage
If pag.Index = 0 Then
pag.Index = lngPageCount - 1
Else
pag.Index = pag.Index - 1
End If
' update caption
Me.Label1.Caption = pag.Name & " is at index " & pag.Index
End Sub
'moves selected page to right
Private Sub CommandButton2_Click()
Dim pag As MSForms.Page
Dim lngPageCount As Long
' get reference to page
Set pag = Me.MultiPage1.SelectedItem
' get number of pages in multipage
lngPageCount = Me.MultiPage1.Pages.Count
' check if trying to go right beyond number of pages and put to start
' otherwise increment pages position in multipage
If pag.Index = lngPageCount - 1 Then
pag.Index = 0
Else
pag.Index = pag.Index + 1
End If
' update caption
Me.Label1.Caption = pag.Name & " is at index " & pag.Index
End Sub
对于将来寻找此内容的任何人,这里是使用 Robin 代码的完整解决方案(谢谢!),但为在运行时创建的页面添加 Class。我只是把相关代码贴到这个问题上,用户也可以在运行时调用CopyPage过程来添加页面。现在用户还可以在第 2 页(索引 1)和第 n 页之间左右移动它们。
在我的主模块中:
Public arrLeftButton() As New CButton
Public arrRightButton() As New CButton
在我的 CButton Class 模块中:
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
我正在研究一些 Excel-VBA GUI/Form 供用户读写数据 from/to .ini 文件。 UserForm 有一个 MultiPage,用户在运行时在其中创建页面,每个页面将代表一个 ini 部分。此外,这些部分还在主部分中编入索引以供进一步处理:此时我循环遍历 MultiPage 页面以创建此索引。问题是,用户需要能够更改此索引的顺序。现在,是否可以在运行时在 MultiPage 中移动页面?我在想
的效果Me.MultiPage1.Pages(i).Index = i + 1
但显然那是行不通的。或者,有没有一种方法可以通过 before:= 或任何类似于 Multipage.Pages.Add 的方法来解决它? 如果 none 有效,我想我会创建一个带有 MoveUp/Down 按钮的单独的 ListBox 控件。打开任何更好的解决方案。
假设您的 UserForm
看起来像这样:
然后您可以包含下面的示例代码来移动 MultiPage
的 Page
项的顺序:
Option Explicit
'moves selected page to left
Private Sub CommandButton1_Click()
Dim pag As MSForms.Page
Dim lngPageCount As Long
' get reference to page
Set pag = Me.MultiPage1.SelectedItem
' get number of pages in multipage
lngPageCount = Me.MultiPage1.Pages.Count
' check if trying to go left beyond first page and put to end
' otherwise decrement pages position in multipage
If pag.Index = 0 Then
pag.Index = lngPageCount - 1
Else
pag.Index = pag.Index - 1
End If
' update caption
Me.Label1.Caption = pag.Name & " is at index " & pag.Index
End Sub
'moves selected page to right
Private Sub CommandButton2_Click()
Dim pag As MSForms.Page
Dim lngPageCount As Long
' get reference to page
Set pag = Me.MultiPage1.SelectedItem
' get number of pages in multipage
lngPageCount = Me.MultiPage1.Pages.Count
' check if trying to go right beyond number of pages and put to start
' otherwise increment pages position in multipage
If pag.Index = lngPageCount - 1 Then
pag.Index = 0
Else
pag.Index = pag.Index + 1
End If
' update caption
Me.Label1.Caption = pag.Name & " is at index " & pag.Index
End Sub
对于将来寻找此内容的任何人,这里是使用 Robin 代码的完整解决方案(谢谢!),但为在运行时创建的页面添加 Class。我只是把相关代码贴到这个问题上,用户也可以在运行时调用CopyPage过程来添加页面。现在用户还可以在第 2 页(索引 1)和第 n 页之间左右移动它们。
在我的主模块中:
Public arrLeftButton() As New CButton
Public arrRightButton() As New CButton
在我的 CButton Class 模块中:
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