Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Move button

Move button

Scheduled Pinned Locked Moved C#
tutorialquestion
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    Giuseppe Di Modica
    wrote on last edited by
    #1

    Hi! I want to know how to move buttons inside a windows form. I tried in some ways, but nothing..:( People said me to use drag&drop property but I think it is not the right way.. Can someone give me a hand? If it is not to ask too much, could you write me the code? Thanks a lot!

    D 1 Reply Last reply
    0
    • G Giuseppe Di Modica

      Hi! I want to know how to move buttons inside a windows form. I tried in some ways, but nothing..:( People said me to use drag&drop property but I think it is not the right way.. Can someone give me a hand? If it is not to ask too much, could you write me the code? Thanks a lot!

      D Offline
      D Offline
      DougW48
      wrote on last edited by
      #2

      Use the setbounds function to change the location of a button on a form...

      G 1 Reply Last reply
      0
      • D DougW48

        Use the setbounds function to change the location of a button on a form...

        G Offline
        G Offline
        Giuseppe Di Modica
        wrote on last edited by
        #3

        I am still trying but I don't succeed in understanding how to use it.. HELP ME!!! I have to use SetBounds property inside MouseMove event? Could you write me, please, some line of code?

        R 1 Reply Last reply
        0
        • G Giuseppe Di Modica

          I am still trying but I don't succeed in understanding how to use it.. HELP ME!!! I have to use SetBounds property inside MouseMove event? Could you write me, please, some line of code?

          R Offline
          R Offline
          Robert Rohde
          wrote on last edited by
          #4

          Just as a starting point (you need to adjust this a bit): Create an empty form with a button called button1. Then add the following code:

          bool attached = false;
          Point oldPos;

          private void button1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
          {
          attached = true;
          oldPos = button1.PointToScreen(new Point(e.X, e.Y));
          }

          private void button1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
          {
          Move(button1.PointToScreen(new Point(e.X, e.Y)));
          }

          private void button1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
          {
          attached = false;
          }

          private void Form1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
          {
          Move(PointToScreen(new Point(e.X, e.Y)));
          }

          private void Move(Point p)
          {
          if (attached)
          {
          button1.Left -= oldPos.X - p.X;
          button1.Top -= oldPos.Y - p.Y;
          oldPos = p;
          }
          }

          Dont forget to attach the eventhandlers

          G 1 Reply Last reply
          0
          • R Robert Rohde

            Just as a starting point (you need to adjust this a bit): Create an empty form with a button called button1. Then add the following code:

            bool attached = false;
            Point oldPos;

            private void button1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
            {
            attached = true;
            oldPos = button1.PointToScreen(new Point(e.X, e.Y));
            }

            private void button1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
            {
            Move(button1.PointToScreen(new Point(e.X, e.Y)));
            }

            private void button1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
            {
            attached = false;
            }

            private void Form1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
            {
            Move(PointToScreen(new Point(e.X, e.Y)));
            }

            private void Move(Point p)
            {
            if (attached)
            {
            button1.Left -= oldPos.X - p.X;
            button1.Top -= oldPos.Y - p.Y;
            oldPos = p;
            }
            }

            Dont forget to attach the eventhandlers

            G Offline
            G Offline
            Giuseppe Di Modica
            wrote on last edited by
            #5

            This is a great solution! Thanks a lot, really. I have just tried it, and it works. Really good. Thanx, thanx and thanx!

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups