2013-10-23 31 views
0

我試圖在文本框中放置一個標籤。不過,我在標籤在文本框中正確定位時遇到了一些麻煩。我可以在文本框中顯示它,但那是我出現問題的地方。當我嘗試放置標籤時,它看起來不能正常工作。我有下面的代碼片段到TextBox的構造函數中添加標籤:TextBox ClientRectangle,DisplayRectangle或Bounds爲什麼不返回顯示大小?

 Label lblClear = new Label(); 
     lblClear.Text = "X"; 
     lblClear.Font = this.Font; 
     lblClear.Location = new Point(this.DisplayRectangle.X + (this.DisplayRectangle.Width - 15), this.Bounds.Y); 
     lblClear.Size = new Size(15, 15); 

     this.Controls.Add(lblClear); 

然而,這並沒有把它所有的方式到文本框的右邊,因爲我期待,而是在中間某個地方的文本框。爲什麼ClientRectangle,DisplayRectangle或Bound沒有返回文本框的大小,因爲我認爲它應該?底層的texbox矩形實際上比屏幕上顯示的小嗎?

任何幫助表示讚賞。謝謝。

編輯:這就是我講的截圖:

enter image description here

這裏是I類有:

public class SearchTextBox : TextBox 
{ 
    public SearchTextBox() 
    { 
     InitializeComponent(); 
     Label lblClear = new Label(); 
     lblClear.Text = "X"; 
     lblClear.Font = this.Font; 
     lblClear.Location = new Point(this.DisplayRectangle.X + (this.DisplayRectangle.Width - 15), this.Bounds.Y); 
     lblClear.Size = new Size(15, 15); 

     this.Controls.Add(lblClear); 
    } 
} 

編輯:我得到了它的工作,但是這隻有當我從我的表格中刪除文本框並重新添加它時,纔會產生影響...

+0

這看起來比在表單上放置的標準文本框寬**。 –

回答

0

你可以顯示它是如何工作的嗎?

如果你希望在調整TextBox大小時讓「X」保持在右側,那麼你需要將它錨定到右側。

這裏是我所得到的代碼:「我不是在找這個調整大小」

TextBox with 'X'

public class MyTextBox : TextBox 
{ 

    public MyTextBox() 
    { 
     Label lblClear = new Label(); 
     lblClear.Text = "X"; 
     lblClear.Font = this.Font; 
     lblClear.Location = new Point(this.DisplayRectangle.X + (this.DisplayRectangle.Width - 15), this.Bounds.Y); 
     lblClear.Size = new Size(15, 15); 

     this.Controls.Add(lblClear); 
    } 

} 
+0

我編輯了我的原始文章,包括一張圖片和我擁有的課程。我不想找這個來調整大小。這將是一個固定寬度的文本框,但我似乎無法讓標籤一路移動,即使我的構造函數代碼與您的構造函數代碼大致相同。 – dpCode

相關問題