Want to capture the OLEOBJECTS button Ids/Name on their DblClick event

archanasinghvi

New Member
Joined
Dec 15, 2011
Messages
6
Hi,
I have one requirement in which i have dynamically created oleobjects button through COMMAND BUTTON CLASS. Now what i want is i want to change their basic properties like backColor, Font etc through Modal form.
so i want to handle their DoubleClick event where i can get the Name of the buttons that be unique and then i can set the properties.
the thing is how can i get the Ids/Name of each button that is double clicked. as there is no argument for it.
I have written this code for dynAMICALLY GENERATING BUTTONS:


Sub AddButtonAndCode()
Try


act = Globals.ThisAddIn.Application.ActiveSheet
' Declare variables
Dim i As Long = 0, Hght As Long = 27
Dim Name As String, NName As String
'Dim oleObject As Excel.OLEObject
' Set the button properties
Hght = 305.25
' Set the name for the button
NName = "cmdAction" & i
' Test if there is a button already and if so, increment its name
Dim entry As KeyValuePair(Of String, Microsoft.Vbe.Interop.Forms.CommandButton)
If listOLEControls.Count > 0 Then
For Each entry In listOLEControls
If Microsoft.VisualBasic.Left(entry.Key, 9) = "cmdAction" Then
Name = Microsoft.VisualBasic.Right(entry.Key, Len(entry.Key) - 9)
If Name >= i Then
i = Name + 1
End If
End If
Next
Hght = entry.Value.Top + 27
End If
NName = "cmdAction" & i
' Add button
Dim myCmdObj As Microsoft.Office.Interop.Excel.OLEObject
myCmdObj = act.OLEObjects.Add(ClassType:="Forms.CommandButton.1", _
Link:=False, DisplayAsIcon:=False, Left:=52.5, Top:=Hght, _
Width:=102.5, Height:=26.25)
' Define buttons name
myCmdObj.Name = NName

' Define buttons caption
' myCmdObj.Object.Caption = "Click for action"
'myCmdObj.d
myCmdObj.Object.Caption = NName


btn = myCmdObj.Object
' btn.DesignMode = True

AddHandler btn.MouseDown, AddressOf btn_MouseDown



AddHandler btn.MouseMove, AddressOf btn_MouseMove


listOLEControls.Add(NName, btn)
btn = Nothing
myCmdObj = Nothing
Catch ex As Exception

End Try
End Sub
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
What do btn_mousemove and btn_mousedown look like?
 
Upvote 0
What do u mean by looks like..

I want to handle their events and have written the handler ..liek

Private Sub btn_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
MsgBox(
"ButtonInt" & Button)
' MsgBox("Button" & Button)
End Sub

here i want to capture the Name of the button to whom mouse is downed?

so i can open the Property diallog box and can set the property of only that button
 
Upvote 0
I'm afraid that .Net is not really my area, so I'm not sure if what you want is even possible, but in VBA you would create a class with a commandbutton variable declared withevents and then for each button you add, you would create an instance of the class, assign your created button to its button variable, then add those class instances to a collection to keep them in scope. You can then use the button_click event in the class and refer to the button variable directly when you need to access its properties.

Having said all that, you appear to simply be recreating the built in properties and design mode buttons that already exist in Excel - can you not use those?

I would also note that this is not really a .Net forum - you might have better luck in a forum aimed specifically at .Net, and more specifically VSTO.
 
Upvote 0
I'm afraid that .Net is not really my area, so I'm not sure if what you want is even possible, but in VBA you would create a class with a commandbutton variable declared withevents and then for each button you add, you would create an instance of the class, assign your created button to its button variable, then add those class instances to a collection to keep them in scope. You can then use the button_click event in the class and refer to the button variable directly when you need to access its properties.

Having said all that, you appear to simply be recreating the built in properties and design mode buttons that already exist in Excel - can you not use those?

I would also note that this is not really a .Net forum - you might have better luck in a forum aimed specifically at .Net, and more specifically VSTO.
 
Upvote 0
In VBA, the class would be as simple as:
Code:
Option Explicit

Public WithEvents btn As MSForms.CommandButton

Private Sub btn_Click()
   MsgBox btn.Name
End Sub

and then when adding the buttons to the sheet, you do something like:
Declarations section of module:
Code:
dim colHandlers as Collection

then in your loop:

Code:
Dim objHandler as CLass1

set objHandler = New Class1
set objHandler.btn = myCmdObj.Object
colHandlers.Add objHandler

I don't really know what forums to suggest - but you could try MSDN or XTremeVBTalk
 
Upvote 0

Forum statistics

Threads
1,215,054
Messages
6,122,895
Members
449,097
Latest member
dbomb1414

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top