Question how to change the button location on my settings when app is running ?

edinet

Member
Joined
May 8, 2013
Messages
22
Programming Experience
Beginner
sorry for bad english...

i have 7 buttons to an form,,and want to change the location of button and save to my settings (move to another location of form) ,, when app or form is running,,can this be done ?

buttons.png
 
Last edited:
If you want to use My.Settings to persist control properties then your first choice should be to use data-binding. You can't do so in all cases but, in those can, it's the easiest and best option. Select your control and open the Properties window, then use the (ApplicationSettings) property to bind one or more control properties to settings, creating those settings too if they don't already exist. You can then simply set the appropriate property of My.Settings and the bound control(s) will be updated automatically.
 
Here you will bind the Location property of the control to a setting.
For moving the control you can use the built in drag-drop functionality for forms. You'll need to set the forms AllowDrop property to True and handle DragEnter event and DragDrop event. To enable drag functionality for a button at runtime you can use AddHandler statement and handle MouseDown event where you start the drag operation by calling DoDragDrop method. Do some research about how to do drag-drop in general and give it a try. Very little code needed to do this.
 
i found the code...but this code just muve the button...can anyone help how to save the location on my settings

Public Class Form1
Dim x, y As Integer
Dim newpoint As New Point


Private Sub Button1_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles Button1.MouseDown
x = Control.MousePosition.X - Button1.Location.X
y = Control.MousePosition.Y - Button1.Location.Y
End Sub


Private Sub Button1_MouseMove(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles Button1.MouseMove
If e.Button = Windows.Forms.MouseButtons.Left Then
newpoint = Control.MousePosition
newpoint.X -= x
newpoint.Y -= y
Button1.Location = newpoint
Application.DoEvents()



End If
End Sub
End Class
 
The setting is automatically synchronized with the object property when you bind them together, you already know to do that. When bound it is the same whether you change the property or the setting.

I will again suggest you use the standard Drag-drop functionality to do this, especially since it will intercept the default click action that a Button control is normally designed to do.
 
johnH im i beginner,,i really don't know how to 'do' the Drag-drop functionality,,i search to the google and i don't found anything ,,do you know where to found instructions for that
 
You're taking something that's easy and trying to make it difficult. I already told you what you need to do so now all you have to do is follow instructions. Let's try it again.

1. Select your Button in the designer.
2. Open the Properties window.
3. Expand the (ApplicationSettings) node.
4. Select the (PropertyBindings) node and click the browse (...) button.

From there you can bind various properties of your control to new or existing settings. Once the binding has been created, you simply update one of the bound properties (either the control property or the My.Settings property) in code and the other will be updated automatically, which is the whole point of data-binding. That's one line of code, e.g.
Button1.Location = New Point(x, y)
 
Back
Top