Page 1 of 1

Change Form Size in a running application

#1 Macjohn  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 80
  • View blog
  • Posts: 407
  • Joined: 10-April 09

Posted 17 May 2011 - 06:31 AM

Hey guys,

in this tutorial I will be showing you how you can increase and decrease your forms width and height.

Ok, so why would you use this? This method can be use in a fixed width and height form, to show hidden things in the unvisible part of the form.

So lets just start.

1st Step - Start by creating a new C# Form Project.

2nd Step - Increase the Width and the height of the form to width = 500, height = 500 (you can easely do this in the Properties panel, at the bottom right of your IDE)

3rd Step - Create 6 buttons

1st button: change the name to bttWidthInc will be the Width button, it will be used to increse the width of the form.

2nd button: change the name to bttHeightInc, will be the Height button, it will be used to increse the width of the form.

3rd button: change the name to bttInc, will be the Width and Height button, it will be used to increse the width and the Height of the form at the saime time.

4th button: change the name to bttWidthDec will be the second Width button, it will be used to decrease the width of the form.

5th button: change the name to bttHeightDec will be the second Height button, it will be used to decrease the width of the form.

6th button: change the name to bttDec, will be the second Width and Height button, it will be used to decrease the width and the Height of the form at the saime time.

At this stage of the tutorial your form should be lookng something like this:

Attached Image


Now that we have our form in order lets start with some code.

1ST PART - INCREASING THE FORM


We will start with the button that we named bttWidthInc.

Double click the button bttWidthInc, and write this code:

        private void bttWidthInc_Click(object sender, EventArgs e)
        {
            Form1.ActiveForm.Width = 500;
            bttWidthInc.Visible = false;
        }


1st Line of code:
Form1 - the element that will be selected
ActiveForm - the selection of the element
width - The property that will be changed
"500" - the width choosen

2nd Line of code:
bttWidthInc - the element selected
Visible - The propertie that will be changed
false - the propertie option

(Note: The width choosen doesn't need to be a static number, you may use a variable if you like.I'll do it the next tutorial.)

Lets keep on going...

next step,

double click the button bttHeightInc, and write the saime code, with only a bit of changing.
Instead of Width, you shall write Height.

        private void bttHeigtInc_Click(object sender, EventArgs e)
        {
            Form1.ActiveForm.Height = 500;
            bttHeightInc.Visible = false;
        }




Now we will choose the bttInc, this button will change the Width and the Height at the saime time.

Double click the bttInc, and the code will be a junction of the two other buttons.

        private void bttInc_Click(object sender, EventArgs e)
        {
            Form1.ActiveForm.Height = 500;
            Form1.ActiveForm.Width = 500;
            bttWidthInc.Visible = false;
            bttHeightInc.Visible = false;
            bttInc.Visible = false;
        }



In this code we have all the other code that was used the the previous buttons, because this button will open the Width and the Height so we want it to put the button bttWidthInc and the button bttHeightInc invisible to.

Now we have finished all the buttons that will make our form Increase, lets go to the buttons that make our form decrease.

2ND PART - DECREASING THE FORM

The part of decreasing the form is pretty much the saime as the increasing. We will only change the properties of the the elements.

So, lets start with the button called bttWidthDec:

        private void bttWidthDec_Click(object sender, EventArgs e)
        {
            Form1.ActiveForm.Width = 300;
            bttWidthInc.Visible = true;
        }



Again, the code is pretty much the saime, only the properties changed.
The Width will decrease from
500
to
300
, and the button that was invisible, with the property
 true
, it will become visible again.

next step,

double click the button bttHeightInc, and write the saime code, with only a bit of changing.
Instead of Width, you shall write Height.

        private void bttHeigtInc_Click(object sender, EventArgs e)
        {
            Form1.ActiveForm.Height = 300;
            bttHeightInc.Visible = true;
        }



and for the last button, the bttDec is also the saime that the bttInc with only the saime changes as the above.

        private void bttHeightDec_Click(object sender, EventArgs e)
        {
            Form1.ActiveForm.Width = 300;
            Form1.ActiveForm.Height = 300;
            bttHeightInc.Visible = true;
            bttHeightInc.Visible = true;
            bttInc.Visible = true;
        }




And that is it for the code.

Now you can do the debug of the application to see if every button works.

You can now implement it in other applications.

Hope you enjoyed it.

If you have any questions just post below.

Is This A Good Question/Topic? 2
  • +

Replies To: Change Form Size in a running application

#2 waqar144  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 05-December 11

Posted 10 May 2013 - 12:15 AM

Revise your tut. Lots of mistakes in there.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1