Moving a control as runtime

J

jcrouse

I use the following three subs to click on a label and reposition it at
runtime:

Private Sub lblP1JoyUp_MouseDown(ByVal sender As System.Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles lblP1JoyUp.MouseDown

myMouseDown = True

mouseX = Cursor.Position.X - Me.Location.X - (lblP1JoyUp.Width / 2)

mouseY = Cursor.Position.Y - Me.Location.Y - (lblP1JoyUp.Height / 2)

End Sub



Private Sub lblP1JoyUp_MouseMove(ByVal sender As System.Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles lblP1JoyUp.MouseMove

Static LastCursor As Point

Dim NowCursor As Point = New Point(mouseX, mouseY)

If Point.op_Inequality(NowCursor, LastCursor) Then

If myMouseDown Then

lblP1JoyUp.BorderStyle = BorderStyle.FixedSingle

lblP1JoyUp.Location = New
System.Drawing.Point(Cursor.Position.X - (Me.Location.X + 4) -
(lblP1JoyUp.Width / 2), Cursor.Position.Y - (Me.Location.Y + 50) -
(lblP1JoyUp.Height / 2))

End If

LastCursor = Cursor.Position

End If

End Sub



Private Sub lblP1JoyUp_MouseUp(ByVal sender As System.Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles lblP1JoyUp.MouseUp

lblP1JoyUp.BorderStyle = BorderStyle.None

myMouseDown = False

End Sub



The problem is that if I drag it over top of another label funny things
happen. I seems somewhat randon. Sometimes the label I'm dragging will drop
and the label I moved over will be picked up instead. Sometime labels seem
to jump to another part of the screen. I am looking for a way to lock all of
the controls except for the one that I am dragging.



Thanks,

John
 
C

Cor Ligthert

Hi John,

Do you mean that you want it to bring to front in the mouse down event?

:)

Cor
 
J

jcrouse

I think I may have resolved it. i was using a global variable called
"myMouseDown". I think the problem was that all 25 label controls called the
same variable. Hence, when I drag one label over top of another label the
"myMouseDown" variable is true and the label does funny things. I created a
seperate variable for each label. I'll give it a try and see if it seemed to
help.

Thanks,

John
 
C

Cor Ligthert

Hi John,

But did you set that label1 you are using as I said
label(x).bringtofront

I think that will do a lot.

:)

Cor
 
C

Cor Ligthert

HI John,

I still not see that sentence,
Private Sub lblP1JoyUp_MouseDown(ByVal sender As System.Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles lblP1JoyUp.MouseDown
lblP1JoyUpMouseDown = True

lblP1JoyUp.bringtofront

mouseX = Cursor.Position.X - Me.Location.X - (lblP1JoyUp.Width / 2)
mouseY = Cursor.Position.Y - Me.Location.Y - (lblP1JoyUp.Height / 2)
End Sub

In you code what was in the previous message?

Cor
 
J

jcrouse

Cor....Sorry man. I didn't see your suggestion until after I thought of it
myself, which was AFTER I made the previous post. That solved the problem. I
have one other slight problem with this routine. If I move the mouse to
quickly and the cursor gets outside of the label control, the label stops
moving until you go back over top of it again. It's pretty annoying since
you have to move the mouse quite slowly while dragging. One thing I did to
help was changed the backcolor property from transparent to black. The user
can the set them to transparent once they get the label into position.
Transparent seems to REALLY slow things dow. Must be all of the additional
calcs the processor needs to perform. ANyways, an ideas on the dragging
issue?

Thanks,
John
 
C

Cor Ligthert

Hi John,

I made a complete sample, for me it does work as I expect, however do not
forget this goes through all borders etc. I did bring the sample back to
keep it readable.

(You can open a new form project, delete everything in the code past this in
and than it should go)

I hope this helps,

Cor

Public Class Form1
Inherits System.Windows.Forms.Form
Public Sub New()
MyBase.New()
End Sub
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
Private components As System.ComponentModel.IContainer
Private WithEvents Label1 As New Label
Private WithEvents Label2 As New Label
Private mouseX, mouseY As Integer
Private arLabels() As Label
Dim myMousedown As String
Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Me.ClientSize = New System.Drawing.Size(400, 400)
Label1.Name = "Label1"
Label2.Name = "Label2"
arLabels = New Label() {Label1, Label2}
Dim lblY As Integer = 100
For Each Lbl As Label In arLabels
Lbl.Location = New System.Drawing.Point(100, lblY)
Lbl.ForeColor = Color.Red
Lbl.BackColor = Color.Transparent
Lbl.TextAlign = ContentAlignment.MiddleCenter
Lbl.Text = Lbl.Location.X.ToString & "." &
Lbl.Location.Y.ToString
AddHandler Lbl.MouseDown, AddressOf Label_MouseDown
AddHandler Lbl.MouseUp, AddressOf Label_MouseUp
AddHandler Lbl.MouseMove, AddressOf Label_MouseMove
lblY += 30
Me.Controls.Add(Lbl)
Next
End Sub
Private Sub Label_MouseDown(ByVal sender As Object, ByVal _
e As System.Windows.Forms.MouseEventArgs)
Dim lbl As Label = DirectCast(sender, Label)
myMousedown = lbl.Name
lbl.BringToFront()
mouseX = Cursor.Position.X - lbl.Location.X
mouseY = Cursor.Position.Y - lbl.Location.Y
lbl.Cursor = Cursors.Hand
End Sub
Private Sub Label_MouseUp(ByVal sender As Object, ByVal e As _
System.Windows.Forms.MouseEventArgs)
Dim lbl As Label = DirectCast(sender, Label)
myMousedown = ""
lbl.Cursor = Cursors.Default
End Sub
Private Sub Label_MouseMove(ByVal sender As Object, ByVal e _
As System.Windows.Forms.MouseEventArgs)
Dim lbl As Label = DirectCast(sender, Label)
Static LastCursor As Point
Dim NowCursor As Point = New Point(Cursor.Position.X,
Cursor.Position.Y)
If Point.op_Inequality(NowCursor, LastCursor) Then
If myMousedown = lbl.Name Then
lbl.Location = New System.Drawing.Point(Cursor.Position.X _
- mouseX, Cursor.Position.Y - mouseY)
End If
LastCursor = Cursor.Position
lbl.Text = lbl.Location.X.ToString & "." &
lbl.Location.Y.ToString
End If
End Sub
End Class
I hope this helps a little bit?

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top