[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How to ADD and DELETE VB.Shape controls at RunTime

Posted on 2004-04-11
8
Medium Priority
?
882 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 800 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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 1200 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
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…
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…
Suggested Courses

830 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