Solved

How to ADD and DELETE VB.Shape controls at RunTime

Posted on 2004-04-11
8
854 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
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…

895 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

12 Experts available now in Live!

Get 1:1 Help Now