Click here to Skip to main content
Click here to Skip to main content

C# - Automatically Resize Controls @ Runtime

, 13 Mar 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
Resize controls @ runtime the easier way


This is my first post using C# to resize controls inside the container or form. I browsed the web to look for the reference code but unfortunately, I never found one. I was desperate that no code has been available online using the C# language, so I decided to create my own. I took me 30 minutes to figure this out using simple mathematical calculations. Feedback is highly appreciated.


Usually, when we're creating an application, we usually take for granted the simple controls resizing during runtime. I observed that most devs usually used the Anchor, Docks or Layoutpanel to implement the controls.

Using the Code

This is the main body of the program: [Modified Complete source with auto-font adjust]

This is where the default includes of C#...

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

Below here is the public variable declarations:

If you will take a look in the initial variable declaration, it will be initialized to zero as default. Later on, you will see that the variable here will be used in the main program.

  //Public Declaration: Initial declaration

        double rW = 0;
        double rH = 0;

        int fH = 0;
        int fW = 0;

        bool hasTabs=false;

Next is the form initialization. We will define the resize and the handler.

// @ Form Initialization
public  MyForm(){
this.Resize += MyForm.Resize
this.tabControl1.Dock = DockStyle.None; } 

Next is the form resize routine. This will be called during form resizing event. If you will look at the form initialization event, the form_resize is handled by our event declared before.

 //This is the form resize function:
