Allow users to rearrange the form

  • Thread starter Thread starter Eric Sabine
  • Start date Start date
E

Eric Sabine

For a certain app, I'd like the user to be able to move buttons around (I
mean physically move them). The reason isn't really important, but it has
to do with an organizational layout. Anyway, it would be cool if the user
could click a "mode" button which almost makes a portion of the form in
"design view" (showing a grid would be cool too) and lets them move the
buttons around. Seeing code-behind is not what I want here. Just moving
them around.

In a form event, I would then capture the new coordinates for all of the
buttons and save them to a database or XML. Then at next form load, replay
the new locations.

Any hints?

thanks,
Eric
 
You can do this by setting a variable to detect when you're in 'design'
mode and then catching the mouse down event to select which button to move
and then catching the form_mousemove event to change the location of the
button. here's a quick example.

open a new windows app and add 1 checkbox and 1 button. then add the
following code. Run it. Click the checkbox then click the button and move
the mouse around. Click the mouse again and the button stays where you
clicked:

Public down As Boolean = False
Public ReadyToMove As Boolean = False
Public ButtonToMove As Button
Private Sub CheckBox1_CheckedChanged(ByVal sender As Object, ByVal e As
System.EventArgs) Handles CheckBox1.CheckedChanged
'if you're in design mode
If CheckBox1.Checked = True Then
down = True
Else
down = False
ReadyToMove = False
End If

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
If down = True Then
ButtonToMove = Button1
ReadyToMove = True
End If
End Sub
Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseMove
If down And ReadyToMove Then
ButtonToMove.Location = New Point(Cursor.Current.Position.X -
Form1.ActiveForm.Location.X, Cursor.Current.Position.Y -
Form1.ActiveForm.Location.Y)
End If
End Sub


Private Sub Form1_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Click
If down And ReadyToMove Then
ReadyToMove = False
End If
End Sub
 
Eric Sabine said:
For a certain app, I'd like the user to be able to move buttons
around (I mean physically move them). The reason isn't really
important, but it has to do with an organizational layout. Anyway,
it would be cool if the user could click a "mode" button which almost
makes a portion of the form in "design view" (showing a grid would be
cool too) and lets them move the buttons around. Seeing code-behind
is not what I want here. Just moving them around.

In a form event, I would then capture the new coordinates for all of
the buttons and save them to a database or XML. Then at next form
load, replay the new locations.

Any hints?

In addition to Steve's answer, another suggestion:
When the user switches to "design mode", disable the form and put another
form on top of it at the same location. This second form can be used as a
"designer". It doesn't contain controls but it handles the mouse events to
move the controls. It does not really contain controls, but paints
rectangles instead of the controls. You could pass the first form to the
second, and the second can take all control information from the first.
 
Very cool! thanks

Steve DuMosch said:
You can do this by setting a variable to detect when you're in 'design'
mode and then catching the mouse down event to select which button to move
and then catching the form_mousemove event to change the location of the
button. here's a quick example.

open a new windows app and add 1 checkbox and 1 button. then add the
following code. Run it. Click the checkbox then click the button and move
the mouse around. Click the mouse again and the button stays where you
clicked:

Public down As Boolean = False
Public ReadyToMove As Boolean = False
Public ButtonToMove As Button
Private Sub CheckBox1_CheckedChanged(ByVal sender As Object, ByVal e As
System.EventArgs) Handles CheckBox1.CheckedChanged
'if you're in design mode
If CheckBox1.Checked = True Then
down = True
Else
down = False
ReadyToMove = False
End If

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
If down = True Then
ButtonToMove = Button1
ReadyToMove = True
End If
End Sub
Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseMove
If down And ReadyToMove Then
ButtonToMove.Location = New Point(Cursor.Current.Position.X -
Form1.ActiveForm.Location.X, Cursor.Current.Position.Y -
Form1.ActiveForm.Location.Y)
End If
End Sub


Private Sub Form1_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Click
If down And ReadyToMove Then
ReadyToMove = False
End If
End Sub
 
Thanks too Armin


Armin Zingler said:
In addition to Steve's answer, another suggestion:
When the user switches to "design mode", disable the form and put another
form on top of it at the same location. This second form can be used as a
"designer". It doesn't contain controls but it handles the mouse events to
move the controls. It does not really contain controls, but paints
rectangles instead of the controls. You could pass the first form to the
second, and the second can take all control information from the first.
 
Back
Top