MBasic
MBasic

Reputation: 23

Size from top to bottom

This is more of a question of methodology. I know how to create a control that scales from all sides, but I don't understand why this code doesn't work fluidly without redraw issues (jittering). It scales the panel from the top only, but jitters in the process. What am I missing?

    public partial class Form1 : Form
{
    Point MousePoint = new Point();
    public Form1()
    {
        InitializeComponent();
        panel1.MouseMove += Panel1_MouseMove;
        panel1.MouseDown += Panel1_MouseDown;
        panel1.Width = 100;
        panel1.Height = 100;
    }

    private void Panel1_MouseDown(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            MousePoint = e.Location;
        }
    }

    private void Panel1_MouseMove(object sender, MouseEventArgs e)
    {
        if(e.Button == MouseButtons.Left)
        {
            panel1.Top = e.Location.Y + panel1.Location.Y - MousePoint.Y;
            panel1.Height = panel1.Height - e.Y + MousePoint.Y;
        }
    }
}

Upvotes: 2

Views: 83

Answers (1)

LarsTech
LarsTech

Reputation: 81610

You are changing two different properties: the top, then the height. That could cause the "jittering" you are seeing.

Try using the SetBounds call instead:

 panel1.SetBounds(panel1.Left,
                  e.Location.Y + panel1.Location.Y - mousePoint.Y,
                  panel1.Width,
                  panel1.Height - e.Y + mousePoint.Y);

If there are contained controls inside the panel that are anchored, etc, that could affect that smoothness of the resizing, too.

Upvotes: 1

Related Questions