Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Have A Shape Delete Itself 2

Status
Not open for further replies.

Stainnd

Programmer
Jul 9, 2001
65
US
This isn't exactly what I'm trying to do, but it's a simple example of what I'm trying to do.

There is a button on a blank workbook. When you click the button, it creates a new shape. When you click the shape, it deletes itself.

The reason I can't do this is because if I set the OnAction property of the Shape to some Sub like 'DeleteYourself', there is no way for the Sub to know which shape was clicked.

The only way I can imagine solving this is if a) somehow a Sub can know what object called it, b) you can pass an argument in the OnAction property or c) you can create new Subs with code (i.e. when the button is clicked it would create a new Sub called DeleteShapeX with X being the index number of the shape and then set that Sub to the OnAction property).

I'm pretty stumped by this one. If anyone can help I'd appreciate it.

-Mike
 
Probably ugly, but works in Excel:

Code:
Public last_click As String

Public Sub Oval1_click()
last_click = "Oval 1"
Call KillShape(last_click)
End Sub

Public Function KillShape(target As String)
ActiveSheet.Shapes(target).Delete
End Function
 
Thanks for the response.

I could be wrong but I think this requires me to create a Sub (Oval1_click) for every single shape. However these shapes are being created dynamically, so unless there is a way to dynamically create new subs, this wouldn't work.

-Mike
 
You're right. It is possible to create procedures dynamically, but it seems too messy for what you want to do.

Obviously the application knows what shape is under the cursor when you click. It would stand to reason that there is a way to get at that, but I don't know.
 
Could you maybe point me in the direction of where I could learn how to create procedures dynamically? I'd like to learn anyway.

-Mike
 



Try this...
Code:
Sub Setup()[b]
'run this in the workbook open event or otherwise
'be sure to change the SHEET OBJECT to a SPECIFIC worksheet in BOTH procedures!!!!![/b]
    Dim shp As Shape
    For Each shp In ActiveSheet.Shapes
        shp.OnAction = "DeleteThySelf"
    Next
End Sub
Sub DeleteThySelf()
    ActiveSheet.Shapes(Application.Caller).Delete
End Sub

Skip,

[glasses]Just traded in my old subtlety...
for a brand NUANCE![tongue]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top