I have a form with several controls, label, textBox, etc. I need to resize all the controls when the form is resized and it seems that I have achieved it, but even if the controls are resized, the size of their font does not seem to change, it stays the same size so it does not adapt, how can I do so that the size of my label, textbox etc adapt to the size of the control itself? My code is:
public partial class Marcador : Form
{
private Rectangle pictureBoxLocal;
private Rectangle pictureBoxVisit;
private Rectangle txtPuntosLocal;
private Rectangle txtPuntosVisit;
private Rectangle lblNomLocal;
private Rectangle lblNomVisit;
private Rectangle txtCrono;
private Rectangle lblLetreroLocal;
private Rectangle lblLetreroVisit;
private Rectangle lblGuion;
private Size formOriginalSize;
public Marcador()
{
InitializeComponent();
}
private void Marcador_Load(object sender, EventArgs e)
{
formOriginalSize = this.Size;
pictureBoxLocal = new Rectangle(mPictureBoxLocal.Location.X, mPictureBoxLocal.Location.Y, mPictureBoxLocal.Width, mPictureBoxLocal.Height);
pictureBoxVisit = new Rectangle(mPictureBoxVisit.Location.X, mPictureBoxVisit.Location.Y, mPictureBoxVisit.Width, mPictureBoxVisit.Height);
txtPuntosLocal = new Rectangle(mTxtPuntosLocal.Location.X, mTxtPuntosLocal.Location.Y, mTxtPuntosLocal.Width, mTxtPuntosLocal.Height);
txtPuntosVisit = new Rectangle(mTxtPuntosVisit.Location.X, mTxtPuntosVisit.Location.Y, mTxtPuntosVisit.Width, mTxtPuntosVisit.Height);
lblNomLocal = new Rectangle(mLblNomLocal.Location.X, mLblNomLocal.Location.Y, mLblNomLocal.Width, mLblNomLocal.Height);
lblNomVisit = new Rectangle(mLblNomVisit.Location.X, mLblNomVisit.Location.Y, mLblNomVisit.Width, mLblNomVisit.Height);
txtCrono = new Rectangle(mTxtCrono.Location.X, mTxtCrono.Location.Y, mTxtCrono.Width, mTxtCrono.Height);
lblLetreroLocal = new Rectangle(mLblLetreroLocal.Location.X, mLblLetreroLocal.Location.Y, mLblLetreroLocal.Width, mLblLetreroLocal.Height);
lblLetreroVisit = new Rectangle(mLblLetreroVisit.Location.X, mLblLetreroVisit.Location.Y, mLblLetreroVisit.Width, mLblLetreroVisit.Height);
lblGuion = new Rectangle(mLblGuion.Location.X, mLblGuion.Location.Y, mLblGuion.Width, mLblGuion.Height);
}
private void resizeChildrenControls()
{
resizeControl(pictureBoxLocal, mPictureBoxLocal);
resizeControl(pictureBoxVisit, mPictureBoxVisit);
resizeControl(txtPuntosLocal, mTxtPuntosLocal);
resizeControl(txtPuntosVisit, mTxtPuntosVisit);
resizeControl(lblNomLocal, mLblNomLocal);
resizeControl(lblNomVisit, mLblNomVisit);
resizeControl(txtCrono, mTxtCrono);
resizeControl(lblLetreroLocal, mLblLetreroLocal);
resizeControl(lblLetreroVisit, mLblLetreroVisit);
resizeControl(lblGuion, mLblGuion);
}
private void resizeControl(Rectangle originalControlRect, System.Windows.Forms.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 Marcador_Resize(object sender, EventArgs e)
{
resizeChildrenControls();
}
In the end I was able to solve it by myself, I leave the example in case it helps someone.
To change the font is with:
Leaving the resizeControl method like this:
I have included the Try because it could find controls that do not have the FONT property.
Thank you