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. Focus lost when dragging a button

Focus lost when dragging a button

Scheduled Pinned Locked Moved C#
helpquestion
8 Posts 2 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.
  • A Offline
    A Offline
    AtomRiot
    wrote on last edited by
    #1

    ok, i have a button that i am dragging around. this much i can handle, but then i thought i would put it on a panel (like change the parent attribute) until the time came to use it and then when the button was clicked, it would set the parent to the main form (this) the issue is when I do this, i can drag the button around ok if i drag very slowly, if i go to fast, the mouse comes off the button and the button no longer drags. here is the main code. It uses a panel named panel1 and a button named button1

    namespace focus_test
    {
    public partial class Form1 : Form
    {
    int xOffset, yOffset;
    bool buttonClicked;
    public Form1()
    {
    xOffset = yOffset = 0;
    buttonClicked = false;
    InitializeComponent();
    }

        private void button1\_MouseDown(object sender, MouseEventArgs e)
        {
            xOffset = e.X;
            yOffset = e.Y;
            buttonClicked = true;
    
            if (button1.Parent != this)
            {
                button1.Parent = this;
                button1.Left += panel1.Left;
                button1.Top += panel1.Top;
            }            
            button1.BringToFront();
        }
    
        private void button1\_MouseUp(object sender, MouseEventArgs e)
        {
            buttonClicked = false;
        }
    
        private void button1\_MouseMove(object sender, MouseEventArgs e)
        {
            if (buttonClicked)
            {
                button1.Left += (e.X - xOffset);
                button1.Top += (e.Y - yOffset);
            }
        }
    }
    

    }

    to see what i am seeing, click on the button and hold down the mouse and drag it around. Going too fast (using the term "fast" loosely) the mouse will end up not over the piece any longer and not send a mouse move message to it anymore. but if you go over the button and let go of your mouse then click and drag again, you can go as fast as you want like the poor little mouse is welded to the button. anyone got any clues as to why this is so?

    "Details details. Things to do Things to get done. Don't bother me with details just tell me when they're done." James Price

    G 1 Reply Last reply
    0
    • A AtomRiot

      ok, i have a button that i am dragging around. this much i can handle, but then i thought i would put it on a panel (like change the parent attribute) until the time came to use it and then when the button was clicked, it would set the parent to the main form (this) the issue is when I do this, i can drag the button around ok if i drag very slowly, if i go to fast, the mouse comes off the button and the button no longer drags. here is the main code. It uses a panel named panel1 and a button named button1

      namespace focus_test
      {
      public partial class Form1 : Form
      {
      int xOffset, yOffset;
      bool buttonClicked;
      public Form1()
      {
      xOffset = yOffset = 0;
      buttonClicked = false;
      InitializeComponent();
      }

          private void button1\_MouseDown(object sender, MouseEventArgs e)
          {
              xOffset = e.X;
              yOffset = e.Y;
              buttonClicked = true;
      
              if (button1.Parent != this)
              {
                  button1.Parent = this;
                  button1.Left += panel1.Left;
                  button1.Top += panel1.Top;
              }            
              button1.BringToFront();
          }
      
          private void button1\_MouseUp(object sender, MouseEventArgs e)
          {
              buttonClicked = false;
          }
      
          private void button1\_MouseMove(object sender, MouseEventArgs e)
          {
              if (buttonClicked)
              {
                  button1.Left += (e.X - xOffset);
                  button1.Top += (e.Y - yOffset);
              }
          }
      }
      

      }

      to see what i am seeing, click on the button and hold down the mouse and drag it around. Going too fast (using the term "fast" loosely) the mouse will end up not over the piece any longer and not send a mouse move message to it anymore. but if you go over the button and let go of your mouse then click and drag again, you can go as fast as you want like the poor little mouse is welded to the button. anyone got any clues as to why this is so?

      "Details details. Things to do Things to get done. Don't bother me with details just tell me when they're done." James Price

      G Offline
      G Offline
      Guffa
      wrote on last edited by
      #2

      You have to capture the mouse in the mouse down event.

      --- single minded; short sighted; long gone;

      A 1 Reply Last reply
      0
      • G Guffa

        You have to capture the mouse in the mouse down event.

        --- single minded; short sighted; long gone;

        A Offline
        A Offline
        AtomRiot
        wrote on last edited by
        #3

        what do you mean? in the mouse down i currently grab the offset from the button's corner to the mouse, then use that in the mouse move so i can seamlessly move the button in the mouse move by saying button1.Left += (e.X - xOffset); and the same for the top coordinate. Mouse Down event is only sent once, its not a continuous thing is it? I tried moving in just the mouse down and it only moved once.

        "Details details. Things to do Things to get done. Don't bother me with details just tell me when they're done." James Price

        G 1 Reply Last reply
        0
        • A AtomRiot

          what do you mean? in the mouse down i currently grab the offset from the button's corner to the mouse, then use that in the mouse move so i can seamlessly move the button in the mouse move by saying button1.Left += (e.X - xOffset); and the same for the top coordinate. Mouse Down event is only sent once, its not a continuous thing is it? I tried moving in just the mouse down and it only moved once.

          "Details details. Things to do Things to get done. Don't bother me with details just tell me when they're done." James Price

          G Offline
          G Offline
          Guffa
          wrote on last edited by
          #4

          AtomRiot wrote:

          what do you mean?

          Exacly what I said. Have a look at the Capture property of the control that you want to move.

          --- single minded; short sighted; long gone;

          A 1 Reply Last reply
          0
          • G Guffa

            AtomRiot wrote:

            what do you mean?

            Exacly what I said. Have a look at the Capture property of the control that you want to move.

            --- single minded; short sighted; long gone;

            A Offline
            A Offline
            AtomRiot
            wrote on last edited by
            #5

            The Capture property is simply a bool variable that you can set. that doesnt change the fact that the button mousedown event is only thrown once and is not continuously. i already have a bool variable in that example that does the same thing the Capture property does.

            "Details details. Things to do Things to get done. Don't bother me with details just tell me when they're done." James Price

            G 1 Reply Last reply
            0
            • A AtomRiot

              The Capture property is simply a bool variable that you can set. that doesnt change the fact that the button mousedown event is only thrown once and is not continuously. i already have a bool variable in that example that does the same thing the Capture property does.

              "Details details. Things to do Things to get done. Don't bother me with details just tell me when they're done." James Price

              G Offline
              G Offline
              Guffa
              wrote on last edited by
              #6

              AtomRiot wrote:

              The Capture property is simply a bool variable that you can set.

              And you don't think that something happens when you set it? If it was "just a bool variable", then there would hardly be a point of having it as a property of the control.

              AtomRiot wrote:

              that doesnt change the fact that the button mousedown event is only thrown once and is not continuously.

              Why would you want the mouse down event to fire continously? That's what you have the mouse move event for.

              --- single minded; short sighted; long gone;

              A 1 Reply Last reply
              0
              • G Guffa

                AtomRiot wrote:

                The Capture property is simply a bool variable that you can set.

                And you don't think that something happens when you set it? If it was "just a bool variable", then there would hardly be a point of having it as a property of the control.

                AtomRiot wrote:

                that doesnt change the fact that the button mousedown event is only thrown once and is not continuously.

                Why would you want the mouse down event to fire continously? That's what you have the mouse move event for.

                --- single minded; short sighted; long gone;

                A Offline
                A Offline
                AtomRiot
                wrote on last edited by
                #7

                Sorry, but I am a little confused by the usage. I have tried setting the Capture property but I guess I just don't know how to use it properly. Can you point me to an example of this? Everywhere I search, it looks to be used just as a bool variable for that control.

                "Details details. Things to do Things to get done. Don't bother me with details just tell me when they're done." James Price

                G 1 Reply Last reply
                0
                • A AtomRiot

                  Sorry, but I am a little confused by the usage. I have tried setting the Capture property but I guess I just don't know how to use it properly. Can you point me to an example of this? Everywhere I search, it looks to be used just as a bool variable for that control.

                  "Details details. Things to do Things to get done. Don't bother me with details just tell me when they're done." James Price

                  G Offline
                  G Offline
                  Guffa
                  wrote on last edited by
                  #8

                  Here's how you use it:

                  private Point start;

                  private void movable_MouseDown(object sender, MouseEventArgs e) {
                  Control c = (Control)sender;
                  start = c.PointToScreen(e.Location);
                  c.Capture = true;
                  }

                  private void movable_MouseUp(object sender, MouseEventArgs e) {
                  Control c = (Control)sender;
                  c.Capture = false;
                  }

                  private void movable_MouseMove(object sender, MouseEventArgs e) {
                  Control c = (Control)sender;
                  if (c.Capture) {
                  Point mouse = c.PointToScreen(e.Location);
                  c.Left += mouse.X - start.X;
                  c.Top += mouse.Y - start.Y;
                  start = mouse;
                  }
                  }

                  Note: As this code uses the sender argument to reference the control, the code can be reused for the events of several controls that you want to be able to move.

                  --- single minded; short sighted; long gone;

                  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