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. Windows Forms
  4. Problems Dynamically Moving Controls On WinForm

Problems Dynamically Moving Controls On WinForm

Scheduled Pinned Locked Moved Windows Forms
7 Posts 4 Posters 1 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.
  • O Offline
    O Offline
    oracleConvert
    wrote on last edited by
    #1

    I wrote a small test form that allows the user to click on a control and then move the control to the desired location using mouse down and mouse up events. I have placed a groupbox on the form and then am dynamically creating a combox and placing it in the group box. I can get it to move the control ok, but it does not end up in the position where the user lets up the mouse button. I am using PointToClient but it is not working. Here is the code: public Form1() { InitializeComponent(); ComboBox cb = new ComboBox(); cb.Parent = groupBox; cb.Location = new Point(60, 15); cb.MouseDown +=new MouseEventHandler(cb_MouseDown); cb.MouseUp +=new MouseEventHandler(cb_MouseUp); } private void cb_MouseDown (object sender, MouseEventArgs e ) { Cursor = Cursors.SizeAll; } private void cb_MouseUp (object sender, MouseEventArgs e) { ComboBox cb = ((ComboBox) sender); cb.Location = PointToClient (new Point(e.X, e.Y)); Cursor = Cursors.Default; }

    L R D 3 Replies Last reply
    0
    • O oracleConvert

      I wrote a small test form that allows the user to click on a control and then move the control to the desired location using mouse down and mouse up events. I have placed a groupbox on the form and then am dynamically creating a combox and placing it in the group box. I can get it to move the control ok, but it does not end up in the position where the user lets up the mouse button. I am using PointToClient but it is not working. Here is the code: public Form1() { InitializeComponent(); ComboBox cb = new ComboBox(); cb.Parent = groupBox; cb.Location = new Point(60, 15); cb.MouseDown +=new MouseEventHandler(cb_MouseDown); cb.MouseUp +=new MouseEventHandler(cb_MouseUp); } private void cb_MouseDown (object sender, MouseEventArgs e ) { Cursor = Cursors.SizeAll; } private void cb_MouseUp (object sender, MouseEventArgs e) { ComboBox cb = ((ComboBox) sender); cb.Location = PointToClient (new Point(e.X, e.Y)); Cursor = Cursors.Default; }

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      it ain't that simple. First of all, mouse events provide locations relative to the top lefthand corner of the control that reports the mouse actions, so any movement you want to accomplish needs at least to do the following:

      finalLocation = originalLocation - mouseDownLocation + mouseUpLocation.

      Furthermore, you have some functional shortcomings: 1. you probably want to see the drag going on, which means you need MouseMove as well. 2. when the mouse leaves the container, your container no longer gets mouse events, unless you have captured the mouse first. 3. when you want context menus you need to process right-clicks; you then don't want to move the control I guess. :)

      Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

      Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

      O 1 Reply Last reply
      0
      • O oracleConvert

        I wrote a small test form that allows the user to click on a control and then move the control to the desired location using mouse down and mouse up events. I have placed a groupbox on the form and then am dynamically creating a combox and placing it in the group box. I can get it to move the control ok, but it does not end up in the position where the user lets up the mouse button. I am using PointToClient but it is not working. Here is the code: public Form1() { InitializeComponent(); ComboBox cb = new ComboBox(); cb.Parent = groupBox; cb.Location = new Point(60, 15); cb.MouseDown +=new MouseEventHandler(cb_MouseDown); cb.MouseUp +=new MouseEventHandler(cb_MouseUp); } private void cb_MouseDown (object sender, MouseEventArgs e ) { Cursor = Cursors.SizeAll; } private void cb_MouseUp (object sender, MouseEventArgs e) { ComboBox cb = ((ComboBox) sender); cb.Location = PointToClient (new Point(e.X, e.Y)); Cursor = Cursors.Default; }

        R Offline
        R Offline
        RobCroll
        wrote on last edited by
        #3

        Don't use location use Left and Top

            private void cb\_MouseUp(object sender, MouseEventArgs e)
            {
                ComboBox box = sender as ComboBox;
                box.Left += e.X;
                box.Top += e.Y;
        
                Cursor = Cursors.Default;
            }
        

        If you're moving it from one container into another, You're going to have to work out which container and then remove control from original container and add to new container. Sounds like fun :)

        1 Reply Last reply
        0
        • O oracleConvert

          I wrote a small test form that allows the user to click on a control and then move the control to the desired location using mouse down and mouse up events. I have placed a groupbox on the form and then am dynamically creating a combox and placing it in the group box. I can get it to move the control ok, but it does not end up in the position where the user lets up the mouse button. I am using PointToClient but it is not working. Here is the code: public Form1() { InitializeComponent(); ComboBox cb = new ComboBox(); cb.Parent = groupBox; cb.Location = new Point(60, 15); cb.MouseDown +=new MouseEventHandler(cb_MouseDown); cb.MouseUp +=new MouseEventHandler(cb_MouseUp); } private void cb_MouseDown (object sender, MouseEventArgs e ) { Cursor = Cursors.SizeAll; } private void cb_MouseUp (object sender, MouseEventArgs e) { ComboBox cb = ((ComboBox) sender); cb.Location = PointToClient (new Point(e.X, e.Y)); Cursor = Cursors.Default; }

          D Offline
          D Offline
          Dave Kreskowiak
          wrote on last edited by
          #4

          I hate tooting my own horn, but take a look at my article on the subject -> Create your Own Runtime Movable Windows Forms Controls[^].

          A guide to posting questions on CodeProject[^]
          Dave Kreskowiak

          L 1 Reply Last reply
          0
          • D Dave Kreskowiak

            I hate tooting my own horn, but take a look at my article on the subject -> Create your Own Runtime Movable Windows Forms Controls[^].

            A guide to posting questions on CodeProject[^]
            Dave Kreskowiak

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #5

            Sorry, I should have provided that link, I was aware of your excellent article on the subject... :)

            Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

            Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

            D 1 Reply Last reply
            0
            • L Luc Pattyn

              it ain't that simple. First of all, mouse events provide locations relative to the top lefthand corner of the control that reports the mouse actions, so any movement you want to accomplish needs at least to do the following:

              finalLocation = originalLocation - mouseDownLocation + mouseUpLocation.

              Furthermore, you have some functional shortcomings: 1. you probably want to see the drag going on, which means you need MouseMove as well. 2. when the mouse leaves the container, your container no longer gets mouse events, unless you have captured the mouse first. 3. when you want context menus you need to process right-clicks; you then don't want to move the control I guess. :)

              Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

              Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

              O Offline
              O Offline
              oracleConvert
              wrote on last edited by
              #6

              Thanks Luc! This is what I needeed.

              1 Reply Last reply
              0
              • L Luc Pattyn

                Sorry, I should have provided that link, I was aware of your excellent article on the subject... :)

                Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

                Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

                D Offline
                D Offline
                Dave Kreskowiak
                wrote on last edited by
                #7

                No big deal. I just hate it when I see people doing that and they all start by handling the mouse event of the form itself. There's an easier way!

                A guide to posting questions on CodeProject[^]
                Dave Kreskowiak

                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