Draggable controls possible at run-time?

S

Sam Marrocco

Is is possible to make vb.net controls such as a checkbox or button
draggable while an app is running, thereby allowing the user to
"re-design" the layout of buttons on a form?
--
==================================================================
Sam J. Marrocco
Sr. Visual Effects Artist/R&D
Travelling Pictures/GTN
Inferno, Flame, Maya, All that cool stuff!
"The fact that no one understands you doesn't make you an artist."
==================================================================
 
L

lonnieh

I don't know how everybody else does it, but this is how i'd do it.
I'll have to code this in C# because I don't have a VB.net, but i'm
sure you'll be able to translate this just fine.

private int _mX; // _mX and _mY hold the
coordinates
private int _mY; // when the event _MouseDown fires

private void pictureBox1_MouseMove(object sender,
System.Windows.Forms.MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
pictureBox1.Left += e.X - _mX; //(.Left = .Left) + e.X
- _mX
pictureBox1.Top += e.Y - _mY;
}
}

private void pictureBox1_MouseDown(object sender,
System.Windows.Forms.MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
_mX = e.X;
_mY = e.Y;
}
}

looking through the MSDN cds I'd assume that the VB.net equivalent
would be...


[code:1:02070e9725]Private mX as Integer
Private mY as Integer

Private Sub pictureBox1_MouseDown(sender as Object, e As
System.Windows.Forms.MouseEventArgs) Handles
pictureBox1.MouseDown
If e.Button = MouseButtons.Left Then
mX = e.X
mY = e.Y
End If
End Sub

Private Sub pictureBox1_MouseMove(sender as Object, e As
System.Windows.Forms.MouseEventArgs) Handles
pictureBox1.MouseMove
If e.Button = MouseButtons.Left Then
pictureBox1.Left = pictureBox1.Left + e.X - mX
pictureBox1.Top = pictureBox1.Top + e.Y - mY
End If
End Sub[/code:1:02070e9725]

good luck.
 
S

Sam Marrocco

Thanks, that answers my question---I was hoping there was a property
that I could set to make controls movable, but it looks like you're
suggesting the method (I was hoping) I wouldn't have to use. Thanks, it
works great!
I don't know how everybody else does it, but this is how i'd do it.
I'll have to code this in C# because I don't have a VB.net, but i'm
sure you'll be able to translate this just fine.

private int _mX; // _mX and _mY hold the
coordinates
private int _mY; // when the event _MouseDown fires

private void pictureBox1_MouseMove(object sender,
System.Windows.Forms.MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
pictureBox1.Left += e.X - _mX; //(.Left = .Left) + e.X
- _mX
pictureBox1.Top += e.Y - _mY;
}
}

private void pictureBox1_MouseDown(object sender,
System.Windows.Forms.MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
_mX = e.X;
_mY = e.Y;
}
}

looking through the MSDN cds I'd assume that the VB.net equivalent
would be...


[code:1:02070e9725]Private mX as Integer
Private mY as Integer

Private Sub pictureBox1_MouseDown(sender as Object, e As
System.Windows.Forms.MouseEventArgs) Handles
pictureBox1.MouseDown
If e.Button = MouseButtons.Left Then
mX = e.X
mY = e.Y
End If
End Sub

Private Sub pictureBox1_MouseMove(sender as Object, e As
System.Windows.Forms.MouseEventArgs) Handles
pictureBox1.MouseMove
If e.Button = MouseButtons.Left Then
pictureBox1.Left = pictureBox1.Left + e.X - mX
pictureBox1.Top = pictureBox1.Top + e.Y - mY
End If
End Sub[/code:1:02070e9725]

good luck.

--
==================================================================
Sam J. Marrocco
Sr. Visual Effects Artist/R&D
Travelling Pictures/GTN
Inferno, Flame, Maya, All that cool stuff!
"The fact that no one understands you doesn't make you an artist."
==================================================================
 

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