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

Asked

Viewed 162 times

0

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

Example:

inserir a descrição da imagem aqui

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;

                }

1 answer

3


Place the following properties on 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.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.