1

tengo la Clase MARCADOR donde se muestran una serie de controles, y un método donde redimensiona los controles si se redimensiona la ventana, todo funciona bien,pero el evento

Marcador_Resize(object sender, EventArgs e) 

se ejecuta antes que mi evento

private void Marcador_Load(object sender, EventArgs e)

Alguien podría decirme por que? Dejo mi clase:

namespace MarcadorDeportivo
{
    public partial class Marcador : Form
    {
        private Rectangle pictureBoxLocal;
        private Rectangle pictureBoxVisit;
        private Rectangle txtPuntosLocal;
        private Rectangle txtPuntosVisit;
        private Rectangle txtNomLocal;
        private Rectangle txtNomVisit;
        private Rectangle txtCrono;
        private Rectangle lblLetreroLocal;
        private Rectangle lblLetreroVisit;
        private Rectangle lblGuion;
        private Rectangle txtParte;
        private Rectangle pictureBoxLogoSilicon;
        private Rectangle pictureBoxPubliBottom;

        private Size formOriginalSize;

        public Marcador()
        { 
            InitializeComponent();
        }

        private void Marcador_Load(object sender, EventArgs e)
        {
            Archivo archivo = new Archivo();
            String[] reso = archivo.leer_resolucion();

            Width = int.Parse(reso[0]);
            Height = int.Parse(reso[1]);

            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);
            txtNomLocal = new Rectangle(mTxtNomLocal.Location.X, mTxtNomLocal.Location.Y, mTxtNomLocal.Width, mTxtNomLocal.Height);
            txtNomVisit = new Rectangle(mTxtNomVisit.Location.X, mTxtNomVisit.Location.Y, mTxtNomVisit.Width, mTxtNomVisit.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);
            txtParte = new Rectangle(mTxtParte.Location.X, mTxtParte.Location.Y, mTxtParte.Width, mTxtParte.Height);
            pictureBoxLogoSilicon = new Rectangle(mPictureBoxLogoSilicon.Location.X, mPictureBoxLogoSilicon.Location.Y, mPictureBoxLogoSilicon.Width, mPictureBoxLogoSilicon.Height);
            pictureBoxPubliBottom = new Rectangle(mPictureBoxPubliBottom.Location.X, mPictureBoxPubliBottom.Location.Y, mPictureBoxPubliBottom.Width, mPictureBoxPubliBottom.Height);
        }

        private void resizeChildrenControls()
        {
            resizeControl(pictureBoxLocal, mPictureBoxLocal);
            resizeControl(pictureBoxVisit, mPictureBoxVisit);
            resizeControl(txtPuntosLocal, mTxtPuntosLocal);
            resizeControl(txtPuntosVisit, mTxtPuntosVisit);
            resizeControl(txtNomLocal, mTxtNomLocal);
            resizeControl(txtNomVisit, mTxtNomVisit);
            resizeControl(txtCrono, mTxtCrono);
            resizeControl(lblLetreroLocal, mLblLetreroLocal);
            resizeControl(lblLetreroVisit, mLblLetreroVisit);
            resizeControl(lblGuion, mLblGuion);
            resizeControl(txtParte, mTxtParte);
            resizeControl(pictureBoxLogoSilicon, mPictureBoxLogoSilicon);
            resizeControl(pictureBoxPubliBottom, mPictureBoxPubliBottom);
        }

        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);
            try
            {
                control.Font = new Font(control.Font.FontFamily, newHeight/2);
            }
            catch { }
        }

        public void cargarDatos(String escudoLocal, String escudoVisit, String puntosLocal, String puntosVisit, String nomLocal, String nomVisit, String banner)
        {
            mPictureBoxLocal.ImageLocation = escudoLocal;
            mPictureBoxVisit.ImageLocation = escudoVisit;
            mTxtPuntosLocal.Text = puntosLocal;
            mTxtPuntosVisit.Text = puntosVisit;
            mTxtNomLocal.Text = nomLocal.ToUpper();
            mTxtNomVisit.Text = nomVisit.ToUpper();
            if(banner != null)
            {
                mPictureBoxPubliBottom.ImageLocation = banner;
            }         
        }

        public void set_tamano(String ancho, String alto)
        {
            Width = int.Parse(ancho);
            Height = int.Parse(alto);
        }
        public void setCrono(String tiempo, String parte)
        {
            mTxtCrono.Text = tiempo;
            mTxtParte.Text = parte;
            mTxtCrono.Show();
        }

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

    }
}

1 respuesta 1

2

El tema es que la ventana se crea antes de ejecutar el Load por lo que al tomar dimensiones se aplica el Resize

Quizás debas definir un flag que permite verificar cuando se aplico el Load y recién allí tome en cuenta el Resize

public partial class Marcador : Form
{
    //resto codigo

    private loadCompleted = false;

    public Marcador()
    { 
        InitializeComponent();
    }

    private void Marcador_Load(object sender, EventArgs e)
    {

        //resto codigo

        loadCompleted = true;
    }

    private void Marcador_Resize(object sender, EventArgs e)
    {
        if(loadCompleted)
        {
            resizeChildrenControls();
        }
    }

}

veras como la variable loadCompleted define cuando actual el evento

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.