0

I am creating a label control that allows resizing from the top and bottom of the label. It also only slides vertically when moved. I created the boundaries for its movement and for the bottom part of the resizable label, but I'm having trouble figuring out what I'm doing wrong for the top part. Whenever the mouse is dragged outside of the bounds from the top, it triggers the preset length of 450 pixels. I am using .SetBounds along with Math.Min.

using System;
using System.Drawing;
using System.Windows.Forms;

namespace EventTest
{
    public partial class Form1 : Form
    {
        Point label1_MousePoint_01 = new Point();
        Point label1_Start_01 = new Point();
        Point label1_End_01 = new Point();
        int label1_MouseSwitch_01;

        public Form1()
        {
            InitializeComponent();
            label1.MouseDown += Label1_MouseDown;
            label1.MouseUp += Label1_MouseUp;
            label1.MouseMove += Label1_MouseMove;
        }

        // Mouse Move Event
        private void Label1_MouseMove(object sender, MouseEventArgs e)
        {
            if ((e.X < label1.Width && e.X > 0 && e.Y > 0 && e.Y < 4) || (e.X < label1.Width && e.X > 0 && e.Y > label1.Height - 4 && e.Y < label1.Height))
            {
                Cursor.Current = Cursors.SizeNS;
            }

            // Mouse Event Top Move
            if (label1_MouseSwitch_01 == 1)
            {
                label1.SetBounds(0, Math.Max(e.Y + label1.Location.Y - label1_MousePoint_01.Y, 40), 120, Math.Min(label1.Height - e.Y + label1_MousePoint_01.Y, 450));
            }

            // Mouse Event Bottom Move
            else if (label1_MouseSwitch_01 == 2)
            {
                label1.Height = Math.Min(660 - label1.Location.Y, e.Location.Y);
            }

            // Mouse Event Grab and Move
            else if (label1_MouseSwitch_01 == 3)
            {
                int ny = Math.Min(Math.Max((label1.Location.Y - label1_MousePoint_01.Y + e.Y), 40), 660 - label1.Height);
                label1.Location = new Point(0, ny);
            }
        }

        // Mouse Up Event
        private void Label1_MouseUp(object sender, MouseEventArgs e)
        {
            label1_MouseSwitch_01 = 0;

            if (label1.Height < 20)
            {
                label1.Height = 20;
            }
        }

        // Mouse Down Event
        private void Label1_MouseDown(object sender, MouseEventArgs e)
        {
            label1_Start_01.Y = label1.Height;
            label1_End_01.Y = label1.Top;

            // Set Mouse Switch
            if (e.Button == MouseButtons.Left)
            {
                label1_MousePoint_01 = e.Location;

                if (e.Y > 0 && e.Y < 4)
                {
                    Cursor.Current = Cursors.SizeNS;
                    label1_MouseSwitch_01 = 1;
                }

                else if (e.Y > label1.Height - 4 && e.Y < label1.Height)
                {
                    Cursor.Current = Cursors.SizeNS;
                    label1_MouseSwitch_01 = 2;
                }

                else if (e.Y > 4 && e.Y < label1.Height - 4)
                {
                    label1_MouseSwitch_01 = 3;
                }
            }
        }

        // Snap To Every 20 Pixels 
        public int RoundEr(int i)
        {
            return ((int)Math.Round(i / 20.0)) * 20;
        }
    }
}
MBasic
  • 23
  • 6
  • One problem is that by moving the label you trigger more move (relative ) events than are actaully (absolutely) taking place. Insert this `if (lastCursorPos == Cursor.Position) return;` to the top and this `lastCursorPos = Cursor.Position;` at the bottom of the move event and add the new variable. Now you can see that at some point you add size at the bottom instead of the top. I'll leave it to you to investigate further.. – TaW Jul 26 '17 at 18:46
  • Yeah, I think .setbounds helped the effect from that which was jittery. I guess maybe I'm not setting the Min/ Max correctly for the label1.Height under .setbounds – MBasic Jul 26 '17 at 19:51

0 Answers0