Moving toolbox

N

nime

Hello

I made a toolbox like thing by using a panel with a label docked on top.
I can move the toolbox by dragging on the label. I've coded it, VB6 style
and got no problem. Then I tried to use new VB.NET way and now I'm stuck.
If you examine Label1_MouseMove event you'll see both methods.
Can anybody fix it?

To execute the code:
1-) Add a panel: Panel1
2-) Add a label into panel (Label1)
Then run...

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Public Class Form1

Private Structure MoveStructure
Public MouseOffset As Point
Public MouseDown As Boolean
End Structure
Private Move As MoveStructure

Private Sub Label1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Label1.MouseDown
Move.MouseDown = True
Move.MouseOffset = New Point(e.X, e.Y)
End Sub

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


If e.Button = Windows.Forms.MouseButtons.Left Then
If Not Move.MouseDown Then Exit Sub

''New way
'Dim Dif As Point = New Point(e.X, e.Y)
'Dif.Offset(Move.MouseOffset)
'Panel1.Location = Dif
'Move.MouseOffset = Dif
'Exit Sub


'Old way
Dim DifX As Integer = Move.MouseOffset.X - e.X
Dim DifY As Integer = Move.MouseOffset.Y - e.Y

With Panel1
.Top = .Top - DifY
.Left = .Left - DifX
End With


End If
End Sub

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
With Label1
.AutoSize = False
.BorderStyle = BorderStyle.FixedSingle
.BackColor = System.Drawing.SystemColors.ActiveCaption
.ForeColor = System.Drawing.SystemColors.ActiveCaptionText
.Dock = DockStyle.Top
End With
Panel1.BorderStyle = BorderStyle.FixedSingle
End Sub
End Class
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
 
G

gene kelley

Hello

I made a toolbox like thing by using a panel with a label docked on top.
I can move the toolbox by dragging on the label. I've coded it, VB6 style
and got no problem. Then I tried to use new VB.NET way and now I'm stuck.
If you examine Label1_MouseMove event you'll see both methods.
Can anybody fix it?

To execute the code:
1-) Add a panel: Panel1
2-) Add a label into panel (Label1)
Then run...

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Public Class Form1

Private Structure MoveStructure
Public MouseOffset As Point
Public MouseDown As Boolean
End Structure
Private Move As MoveStructure

Private Sub Label1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Label1.MouseDown
Move.MouseDown = True
Move.MouseOffset = New Point(e.X, e.Y)
End Sub

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


If e.Button = Windows.Forms.MouseButtons.Left Then
If Not Move.MouseDown Then Exit Sub

''New way
'Dim Dif As Point = New Point(e.X, e.Y)
'Dif.Offset(Move.MouseOffset)
'Panel1.Location = Dif
'Move.MouseOffset = Dif
'Exit Sub


'Old way
Dim DifX As Integer = Move.MouseOffset.X - e.X
Dim DifY As Integer = Move.MouseOffset.Y - e.Y

With Panel1
.Top = .Top - DifY
.Left = .Left - DifX
End With


End If
End Sub

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
With Label1
.AutoSize = False
.BorderStyle = BorderStyle.FixedSingle
.BackColor = System.Drawing.SystemColors.ActiveCaption
.ForeColor = System.Drawing.SystemColors.ActiveCaptionText
.Dock = DockStyle.Top
End With
Panel1.BorderStyle = BorderStyle.FixedSingle
End Sub
End Class
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
To simply move a panel via a top-docked label:

Private StartPoint as Point


Private Sub Label1_MouseDown(ByVal sender As Object, ByVal e _
As System.Windows.Forms.MouseEventArgs) Handles Label1.MouseDown
StartPoint = New Point(e.X, e.Y)

End Sub

Private Sub Label1_MouseMove(ByVal sender As Object, ByVal e _
As System.Windows.Forms.MouseEventArgs) Handles Label1.MouseMove
If e.Button = Windows.Forms.MouseButtons.Left Then
Dim mousePos As Point = New Point(e.X, e.Y)
Panel1.Location = New Point(Panel1.Left + (mousePos.X - StartPoint.X), _
Panel1.Top + (mousePos.Y - StartPoint.Y))

End If

End Sub


You may want to expand the example to restrict Panel1 to the forms ClientRectangle.

Gene
 
N

nime

That's what I did succesfully : )
I just tried to use .Offset method instead of calculating a few things...
 

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