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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Move Objects During RunTime 1

Status
Not open for further replies.

mmbsports

Programmer
Oct 25, 2005
22
US
Thanks in advance for your assistance.

ITEM 1. Allow a user to click on a picture (icon) that is on a form and drag it to a diffent location on the same form.

ITEM 2. If I have 5 pictures (or icons) on a form, I want the user to be able to move them, delete them, or add more pictures or icons on the same form.

I am using VB6.

Again, Thanks!!
 
To move pictures, assuming you have a picMyPicture picture object:

Code:
Option Explicit

Dim sngX As Single
Dim sngY As Single

Private Sub picMyPicture_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
   sngX = X
   sngY = Y
End Sub

Private Sub picMyPicture_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
   Dim sngNewX As Single
   Dim sngNewY As Single
   
   'move only on left button
   If Button = 1 Then
      With picMyPicture
         sngNewX = .Left
         sngNewY = .Top
         If (X - sngX + .Left >= 0) Then sngNewX = X - sngX + .Left
         
         If (Y - sngY + .Top >= 0) Then sngNewY = Y - sngY + .Top
         
         .Move sngNewX, sngNewY
      End With
   End If
End Sub

-Max
 
To add or delete controls during runtime:

-Add a control to the form, set its index to 0.
-Declare a control in code.
-Set that it to the desired control with an index not yet on the form.
-Load the control.
-Position it
-Show it

example:
Code:
   Dim ctr As Control
   Set ctr = picMyPicture(1)
   Load ctr
   ctr.Move 100, 100
   ctr.Visible = True

The problem is making sure that the control index you are using is not loaded already. Also, to delete the control, just use the Unload: Unload picMyPicture(1).

Note: the original control cannot be unloaded (Unload picMyPicture(0) will give you an error).

-Max
 
That is just way too fast!

I am having a problem, but it is on my end.

You code is very tight, and I know it works!

I'll give you more feedback as soon as I get something figured out.

Till then, THANKS!!
 
I figured out what I was doing wrong. I was doing an array and didn't call it correctly.

Your code works PERFECTLY!

Thanks!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top