Darren Bartrup
Well-known Member
- Joined
- Mar 13, 2006
- Messages
- 1,297
- Office Version
- 365
- Platform
- Windows
I'm hoping to get this working.
My Excel project has a number of forms each of which can be opened from a number of buttons at the top of each form (i.e. a navigation bar - clicking a button opens the target form and closes the current form).
At present each form has code similar to that below for each button:
What I'd like to have is a class that creates the buttons, automatically disables the button that refers to the current form and does everything for me.
I've got the code that creates the bar and enables/disables the button but I can't get the Click event to fire.
Usually when adding a single command button I'd add the Click event to a collection within the Declarations section of the form and it will work - this adds an instance of the class for each button.
What I'd like to do now is create all buttons within a single instance of the class and capture the click event for each button - I'd then use the value held in Tag to decide what to do after the click.
Here's where I'm up to:
The form has this code to create an instance of the class, pass the frame (Me.fmeNavigationBar) to the class and capture the click event:
The class code creates and positions the four buttons I need (I'm planning on adding the buttons in the form using something like NavigationBar.AddButton "MyCaption", "TargetFormName" but haven't got that far yet):
Can I do this all within a single class, or would I need to create another class to deal with the click event and create an instance of that within my NavigatorBar class (I hope not as I'd like to have it all contained within a single class).
Any help is greatly appreciated.
My Excel project has a number of forms each of which can be opened from a number of buttons at the top of each form (i.e. a navigation bar - clicking a button opens the target form and closes the current form).
At present each form has code similar to that below for each button:
Code:
Private Sub btnGoToOutlookStructure_Click()
Me.Hide
Unload Me
frmEmailSetUp.Show
End Sub
What I'd like to have is a class that creates the buttons, automatically disables the button that refers to the current form and does everything for me.
I've got the code that creates the bar and enables/disables the button but I can't get the Click event to fire.
Usually when adding a single command button I'd add the Click event to a collection within the Declarations section of the form and it will work - this adds an instance of the class for each button.
What I'd like to do now is create all buttons within a single instance of the class and capture the click event for each button - I'd then use the value held in Tag to decide what to do after the click.
Here's where I'm up to:
The form has this code to create an instance of the class, pass the frame (Me.fmeNavigationBar) to the class and capture the click event:
Code:
Public colNavigationBar As Collection
Private Sub UserForm_Initialize()
Dim NavigationBar As cls_NavigationBar
Set NavigationBar = New cls_NavigationBar
NavigationBar.AddNavigationBar Me.fmeNavigationBar
Set colNavigationBar = New Collection
Set colNavigationBar = NavigationBar.EventCollection
End Sub
The class code creates and positions the four buttons I need (I'm planning on adding the buttons in the form using something like NavigationBar.AddButton "MyCaption", "TargetFormName" but haven't got that far yet):
Code:
Public WithEvents btnNavigate As MSForms.CommandButton
Private fme As Frame
Private btnControl As Control
Private colEvents As Collection
Private Const BTN_WIDTH As Long = 72, BTN_HEIGHT As Long = 24, BTN_SPACING As Long = 6
Private Const BTN_ENABLED As Long = &H800080, BTN_DISABLED As Long = &HFF80FF
Private Const BTN_TEXT As Long = &HFFFFFF
Public Property Get EventCollection()
Set EventCollection = colEvents
End Property
Public Sub AddNavigationBar(FrameReference As Object)
Dim ContainerForm As Object
Dim sCaption(1 To 4) As String
Dim sTargetForm(1 To 4) As String
Dim x As Long
sCaption(1) = "Set Up"
sCaption(2) = "Sort Inbox"
sCaption(3) = "Update Team"
sCaption(4) = "Allocate Emails"
sTargetForm(1) = "frmEmailSetUp"
sTargetForm(2) = "frmSortInbox"
sTargetForm(3) = "frmTeamSetUp"
sTargetForm(4) = "frmAllocateEmails"
Set fme = FrameReference
Set ContainerForm = fme.Parent
Set colEvents = New Collection
For x = 1 To 4
Set btnControl = fme.Controls.Add("Forms.CommandButton.1", "btnNavigator_" & x)
With btnControl
.Left = (x * BTN_SPACING) + ((x - 1) * BTN_WIDTH)
.Top = BTN_SPACING
.Height = BTN_HEIGHT
.Caption = sCaption(x)
.Tag = sTargetForm(x)
.Font.Bold = True
.ForeColor = BTN_TEXT
If ContainerForm.Name = .Tag Then
.BackColor = BTN_DISABLED
.Locked = True
Else
.BackColor = BTN_ENABLED
.Locked = False
End If
End With
Set btnNavigate = btnControl
colEvents.Add btnNavigate
Next x
fme.Caption = ""
fme.Height = BTN_HEIGHT + (3 * BTN_SPACING)
fme.Width = (x * BTN_SPACING) + ((x - 1) * BTN_WIDTH) + BTN_SPACING
End Sub
Private Sub btnNavigate_Click()
MsgBox "Clicked" 'This doesn't happen.
End Sub
Can I do this all within a single class, or would I need to create another class to deal with the click event and create an instance of that within my NavigatorBar class (I hope not as I'd like to have it all contained within a single class).
Any help is greatly appreciated.