[英]C# Resize textbox to fit content
我正在編寫一個程序,用戶應該能夠在TextBox
編寫文本。 我希望TextBox
自行調整大小,使其適合內容。 我嘗試了以下方法:
private void textBoxTitle_TextChanged(object sender, TextChangedEventArgs e)
{
System.Drawing.Font myFont = new System.Drawing.Font("Verdana", 8);
System.Drawing.SizeF mySize = e.Graphics.MeasureString("This is a test", myFont);
this.textBoxTitle.Width = (int)Math.Round(mySize.Width, 0);
}
我收到一條錯誤消息,說Graphics
不適用於TextChangedEventArgs
。 還有另一種方法可以調整TextBox
大小嗎?
您應該嘗試類似下面的代碼。 它對我很有用。
private void textBox1_TextChanged(object sender, EventArgs e)
{
Size size = TextRenderer.MeasureText(textBox1.Text, textBox1.Font);
textBox1.Width = size.Width;
textBox1.Height = size.Height;
}
有關更多信息,請參閱TextRenderer.MeasureText()
我正在添加這個答案,因為我沒有看到在其他任何一個中討論的文本框的fixed width
方面。 如果您的文本框寬度固定,並且只想調整其高度,則可以執行以下操作:
像這樣的東西給出了文本的高度,因為它是如何在多行自動換行文本框本身中繪制的:
SizeF MessageSize = MyTextBoxControl.CreateGraphics()
.MeasureString(MyTextBoxControl.Text,
MyTextBoxControl.Font,
MyTextBoxControl.Width,
new StringFormat(0));
我不確定StringFormat
應該是什么,但StringFormatFlags
的值似乎不適用於默認的TextBox
組成。
現在使用MessageSize.Height
您知道文本框中文本的高度。
我遇到了同樣的問題,我以更簡單的方式解決了它。
我使用了 Label 控件的 AutoSize 屬性。我在表單中添加了一個不可見的標簽,將其 AutoSize 屬性設置為 True。 當我需要更改 TextBox 的大小時,我使用以下代碼:
MyLabel.Text = MyTextBox.Text;
MyTextBox.Size = MyLabel.Size;
我設置了標簽的最大和最小尺寸以獲得更好的結果。 玩得開心
您綁定到錯誤的事件,並且您不能在TextChangedEventArgs
對象中使用圖形對象。
嘗試使用 TextChanged 事件。 以下代碼段正在運行:
public Form1()
{
InitializeComponent();
this.textBox1.TextChanged += new EventHandler(textBox1_TextChanged);
}
void textBox1_TextChanged(object sender, EventArgs e)
{
System.Drawing.SizeF mySize = new System.Drawing.SizeF();
// Use the textbox font
System.Drawing.Font myFont = textBox1.Font;
using (Graphics g = this.CreateGraphics())
{
// Get the size given the string and the font
mySize = g.MeasureString(textBox1.Text, myFont);
}
// Resize the textbox
this.textBox1.Width = (int)Math.Round(mySize.Width, 0);
}
}
不管目的是什么。
如果文本框的大小應該根據字符串動態設置,應該是這個框內的文本,沒有很好的選項。
原因: MeasureString 使用通常的字符串格式化程序作為其自身寬度和高度的分隔符。 手段,回車和換行也被解析。 導致 sizeF.Width 和 sizeF.Height。
根據字符串(及其字體和行數),這兩個變量都可以攜帶值,這些值有時無法用作文本框的寬度/高度值(因為它們可能大於父窗體的值,這會調整文本框大小,左邊框和下邊框超出父窗體的大小)。
一些解決方案仍然可用,這取決於目標,人們想要實現。
一個想法是:在設計器中創建一個文本框,大小 = 100 X 100。啟用自動換行。
在文本框的OnTextChanged事件處理程序中,我們只是將文本框的寬度調整為一個由我們自己定義的值(例如 parentform.Width 或其他硬值)。
這將導致自動換行重新計算文本框中的字符串,並且這將重新排列文本框中的所有字符,因為啟用了自動換行。
例如,可以將文本框的高度設置為 parentform.Height。
但是,更好:根據方法 texbox.GetPositionFromCharIndex(textbox.TextLength -1) 的 ReturnValue (Point) 的 Y 值動態設置高度。 然后,用Math.Min()確定哪個更小( parentform.Height 或 Point.Y ),並將文本框大小重置為 new Size(previousDeterminedWidth, nowDeterminedHeight)。
請記住(如果啟用了滾動條)將大約 17 個像素添加到您的寬度計算中。
此致
您將需要使用表單的 CreateGraphics() 方法來創建 Graphics 實例來測量字符串。
TextChangedEventArgs
類沒有Graphics
屬性,這是傳遞給Paint
事件處理程序的PaintEventArgs
類的屬性
嘗試這個:
using System.Drawing;
...
private void textBoxTitle_TextChanged(object sender, TextChangedEventArgs e)
{
// Determine the correct size for the text box based on its text length
// get the current text box safely
TextBox tb = sender as TextBox;
if (tb == null) return;
SizeF stringSize;
// create a graphics object for this form
using(Graphics gfx = this.CreateGraphics())
{
// Get the size given the string and the font
stringSize = gfx.MeasureString(tb.Text, tb.Font);
}
// Resize the textbox
tb.Width = (int)Math.Round(stringSize.Width, 0);
}
本質上,您為表單創建自己的Graphics
對象,然后根據 TextBox 的文本和字體對其進行測量。 using
將正確處理Graphics
對象 - 您之前的代碼會嚴重泄漏!
首先,創建方法使 TextBox 適合其內容。
private void AutoSizeTextBox(TextBox txt)
{
const int x_margin = 0;
const int y_margin = 2;
Size size = TextRenderer.MeasureText(txt.Text, txt.Font);
txt.ClientSize =
new Size(size.Width + x_margin, size.Height + y_margin);
}
然后使用TextChanged事件處理程序調用AutoSizeTextBox()函數以在文本更改時使 TextBox 適合其文本。
private void txtContents_TextChanged(object sender, EventArgs e)
{
AutoSizeTextBox(sender as TextBox);
}
這就是全部,有關更多信息:
您是否嘗試設置yourTextBox.AutoSize = true;
? 此屬性可能隱藏在 GUI 設計器中,但您可以在InitializeComponent();
之后立即在表單構造函數中設置它InitializeComponent();
稱呼。
Graphics.Measure 字符串你可以做 o PaintEventArgs ,而不是TextChangedEventArgs
我想你想要的是這個
System.Drawing.Font myFont = new System.Drawing.Font("Verdana", 8);
Graphics graphics = this.CreateGraphics();
SizeF textSize = graphics.MeasureString("This is a test", myFont);
問題是你不能通過簡單地分配它來創建一個Graphics對象,因為它沒有公共構造函數,所以你最好去使用 TextRenderer.MeasureText,就像在http://msdn.microsoft.com/en-us/ 中所做的那樣圖書館/y4xdbe66.aspx
TextRenderer 不太准確,因為它使用 GDI 而 Graphics 使用 GDI+,所以也許您應該在從 Width 屬性獲得的值上留一點余量。
希望這可以幫助
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.