Rj1234
Rj1234

Reputation: 7

Make a Form extend until it reaches a maximum Width and Height

It always goes too far the same with height and everything is off-center because the Form gets bigger than intended.

Code:

private void timer1_Tick(object sender, EventArgs e)
{
    this.Width += 3;
    if (this.Width >= 800)
    {
        timer1.Stop();
        timer2.Start();
    }
} 

private void timer2_Tick(object sender, EventArgs e)
{
    this.Height += 3;
    if (this.Height >= 500)
    {
        timer2.Stop();
    }
}

Upvotes: 0

Views: 64

Answers (2)

DoragonSoruja
DoragonSoruja

Reputation: 123

You're increasing the height and width by three pixels, meaning it's going to skip a lot of numbers and your If Statements are setup incorrectly for that.

Take your width If Statement for example, you're saying "Stop when the width is Greater or Equal to 800", by having Greater or Equal in your If Statement you're allowing your application to grow past your desired size and since the window is increasing by three it's easy for it to skip over 800.

You should change your code so you're incrementing on even numbers.

if(this.Width % 2 == 1)
    this.Width += 3;
else if(this.Width % 2 == 0)
    this.Width += 2;

        if (this.Width >= 800)
        {
            timer1.Stop();
            timer2.Start();
        }

The code above is very sloppy but it should work until you find a better solution or once it has passed your desired size change it back manually since it should only be off by a few numbers according to your posted code.

this.Width += 3;
        if (this.Width >= 800)
        {
            timer1.Stop();
            this.Width == 800;
            timer2.Start();
        }

Upvotes: 0

LarsTech
LarsTech

Reputation: 81675

Set the form's MaximumSize property, either in the constructor or in the designer:

this.MaximumSize = new Size(800, 500);

Upvotes: 1

Related Questions