How to dynamically align the margin of a label via windowns form code?

0

I have lblStatus that serves to assign 4 different values according to the query and each value has a different size, so there is no way I can sort directly by the position property. So I would like to set the direct position for the code for each Status.

Example:

if(string.Compare(objPed.PedStatus,"L") == 0)
                {
                    objPed.ConsultarNFPedido();
                    lblStatus.Text = " LIBERADO " + objPed.PedDtLib + " NOTA FISCAL: " + objPed.pedNFNumero + " SÉRIE: " + objPed.pedNFSerie + " ST: " + objPed.pedNFStatus;
                    lblStatus.ForeColor = Color.Green;
                }
                else if (string.Compare(objPed.PedStatus, "A") == 0)
                {
                    lblStatus.Text = "PENDÊNCIA DE FATURAMENTO";
                    lblStatus.ForeColor = Color.Blue;
                }
                else if (string.Compare(objPed.PedStatus, "E") == 0)
                {
                    lblStatus.Text = "PEDIDO EXCLUÍDO EM " + objPed.PedDtAlt + " POR " + objPed.PedIdAlt;
                    lblStatus.ForeColor = Color.DarkRed;
                }
                else if (string.Compare(objPed.PedStatus, "P") == 0)
                {
                    lblStatus.Text = "AGUARDANDO PAGAMENTO";
                    lblStatus.ForeColor = Color.Blue;

                }
    
asked by anonymous 01.12.2017 / 17:17

1 answer

3

Place the following properties in the Label:

//Auto size off, o tamanho da label, não vai seguir o tamanho do texto
label1.AutoSize = false;

//Dock = o tamanho da label, vai ficar ancorado no controle pai
label1.Dock = DockStyle.Fill; //ou DockStyle.Top; 

//Vai alinhar o texto sempre no meio
label1.TextAlign = ContentAlignment.MiddleCenter;

And then just put the label where you want it to be.

    
01.12.2017 / 17:22