Dynamically add buttons at runtime

A

Allan Till

I have created a vb6 COM add in for use with Excel.

I want to add a number of buttons to the menu at runtime depending
on the contents of a folder on the users hard disk.

How can I capture the events from these buttons given
that I cannot declare instances of the button objet withevents at design
time.
 
M

Michael Bauer

Am Mon, 22 Aug 2005 22:01:39 +0100 schrieb Allan Till:

There´s no problem to declare the variables without having the buttons
added.

Private WithEvents Button1 as Office.ComandBarButton
Private WithEvents Button2 as Office.ComandBarButton

Private Sub AddButtonsAtRuntime()
Set Button1=Application.ActiveExplorer.CommandBars.Controls.Add(...)
Set Button2=Application.ActiveExplorer.CommandBars.Controls.Add(...)
End Sub

Private Sub Button1_Click(ByVal Ctrl As Office.CommandBarButton,
CancelDefault As Boolean)
....
End Sub
 
A

Allan Till

But I do not know at design time how many buttons will be added (this depends
on a file on the users hard disk). I only know at runtime how many buttons
to add
therefore I cannot use this:

Private WithEvents Button1 as Office.ComandBarButton
Private WithEvents Button2 as Office.ComandBarButton

I need a method to dynamically add buttons and pick up their events in the
com add-in.
 
M

Michael Bauer

Am Tue, 23 Aug 2005 02:22:04 -0700 schrieb Allan Till:

Allan, you can move the code into a class module and create as many
instances as you need of it.

Sample:

<CButton.cls>
Private WithEvents Button1 as Office.ComandBarButton

Private Sub Button1_Click(ByVal Ctrl As Office.CommandBarButton,
CancelDefault As Boolean)
' Handle the click here or call a centralized handler
' For the centralized handler it might be interesting to know, _
which button was clicked. You can tell it by using some params
ThisOutlookSession.ButtonWasClicked "ItWasMe"
End Sub
</CButton.cls>

<ThisOutlookSession>
' Holds your class instances
Private m_coll as VB.Collection

Private Sub AddButtonsAtRuntime()
Dim oBtn as CButton
Set oBtn=New CButton
' Maybe init the object here
m_coll.Add New oBtn
End Sub

Public Sub ButtonWasClicked(SomeParameters)
....
End Sub
</ThisOutlookSession>

Please note, this is only a sample. You migth want to add methods to
remove buttons, to start them with some properties, etc.
 
A

Allan Till

That works great, thank you.

Michael Bauer said:
Am Tue, 23 Aug 2005 02:22:04 -0700 schrieb Allan Till:

Allan, you can move the code into a class module and create as many
instances as you need of it.

Sample:

<CButton.cls>
Private WithEvents Button1 as Office.ComandBarButton

Private Sub Button1_Click(ByVal Ctrl As Office.CommandBarButton,
CancelDefault As Boolean)
' Handle the click here or call a centralized handler
' For the centralized handler it might be interesting to know, _
which button was clicked. You can tell it by using some params
ThisOutlookSession.ButtonWasClicked "ItWasMe"
End Sub
</CButton.cls>

<ThisOutlookSession>
' Holds your class instances
Private m_coll as VB.Collection

Private Sub AddButtonsAtRuntime()
Dim oBtn as CButton
Set oBtn=New CButton
' Maybe init the object here
m_coll.Add New oBtn
End Sub

Public Sub ButtonWasClicked(SomeParameters)
....
End Sub
</ThisOutlookSession>

Please note, this is only a sample. You migth want to add methods to
remove buttons, to start them with some properties, etc.
 
M

Michael Bauer

Am Wed, 24 Aug 2005 01:48:02 -0700 schrieb Allan Till:


That works great, thank you.

"Michael Bauer" wrote:

Autsch, please correct the line:

m_coll.Add oBtn
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top