Pergunta

I have two labels next to each other. The values of these labels are changed at run time. Now if the text of first label is long then it overlaps the second label.

What i want is the second label to shift right to avoid overlaping.

Is this possible?

Here is my code:

 // 
        // labelName
        // 
        this.labelName.AutoSize = true;
        this.labelName.BackColor = System.Drawing.Color.Transparent;
        this.labelName.Font = new System.Drawing.Font("Tahoma", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
        this.labelName.ForeColor = System.Drawing.Color.White;
        this.labelName.Location = new System.Drawing.Point(6, 1);
        this.labelName.Name = "labelName";
        this.labelName.Size = new System.Drawing.Size(93, 16);
        this.labelName.TabIndex = 55;
        this.labelName.Tag = "useHeaderImage Core";
        this.labelName.Text = "Name";
        // 
        // labelShareSize
        // 
        this.labelShareSize.AutoSize = true;
        this.labelShareSize.BackColor = System.Drawing.Color.Transparent;
        this.labelShareSize.Font = new System.Drawing.Font("Tahoma", 8.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel, ((byte)(0)));
        this.labelShareSize.ForeColor = System.Drawing.Color.White;
        this.labelShareSize.Location = new System.Drawing.Point(206, 3);
        this.labelShareSize.Name = "labelShareSize";
        this.labelShareSize.Size = new System.Drawing.Size(46, 11);
        this.labelShareSize.TabIndex = 56;
        this.labelShareSize.Tag = "useHeaderImage Core";
        this.labelShareSize.Text = "ShareSize";

Thanks

Foi útil?

Solução

One approach could be to adjust the position of labelShareSize when the size of labelName changes. Here's some example code using the SizeChanged event to do this.

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        // attach this event handler before the text/size changes
        labelName.SizeChanged += labelName_SizeChanged;

        labelName.Text = "really really really really long text gets set here.........................";
    }

    void labelName_SizeChanged(object sender, EventArgs e)
    {
        AdjustLabelPosition();
    }

    private void AdjustLabelPosition()
    {
        if (labelShareSize.Left < labelName.Location.X + labelName.Width)
            labelShareSize.Left = labelName.Location.X + labelName.Width;
    }
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top