Connecting Tech Pros Worldwide Forums | Help | Site Map

dragging an unknown image control

Newbie
 
Join Date: Oct 2009
Posts: 1
#1: 3 Weeks Ago
If I place objects (i.e. image box controls containing graphics) onto a form at random during runtime, how can I then detect a button_down mouse event when I hover over one of those new controls if I don't have specific object_mousedown routines for those objects in my code? Is there a way of detecting which control a cursor is over and then passing this through to a "control-independent" mousemove subroutine, which could then be used to drag the selected imagebox control to a new location? This is a "design" style app where I want to add "x" symbols to a form during runtime (like drawing a diagram) but then randomly select one and move it to a new location on the form. The number of imagebox controls could be different each time, and selection of the one desired to move is entirely at random. Thanks.



Newbie
 
Join Date: Oct 2009
Posts: 18
#2: 2 Weeks Ago

re: dragging an unknown image control


dear,

You can select images by using the index of the imagebox and also the mouse events.

like: see also attachment

form with command1 and image1 !!! with index=0 !!!!

use code=
===========================================

Option Explicit

Dim Xold As Integer
Dim Yold As Integer

Private Sub Form_Load()
Image1(0).BorderStyle = 1
Image1(0).Visible = False
Command1.Caption = "Add picture"
End Sub

Private Sub Command1_Click()
Load Image1(Image1.UBound + 1)
Image1(Image1.UBound).Visible = True
Image1(Image1.UBound).Top = 300
Image1(Image1.UBound).Left = 60
End Sub

Private Sub Image1_MouseDown(Index As Integer, Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = vbLeftButton Then
Xold = X
Yold = Y
End If
End Sub

Private Sub Image1_MouseUp(Index As Integer, Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = vbLeftButton Then
With Image1(Index)
If (X - Xold) < Width - .Left - 300 And (Y - Yold) < Height - .Top - 600 Then _
.Move .Left + (X - Xold), .Top + (Y - Yold)
End With
End If
End Sub
===========================================
Attached Files
File Type: zip move picture.zip (1.6 KB, 3 views)
Newbie
 
Join Date: Oct 2009
Posts: 18
#3: 2 Weeks Ago

re: dragging an unknown image control


dear,

sorry but the limitations (prevent to put a picture outside the form) must be=

=========================================

Private Sub Image1_MouseUp(Index As Integer, Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim LeftNew As Integer
Dim TopNew As Integer
If Button = vbLeftButton Then
With Image1(Index)
LeftNew = .Left + (X - Xold)
TopNew = .Top + (Y - Yold)
If LeftNew < 0 Then LeftNew = 0 '§ Left limit
If TopNew < 0 Then TopNew = 0 '§ top limit
If LeftNew > Width - 120 - .Width Then LeftNew = Width - 120 - .Width '§ right limit
If TopNew > Height - 540 - .Height Then TopNew = Height - 540 - .Height '§ bottom limit
.Move LeftNew, TopNew
End With
End If
End Sub


=========================================


br,
Attached Files
File Type: zip move picture v2.zip (1.6 KB, 3 views)
Reply