The idea is to do basic math to find the difference in size between the object and the form.

To do this take the object size (int1, int2) and the form size (int1_1, int2_2) then get the difference by subtraction.

The math is (int1 - int1_1, int2 - int2_2) so it's fairly simple and straight forward.

For this example we will automatically set up the size of the form and the panel we will be resizing on form load.

void Form1_Load()
        {
            this.Size = new System.Drawing.Size(566, 461);
            panel1.Size = new System.Drawing.Size(526, 399);
        }

Now to get the difference in sizes. To do this, subtract panel size from form size.

566 - 526 = w
461 - 399 = h

In this particular case the difference is (40, 62)

w = 40
h = 62


Now for the re-size event. Just set the panel size to an integer equal to the form size's difference.

void Form1_Resize(object sender, EventArgs e)
        {
            int formH = this.Size.Height;
            int formW = this.Size.Width;
            h = formH - 62;
            w = formW - 40;

            panel.Size = new System.Drawing.Size(w, h);
        }

Now we still have to declare h and w as public integers at the top of the source. We put this after the InitializeComponent void. If we don't we get errors. :(

public int h;
        public int w;

You're welcome. :D If you need help just ask. :)

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public int x;
        public int y;

        void Form1_Load(object sender, EventArgs e)
        {
            this.Size = new System.Drawing.Size(566, 461);
            panel1.Size = new System.Drawing.Size(526, 399);
        }

        void Form1_Resize(object sender, EventArgs e)
        {
            int formX = this.Size.Height;
            int formY = this.Size.Width;
            x = formX - 62;
            y = formY - 40;

            panel1.Size = new System.Drawing.Size(y, x);
        }
    }

Nice code, haven't checked but sure will work...but one question? if you want the panel to resize based on the forms size why make x,y int values public? The code is within the same form so there is no need to make the values public by doing so it allows the values to be changed from outside the form. If you have say purchase program information located within this panel then those values could easily be changed to x=0;w=0; and now this information is basically invisible. What I'm getting at is that you want to make your application as secure as possible and only make the items public that cannot make your application significantly vulnerable outside of the application. Hope this small point helps you out in the future but again nice example.

Edited 5 Years Ago by CsharpChico: n/a

Well, at the time I didn't understand the difference between public and private variables; I'm glad that I didn't because a lot of my old useful sources had this minor detail in them. I converted all of them to DLL's and used them in other projects. Yes I do agree with the fact that it made it vulnerable; I picked up on it as I moved through the C# language about 2 weeks ago. I think I'll upload a few new snippets. :D