2013-08-28 73 views
1

我有兩個彼此相鄰的標籤。這些標籤的值在運行時更改。 現在,如果第一個標籤的文字很長,那麼它與第二個標籤重疊。向右移動標籤以避免重疊

我想要的是第二個標籤右移避免重疊。

這可能嗎?

這裏是我的代碼:

// 
     // 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"; 

感謝

回答

1

一種方法可以調整labelShareSize的位置時的標籤的大小變化。以下是使用SizeChanged事件執行此操作的一些示例代碼。

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; 
    } 
}