开发者

Problem with MouseMove event, launched when the content of control beneath is moved with arrow keys

开发者 https://www.devze.com 2023-03-10 07:17 出处:网络
My UserControl is a kind of a container that has a set of controls inside. One of the UI behaviours I designed is that the move over nested control makes it selected for some keyboard triggered action

My UserControl is a kind of a container that has a set of controls inside. One of the UI behaviours I designed is that the move over nested control makes it selected for some keyboard triggered actions.

The other way of setting up the nested control that is targeted to receive keyboard input is with arrow keys that change focus to certain control within my UserControl.

The problem is that in most cases my UserControl has scrollbars and switching between its elements with arrow keys causes contents to move. Because of that movement it se开发者_StackOverflow中文版ems that mousemove event is launched also when the mouse arrow stands still, but its over my usercontrol. In the end, the wrong nested control is being selected.

I tried to set a boolean flag to temporary lock the actions of mousemove event while the arrow keys handling function is launched, but it seems to not to work at all.

Does anybody have an idea how to prevent that unwanted triggering of mousemove event and avoid the problems it causes?


I did some debugging/experiments and it appears that though mouse stays still over controll the MouseMove is actually called frequently and not only when control move is caused by some UI behaviour (scrolling of parent controll called using keyboard).

It also seems like it is called twice every single second (but probably only when application is launched form VS during debug session?), the calls are separated by few hundred parts of seconds and for now i couldnt determine what causes those.

Solution (maybe not only, and not the best) is to store last position of cursor and compare it inside event to check if mouse was really moved from last MouseMove call. I achieved it using System.Windows.Forms.Cursor.Position because position from MouseEventArgs will be diffrent depending on circumstances but still will be inconclusive to determine if mouse cursor was moved.

    Point lastCursorPosition = new Point();

    private void panelPictures_MouseMove(object sender, MouseEventArgs e)
    {
        if (System.Windows.Forms.Cursor.Position != lastCursorPosition)
        {
            Console.WriteLine("mouse moved");
            lastCursorPosition = System.Windows.Forms.Cursor.Position;
        }
        else
        {
            Console.WriteLine("mouse in place");
        }
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号