Solved

How to ADD and DELETE VB.Shape controls at RunTime

Posted on 2004-04-11
8
865 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
LVL 86

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
 

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 86

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 86

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

726 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