private void MyForm_Resize(object sender, EventArgs e)
        {// if you have a groupbox container, you can change the tabcontrol 
            rResize(this); //Call the routine to resize 

Then, instead of passing the argument to the form resize event, I decided to add this to subroutine. To make the code tidy and minimize clogging in 1 event, usually, I use this style of programming. You can modify this line as you may wish to accommodate different container but make sure that you are calling the right event and controls.

// This is the routine to be called when resizing: 
 private void rResize(Control t) // Routine to Auto resize the control

        {            // rRH- Ratio of cntrol height to container height 
// rRW - ratio of control width to container height
// rH -root Height or the container height
// rW - root width or the container width
// fW -initial form width
// fH -initial form height

            string[] s = null; //set as null contents

            if (this.Width < fW || this.Height < fH)
                //in the future you can change this block to return to 
                //original when form is resize below the default startup size.
                this.Width = (int)fW; // i use simple casting
                this.Height = (int)fH; // this one also

            foreach (Control c in t.Controls)
                // Option 1:
                double rRW = (t.Width > rW ? t.Width / (rW) : rW / t.Width);
                double rRH = (t.Height > rH ? t.Height / (rH) : rH / t.Height);
                s = c.Tag.ToString().Split('/'); //split string using delimiter
                if (c.Name == s[0].ToString()) //check if the control name is 
                    //the same of the stored value in the form load event.
                    //Use integer casting
                    // if you will notice, i use casting to minimize conversion time during runtime
                    c.Width = (int)(Convert.ToInt32(s[3]) * rRW);
                    c.Height = (int)(Convert.ToInt32(s[4]) * rRH);
                    c.Left = (int)(Convert.ToInt32(s[1]) * rRW);
                    c.Top = (int)(Convert.ToInt32(s[2]) * rRH);
                    c.Font = new Font(this.Font.FontFamily, (float)(Convert.ToInt32(s[5]) * rRH));
                if (hasTabs) //check if tabcontrol is present inside the container 
                    //i.e., form/tabcontrol or groupbox
                    if (c.GetType() == typeof(TabControl))
                        foreach (Control f in c.Controls)
                            foreach (Control j in f.Controls) //tabpage
                                s = j.Tag.ToString().Split('/');

                                if (j.Name == s[0].ToString())
                                    j.Width = (int)(Convert.ToInt32(s[3]) * rRW);
                                    j.Height = (int)(Convert.ToInt32(s[4]) * rRH);
                                    j.Left = (int)(Convert.ToInt32(s[1]) * rRW);
                                    j.Top = (int)(Convert.ToInt32(s[2]) * rRH);
                                    j.Font = new Font(this.Font.FontFamily, (float)(Convert.ToInt32(s[5]) * rRH));

Lastly, the form load event. At this point, I will provide the values to declared variables.

// this is the routine @ form load: 
  private void MyForm_Load(object sender, EventArgs e)
        {hasTabs=true; //is tabcontrol present/

            // Put values in the variables
            rW = this.Width; // this value is for container
            rH = this.Height; //this value is for container

            fW = this.Width; //do not change this value
            fH = this.Height; //and this one too

            // Loop through the controls inside the  form i.e. Tabcontrol Container
            foreach (Control c in this.Controls)
                c.Tag = c.Name + "/" + c.Left + "/" + c.Top + "/" + 
                c.Width + "/" + c.Height + "/" + (float)c.Font.Size;
                if (c.GetType() == typeof(TabControl))

                    foreach (Control f in c.Controls)
                        foreach (Control j in f.Controls) //tabpage
                            j.Tag = j.Name + "/" + j.Left + "/" + j.Top + 
                            "/" + j.Width + "/" + j.Height + "/" + (float) j.Font.Size;

Points of Interest

The code described above is easy to use and understand. You must have a basic understanding of the C# language. Though the variables are not well formatted to many, I can guarantee that this will be comprehended easily.


  • March 17, 2014 - Added font resizing
  • March 14, 2014 - Initial release


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Philippines Philippines
A tech hobbyist and loves computer programming. When I design, I focused on the responsiveness GUI and at the same time in the performance(especially speedy network connectivity for DB). In my free time, I usually do android programming/ c# GUI coding.

VB/C# / C++ / C / / Java..


Comments and Discussions

QuestionExcellent example,help me,just copy paste and work great,Thank you !!! Pinmemberelim.projects1-Sep-14 1:14 
QuestionCase MDI PinmemberMember 1079973223-Jul-14 11:51 
AnswerRe: Case MDI Pinmemberkix4625-Jul-14 20:42 
QuestionGroupBox and Panel PinmemberMember 91175775-Apr-14 0:10 
AnswerRe: GroupBox and Panel Pinmemberkix465-Apr-14 4:26 
GeneralRe: GroupBox and Panel PinmemberMember 91175775-Apr-14 19:04 
QuestionDataGridView Column PinmemberMember 91175774-Apr-14 18:44 
AnswerRe: DataGridView Column Pinmemberkix465-Apr-14 4:22 
GeneralRe: DataGridView Column PinmemberMember 91175775-Apr-14 19:52 
GeneralRe: DataGridView Column PinmemberMember 91175776-Apr-14 0:29 
GeneralRe: DataGridView Column Pinmemberkix466-Apr-14 6:52 
QuestionExcelent bro !!! Pinmemberfborgheti1-Apr-14 9:08 
AnswerRe: Excelent bro !!! Pinmemberkix465-Apr-14 4:17 
QuestionWhich panel to use PinmemberKumartyr23-Mar-14 21:51 
AnswerRe: Which panel to use Pinmemberkix4624-Mar-14 21:43 
GeneralMy vote of 5 PinmemberChampion Chen17-Mar-14 17:32 
GeneralRe: My vote of 5 Pinmemberkix4617-Mar-14 19:08 
Question5+ bro! PinprofessionalVolynsky Alex15-Mar-14 4:19 
AnswerRe: 5+ bro! Pinmemberkix4615-Mar-14 23:30 
GeneralRe: 5+ bro! PinprofessionalVolynsky Alex16-Mar-14 0:39 
QuestionVariable names PinprotectorChristian Graus14-Mar-14 17:47 
AnswerRe: Variable names Pinmemberkix4615-Mar-14 1:45 
BugLogical, not bitwise OR PinprofessionalRavi Bhavnani14-Mar-14 7:38 
GeneralRe: Logical, not bitwise OR Pinmemberkix4614-Mar-14 13:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150414.1 | Last Updated 14 Mar 2014
Article Copyright 2014 by emerR46
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid