Horizontally scrolling panel by dragging with the mouse or using the mouse wheel

I made a vertical scrolling panel and I want to make it horizontal scrolling. My code allows you to scroll the panel by dragging it with your mouse or mouse wheel.

This is my code:

private Point _mouseLastPosition;

    protected override void OnMouseDown(MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            _mouseLastPosition = e.Location;
        }
        base.OnMouseDown(e);
    }

    private int ValidateChange(int change)
    {
        var padding = -1;
        if (change < 0)
        {
            var max = (from Control control in Controls select control.Left + control.Width + padding).Concat(new[] { int.MinValue }).Max();

            if (max < Width + Math.Abs(change))
            {
                return Width - max;
            }
        }
        else
        {
            var min = (from Control control in Controls select control.Left).Concat(new[] { int.MaxValue }).Min();

            if (min > padding - Math.Abs(change))
            {
                return padding - min;
            }
        }
        return change;
    }

    private void HandleDelta(int delta)
    {
        var change = ValidateChange(delta);

        foreach (Control control in Controls)
        {
            control.Left += change;
        }

    }

    protected override void OnMouseMove(MouseEventArgs e)
    {
        if ((MouseButtons & MouseButtons.Left) != 0)
        {
            var delta = _mouseLastPosition.X - e.X;
            HandleDelta(delta);
            _mouseLastPosition = e.Location;
        }
        base.OnMouseMove(e);
    }

    protected override void OnMouseWheel(MouseEventArgs e)
    {
        HandleDelta(e.Delta);
        base.OnMouseWheel(e);
    }

      

What do I need to change to make it work the way I want it to work?

UPDATE: I changed my code as you told me, but it doesn't work the way I want it to. This is how it looks now (I want it to scroll to the right).

enter image description here

+3


source to share


1 answer


Change the following options ...

control.Top ->> control.Left

Control.height ->> control.Width

Height →> Width

var delta = eY - _mouseLastPosition.Y; →> var delta = _mouseLastPosition.X - eX;



UPDATE

This is not the best solution, but can you try this?

Keep inital left ..

    private Dictionary<string, int> dicControls;

    private bool isOverFlow;

    protected override void InitLayout()
    {
        base.InitLayout();

        if (dicControls == null)
        {
            dicControls = new Dictionary<string, int>();

            foreach (Control control in Controls)
            {
                dicControls.Add(control.Name, control.Left);
            }

            var max = (from Control control in Controls select control.Left + control.Width + -1).Concat(new[] { int.MinValue }).Max();

            isOverFlow = (Width - max) < 0;
        }
    }

    private void HandleDelta(int delta)
    {
        var change = ValidateChange(delta);

        foreach (Control control in Controls)
        {
            var initalLeft = dicControls[control.Name];

            var tempLeft = control.Left + change;

            if(isOverFlow)
            {
                if (tempLeft > initalLeft || delta > 0)
                {
                    control.Left = initalLeft;
                }
                else
                {
                    control.Left += change;
                }
            }
            else
            {
                if (tempLeft < initalLeft)
                {
                    control.Left = initalLeft;
                }
                else
                {
                    control.Left += change;
                }
            }
        }
    }

      

0


source







All Articles