Thread: Crazy loss of focus issue (i think)

  1. #1
    Registered User AtomRiot's Avatar
    Join Date
    Jan 2003
    Posts
    120

    Crazy loss of focus issue (i think)

    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

    Code:
    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?
    All Your Base Are Still Belong to Someone!!!
    And you Remember that!!!

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Because you only get mouse move messages if the mouse if over the button, and you only get so many per time unit. If the mouse moves fast enough that it leaves the button before the next message is sent, the button won't move to stay centered on the mouse.

    You need to "capture" the mouse for this to work. The WinAPI call is SetMouseCapture, I think. .Net should have an equivalent call, probably in the Window class.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Registered User AtomRiot's Avatar
    Join Date
    Jan 2003
    Posts
    120
    yeah the resolution was to use the controls Capture property rather than using a bool. the new code looks like

    Code:
    private void button1_MouseDown(object sender, MouseEventArgs e)
            {
                xOffset = e.X;
                yOffset = e.Y;
                
                Control s = (Control)sender;
                
                if (s.Parent != this)
                {
                    s.Parent = this;
                    s.Left += panel1.Left;
                    s.Top += panel1.Top;
                }
                s.BringToFront();
                s.Capture = true;
            }
    
            private void button1_MouseUp(object sender, MouseEventArgs e)
            {
                ((Control)sender).Capture = false;
            }
    
            private void button1_MouseMove(object sender, MouseEventArgs e)
            {
                Control s = (Control) sender;
                if (s.Capture)
                {
                    s.Left += (e.X - xOffset);
                    s.Top += (e.Y - yOffset);
                }
            }
    one main noticeable difference is the location of setting the Control.Capture to true inside the mousedown. It had to occur at the end of the MouseDown so all the other stuff could be set and used properly because the button seems to lose focus when the parent changes and putting it at the end seems to regain that attention back to the mouse.
    All Your Base Are Still Belong to Someone!!!
    And you Remember that!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. float calculation issue
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 05-26-2008, 04:56 AM
  2. Integer displaying crazy numbers issue
    By Razorblade Kiss in forum C++ Programming
    Replies: 16
    Last Post: 12-20-2004, 11:00 PM
  3. Focus Messages?
    By PsychoBrat in forum Windows Programming
    Replies: 5
    Last Post: 12-30-2003, 07:14 AM
  4. Configurations give different results
    By Hubas in forum Windows Programming
    Replies: 2
    Last Post: 04-11-2003, 11:43 AM
  5. Focus & Always-On-Top :: MFC
    By kuphryn in forum Windows Programming
    Replies: 2
    Last Post: 06-13-2002, 05:44 PM