C# Automatic Resizing of Buttons in Windows Forms using C#| Resizeable | Auto Adjust Screen

preview_player
Показать описание
Ask any Question in comment Section
This code is very helpful for you if you are beginner and want to become a professional developer.i try my best to solve your problems
CLICK ON SEE MORE TO GET CODE
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 Responsiveness
{
public partial class Form1 : Form
{
private Rectangle textBox1OriginalRect;
private Rectangle button1OriginalRect;
private Rectangle button2OriginalRect;
private Rectangle button3OriginalRect;
private Rectangle button4OriginalRect;
private Rectangle button5OriginalRect;
private Rectangle panel1OriginalRect;
private Rectangle label1OriginalRect;
private Rectangle comboBox1OriginalRect;
private Rectangle dataGridView1OriginalRect;

private Size formOriginalSize;
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
formOriginalSize = this.Size;
textBox1OriginalRect = new Rectangle(textBox1.Location.X, textBox1.Location.Y, textBox1.Width, textBox1.Height);
button1OriginalRect = new Rectangle(button1.Location.X, button1.Location.Y, button1.Width, button1.Height);
button2OriginalRect = new Rectangle(button2.Location.X, button2.Location.Y, button2.Width, button2.Height);
button3OriginalRect = new Rectangle(button3.Location.X, button3.Location.Y, button3.Width, button3.Height);
button4OriginalRect = new Rectangle(button4.Location.X, button4.Location.Y, button4.Width, button4.Height);
button5OriginalRect = new Rectangle(button5.Location.X, button5.Location.Y, button5.Width, button5.Height);
panel1OriginalRect = new Rectangle(panel1.Location.X, panel1.Location.Y, panel1.Width, panel1.Height);
label1OriginalRect= new Rectangle(label1.Location.X, label1.Location.Y, label1.Width, label1.Height);
comboBox1OriginalRect = new Rectangle(comboBox1.Location.X, comboBox1.Location.Y, comboBox1.Width, comboBox1.Height);
dataGridView1OriginalRect = new Rectangle(dataGridView1.Location.X, dataGridView1.Location.Y, dataGridView1.Width, dataGridView1.Height);
}
private void resizeChildrenControls()
{
resizeControl(textBox1OriginalRect, textBox1);
resizeControl(button1OriginalRect, button1);
resizeControl(button2OriginalRect, button2);
resizeControl(button3OriginalRect, button3);
resizeControl(button4OriginalRect, button4);
resizeControl(button5OriginalRect, button5);
resizeControl(panel1OriginalRect, panel1);
resizeControl(label1OriginalRect, label1);
resizeControl(comboBox1OriginalRect, comboBox1);
resizeControl(dataGridView1OriginalRect, dataGridView1);

}
private void resizeControl(Rectangle OriginalControlRect, Control control)
{
float xRatio = (float)(this.Width) / (float)(formOriginalSize.Width);
float yRatio = (float)(this.Height) / (float)(formOriginalSize.Height);

int newX = (int)(OriginalControlRect.X * xRatio);
int newY = (int)(OriginalControlRect.Y * yRatio);

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

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

}

private void Form1_Resize(object sender, EventArgs e)
{
resizeChildrenControls();
}

private void button4_Click(object sender, EventArgs e)
{

}
}
}
Рекомендации по теме
Комментарии
Автор

Thanks for the code. Works well for me. May God bless you !

SushrutKanetkar
Автор

Hi I did almost exact same thing as you, but when i Maximise and Restore down my window, the contents get distorted and some overlap. Do you know what could be the problem?

ajmalishak
Автор

When i follow and try the same code, my buttons disappear somehow.same goes to my text box or my labels. Why is this so?

yt-lqpm
join shbcf.ru