Solved

How to ADD and DELETE VB.Shape controls at RunTime

Posted on 2004-04-11
8
853 Views
Last Modified: 2013-12-25
Hello, I have the following code that instantiates a Single VB.Shape control at runtime:

Dim WithEvents mShape1 As VB.Shape

Private Sub Command1_Click()
   Set mShape1 = Controls.Add("VB.Shape", "mShape1")
   mShape1.Visible = True
End Sub

This crashes when I try adding a second Shape. What I am trying to do is add as many Shapes as needed AND to delete them individually when they are no longer required.

I think I need a Control Array but not sure how to set this up.

Thnx






0
Comment
Question by:const71
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 7

Assisted Solution

by:CleffedUp
CleffedUp earned 200 total points
ID: 10804250
Heya... Try this on for size in a new form (Form1) with two Command buttons.

Dim Shapes() As Shape
Dim intShapes As Integer

Private Sub Command1_Click()
    intShapes = intShapes + 1
    ReDim Preserve Shapes(intShapes)
   
    Set Shapes(intShapes) = Form1.Controls.Add("VB.Shape", "mShape" & intShapes)
    Shapes(intShapes).Top = intShapes * 30
    Shapes(intShapes).Left = intShapes * 30
    Shapes(intShapes).Visible = True
   
End Sub

Private Sub Command2_Click()
    Form1.Controls.Remove ("mShape" & intShapes)
    intShapes = intShapes - 1
End Sub

Private Sub Form_Load()
    intShapes = 0
End Sub

0
 
LVL 7

Expert Comment

by:CleffedUp
ID: 10804257
Incidentally, the problem with your initial code was you were trying to create two objects of the same name, i.e. "mShape1".
0
 

Author Comment

by:const71
ID: 10804764
Looks good. Is there a way to get events?

0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 10805171
There are no events for the shape or line control.  They are only for adding visual components to your interface.

What is it you are trying to do with your program?

Idle_Mind
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

Author Comment

by:const71
ID: 10807253

I am creating rectangular regions representing fields on a form that can be double-clicked and resized. (Like Acrobat Forms). I can use Labels instead of Shapes if Shapes are not functional enough

0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 300 total points
ID: 10807380
To do this with the Shape control you would have to trap the events for the container of your shape controls.  You could add your shapes to a PictureBox for example by adding this line to your shape creation code:
 
    Set Shapes(intShapes).Container = Picture1

The you can trap the MouseDown event for the PictureBox and check the coordinates against those of your shapes like this:

Private Sub Picture1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    Dim s As Integer
   
    For s = 0 To UBound(Shapes)
        If X >= Shapes(s).Left And X <= Shapes(s).Left + Shapes(s).Width Then
            If Y >= Shapes(s).Top And Y <= Shapes(s).Top + Shapes(s).Height Then
                Debug.Print "Shape(" & s & ") Clicked"
                Exit Sub
            End If
        End If
    Next s
End Sub

If you don't want to mess with a control array then simply create the shapes and add them to your form like you are doing but also add a reference to your created shapes into a collection.  Then you can iterate the collection with a For Each...Next loop instead of walking an array by index.

Regards,

Idle_Mind
0
 
LVL 26

Expert Comment

by:EDDYKT
ID: 10807420
Just use control array and then you can trap the event

ie. put the shape control on the form and set index to 0.


Then you should be able to load and unload the shape control and also can handle the event
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 10807476
EDDYKT,

It doesn't matter if you add your shape controls to a control array or create them dynamically since the shape control doesn't have any events to trap and/or handle in the first place!

Idle_Mind
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now