Reputation: 55
I'm working on a little Excel-VBA GUI/Form for the user to read and write data from/to an .ini file. The UserForm has a MultiPage in which the user creates pages at runtime and each page will represent an ini section. Furthermore, these sections are also indexed in a master section for further processing: at this point I'm looping through the MultiPage pages to create this index. Problem is, the user needs to be able to change the order of this index. Now, is it possible to move the pages within a MultiPage at runtime? I was thinking something to the effect of
Me.MultiPage1.Pages(i).Index = i + 1
but obviously that doesn't work. Alternatively, is there a way I can pass a before:= or anything similar to Multipage.Pages.Add to work around it? If none of this works, I think I'll create a seperate ListBox control with MoveUp/Down buttons. Open for any better solutions.
Upvotes: 1
Views: 2718
Reputation: 55
For anybody looking for this in the future, here's the complete solution using Robin's code (Thanks!), but put in a Class for the pages created at runtime. I'm only pasting the relevant code to this question, the CopyPage procedure can also be called by the user to add pages during runtime. Now the user can also move them left and right between pages 2 (index 1) and n.
In my main module:
Public arrLeftButton() As New CButton
Public arrRightButton() As New CButton
In my CButton Class Module:
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
And my 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
Yet in another standard module, so it can be called from elsewhere too:
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
Upvotes: 1
Reputation: 19319
Say you have a UserForm
that looks like this:
Then you can include the sample code below to move the order of the Page
items of the MultiPage
:
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
Upvotes: 2