ErocM
ErocM

Reputation: 4662

Get winform size after mouse up to get final size after resize

I am trying to grab the windows size of a form when the mouse has been released. I can use the event SizeChanged but I put a test in there and it fires off numerous times before the mouse is released. I put in a counter and it was up over a 100 hits on a simple resize. I want to save this 'final' size to a config but I don't want it saving 100 times in a row.

An example of what I mean:

private void CreditCardScreen_Shown(object sender, EventArgs e)
{
  Size = Properties.Settings.Default.ScreenSize;
}

private void CreditCardScreen_SizeChanged(object sender, EventArgs e)
{
  count++;
  Console.WriteLine("Count: " + count);
  // save size here
}

I end up with:

..
Count: 66
Count: 67
Count: 68
Count: 69
Count: 70
Count: 71
Count: 72
Count: 73
Count: 74

How do I get the final window size after they have released the mouse after resizing?

Upvotes: 0

Views: 942

Answers (2)

Gregor Primar
Gregor Primar

Reputation: 6805

This will do the job:

    public partial class Form1 : Form
    {
        Point p = new Point();

        public Form1()
        {
            InitializeComponent();
            this.ResizeEnd += Form1_ResizeEnd;
            SetDimension();
        }

        void SetDimension()
        {
            p = new Point(this.Width, this.Height);
        }

        void Form1_ResizeEnd(object sender, EventArgs e)
        {
            //check to avoid save if form was just moved...
            if (this.Width != p.X || this.Height != p.Y)
            {
                SetDimension();
                MessageBox.Show( string.Format("Width={0} Height={1}, save your settings!", this.Width, this.Height));
            }        
        }

}

Upvotes: 1

Ofir Winegarten
Ofir Winegarten

Reputation: 9365

Use Form.ResizeEnd Event

The ResizeEnd event is raised when the user finishes resizing a form, typically by dragging one of the borders or the sizing grip located on the lower-right corner of the form, and then releasing it. For more information about the resizing operation, see the ResizeBegin event.

Upvotes: 0

Related Questions