Okay so I've been running into trouble, I've figured out the majority of it. However, I can't seem to figure out how to save the location of a button. I could do it my way but my way is really ghetto and not recommended. The way I would do it is to go into settings, then set up an X integer and a Y integer to store the location of the object that was moved, so that the user can go back and re-open the program and the button be in it's new place. I also have an error with my algorithm for moving the control, for right now it's just experimental and follows the mouse on mouse move. Eventually I will put it on mouse down. But for now, I want the button to jump back to original position if it crosses a set line on the form. Well that much I have working. I can't figure out how to move the button around afterward.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace DLLOpen
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public const int buttonDown = 0xA1;
        public const int caption = 0x2;
        public int rotation;

        [DllImportAttribute("user32.dll")]
        public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
        [DllImportAttribute("user32.dll")]
        public static extern bool ReleaseCapture();

        protected override void OnMouseMove(MouseEventArgs mouseEv)
        {
            int mouseX = mouseEv.X;
            int mouseY = mouseEv.Y;

            int myMouseX = button1.Location.X - 10;
            int myMouseY = button1.Location.Y - 10;

            label1.Text = "Mouse X: " + myMouseX.ToString();
            label2.Text = "Mouse Y: " + myMouseY.ToString();

            if (rotation == 0)
            {
                button1.Location = new System.Drawing.Point(mouseX + 10, mouseY + 10);

                if (mouseEv.Y <= 360)
                {
                    rotation = 0;
                    button1.Location = new System.Drawing.Point(mouseX + 10, mouseY + 10);
                    label1.Text = "Mouse X: " + myMouseX.ToString();
                    label2.Text = "Mouse Y: " + myMouseY.ToString();
                }

                else if (mouseEv.Y >= 360)
                {
                    button1.Location = new Point(215, 165);
                    rotation = 2;

                    label1.Text = "Mouse X: " + myMouseX.ToString();
                    label2.Text = "Mouse Y: " + myMouseY.ToString();
                }
            }
        }

        private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            button1.Location = button1.Location;
            rotation = 1;
        }
    }
}

Okay so here is the ghetto result of storing the X and Y coordinates of the button. Just make sure your settings have two integers:

buttonX
buttonY

void saveLocation(object sender, EventArgs e)
        {
            myNamespace.Properties.Settings mySettings = new myNamespace.Properties.Settings();

            mySettings.buttonX = button1.Location.X;
            mySettings.buttonY = button1.Location.Y;

            mySettings.Save();
        }

        void applicationLaunch(object sender, EventArgs e)
        {
            myNamespace.Properties.Settings mySettings = new myNamespace.Properties.Settings();
            button1.Location = new Point(mySettings.buttonX, mySettings.buttonY);
        }

Check the following code i have added the lines which are bold

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace DLLOpen
{
    public partial class Form1 : Form
    {
        public const int buttonDown = 0xA1;
        public const int caption = 0x2;
        public int rotation;
       [B] public int x, y;
        bool flag1 = true;[/B]
        public Form1()
        {
            InitializeComponent();
        }

      [DllImportAttribute("user32.dll")]
        public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
        [DllImportAttribute("user32.dll")]
        public static extern bool ReleaseCapture();

        protected override void OnMouseMove(MouseEventArgs mouseEv)
        {
            
            int mouseX = mouseEv.X;
            int mouseY = mouseEv.Y;

            int myMouseX = button1.Location.X - 10;
            int myMouseY = button1.Location.Y - 10;
            if (flag1)
            {
                label1.Text = "Mouse X: " + myMouseX.ToString();
                label2.Text = "Mouse Y: " + myMouseY.ToString();
                
                if (rotation == 0)
                {
                    button1.Location = new System.Drawing.Point(mouseX + 10, mouseY + 10);

                    if (mouseEv.Y <= 360)
                    {
                        rotation = 0;
                        button1.Location = new System.Drawing.Point(mouseX + 10, mouseY + 10);
                        label1.Text = "Mouse X: " + myMouseX.ToString();
                        label2.Text = "Mouse Y: " + myMouseY.ToString();
                       [B] x = myMouseX;
                        y = myMouseY;[/B]
                    }

                    else if (mouseEv.Y >= 360)
                    {
                        button1.Location = new Point(215, 165);
                        rotation = 2;

                        label1.Text = "Mouse X: " + myMouseX.ToString();
                        label2.Text = "Mouse Y: " + myMouseY.ToString();
                       [B] x = myMouseX;
                        y = myMouseY;[/B]
                    }                }       }                   }

        private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
           // button1.Location = button1.Location;
           // rotation = 1;
        }

       [B] private void button1_Click(object sender, EventArgs e)
        {
            flag1 = true;
        }
        void Form1_MouseClick(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            EventArgs ee = new EventArgs(); ;
            saveLocation(this, ee);
            flag1 = false;
        }
        void saveLocation(object sender, EventArgs e)
        {
            this.button1.Location = new System.Drawing.Point(x, y);
        }
[/B]
       
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.