List of Exercises

Auto Resize Controls in WinForms C#

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

namespace Auto_Resize_Control
{
  public partial class Form1 : Form
  {
    private Size formOriginalSize;
    private Rectangle recBut1;
    private Rectangle recTxt1;
    private Rectangle recPnl1;
    private Rectangle recRdo1;
    public Form1()
    {
      InitializeComponent();
      this.Resize += Form1_Resiz;
      formOriginalSize = this.Size;
      recBut1 = new Rectangle(button1.Location, button1.Size);
      recTxt1 = new Rectangle(textBox1.Location, textBox1.Size);
      recPnl1 = new Rectangle(panel1.Location, button1.Size);
      recRdo1 = new Rectangle(radioButton1.Location, textBox1.Size);
      textBox1.Multiline = true;
    }

    private void Form1_Resiz(object sender, EventArgs e)
    {
      resize_Control(button1, recBut1);
      resize_Control(textBox1, recTxt1);
      resize_Control(panel1, recPnl1);
      resize_Control(radioButton1, recRdo1);
    }
    private void resize_Control(Control c, Rectangle r)
    {
      float xRatio = (float)(this.Width) / (float)(formOriginalSize.Width);
      float yRatio = (float)(this.Height) / (float)(formOriginalSize.Height);
      int newX = (int)(r.X * xRatio);
      int newY = (int)(r.Y * yRatio);

      int newWidth = (int)(r.Width * xRatio);
      int newHeight = (int)(r.Height * yRatio);

      c.Location = new Point(newX, newY);
      c.Size = new Size(newWidth, newHeight);

    }
  }
}

		
	 


Download the project of Visual Studio 2013 in DropBox Download


Auto Resize Controls Based On Resize Form: demonstrates how to do it using Winforms C#


Video Thumbnail