Moving multiple controls simultaneously

S

Sarah Marriott

Hi all,

I hope this is the correct forum for my VB.NET forms based question.

I have written an application which uses my own user controls which the user
can position dynamically on a form.
The user control has several standard controls on it (labels/comboboxes etc)
but there is space at the top allowing the user to click on the control
itself.
The code I use at the moment to allow the user to move the control is fairly
simple:


Private Sub DataSource_MouseDown(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs)
If State = WAITING Then
State = DRAGGING
Pic_X = e.X 'Pic_X and Pic_Y are declared on the form
Pic_Y = e.Y
End If

End Sub
Private Sub DataSource_MouseMove(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs)
If State = DRAGGING Then
If TypeOf sender Is ctl_Datasource Then
Dim mox As Integer
Dim moy As Integer

mox = e.X - Pic_X
moy = e.Y - Pic_Y

sender.Top = sender.top + moy
sender.Left = sender.Left + mox
End If
End If
End Sub

Private Sub DataSource_MouseUp(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs)
State = WAITING
End Sub

The controls (of which there can be over 20 on the form at any one time) are
generated dynamically when the form loads and moved into position based on
their last known position, which was stored in a database when the form was
last closed.

My main problem is that I would like the user to be able to 'drag select'
several controls and move them simultaneously. Something similar to
selecting and dragging several things around on the desktop together.This
would make organising the form so much easier!

Can anyone give me any pointers?

TIA

Sarah
 
L

Lloyd Sheen

Sarah Marriott said:
Hi all,

I hope this is the correct forum for my VB.NET forms based question.

I have written an application which uses my own user controls which the
user
can position dynamically on a form.
The user control has several standard controls on it (labels/comboboxes
etc)
but there is space at the top allowing the user to click on the control
itself.
The code I use at the moment to allow the user to move the control is
fairly
simple:


Private Sub DataSource_MouseDown(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs)
If State = WAITING Then
State = DRAGGING
Pic_X = e.X 'Pic_X and Pic_Y are declared on the form
Pic_Y = e.Y
End If

End Sub
Private Sub DataSource_MouseMove(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs)
If State = DRAGGING Then
If TypeOf sender Is ctl_Datasource Then
Dim mox As Integer
Dim moy As Integer

mox = e.X - Pic_X
moy = e.Y - Pic_Y

sender.Top = sender.top + moy
sender.Left = sender.Left + mox
End If
End If
End Sub

Private Sub DataSource_MouseUp(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs)
State = WAITING
End Sub

The controls (of which there can be over 20 on the form at any one time)
are
generated dynamically when the form loads and moved into position based on
their last known position, which was stored in a database when the form
was
last closed.

My main problem is that I would like the user to be able to 'drag select'
several controls and move them simultaneously. Something similar to
selecting and dragging several things around on the desktop together.This
would make organising the form so much easier!

Can anyone give me any pointers?

TIA

Sarah

Sarah,

You would first need a method to select the controls to move. This
could be CTL-Click etc. Then each control which was selected would be added
to a list of controls. Then in the DataSource_MouseMove method you would do
a loop thru the controls and move each one using top and left.

LS
 

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