C# 将内容自动调整到固定大小的文本框控件中

C# 将内容自动调整到固定大小的文本框控件中,c#,winforms,textbox,C#,Winforms,Textbox,我可以找到大量的例子来调整文本框的大小以适应它的内容,但不是相反。我有一个WinForm“弹出窗口”,大小为fized,包含一个墙纸缩略图和一个文本框,其中包含墙纸标题。有时,标题可以很短,也可以很长,从表格的末尾掉下来: 表单由另一个方法传递的信息填充: this.BringToFront(); this.txtWallpaperTitle.Text = title; this.lnkWallpaper.Text = "http://www.reddit.com/"

我可以找到大量的例子来调整文本框的大小以适应它的内容,但不是相反。我有一个WinForm“弹出窗口”,大小为fized,包含一个墙纸缩略图和一个文本框,其中包含墙纸标题。有时,标题可以很短,也可以很长,从表格的末尾掉下来:

表单由另一个方法传递的信息填充:

    this.BringToFront();
    this.txtWallpaperTitle.Text = title;
    this.lnkWallpaper.Text = "http://www.reddit.com/" + threadid;

    Bitmap img = new Bitmap(Properties.Settings.Default.currentWallpaperFile);            
    this.imgWallpaper.BackgroundImage = img;
    this.imgWallpaper.BackgroundImageLayout = ImageLayout.Stretch;

当前,
TextBox
text sdize是通过设计器设置的。是否有任何方法可以自动调整文本大小,使其适合
文本框
控件

可能通过这种方式或类似方式处理
TextChanged
事件可以解决此问题

private void textBox1_TextChanged(object sender, EventArgs e)
{
    if (textBox1.Text != "".Trim())
    {
        int w = TextRenderer.MeasureText(textBox1.Text, textBox1.Font).Width;
        int h = TextRenderer.MeasureText(textBox1.Text, textBox1.Font).Height;
        int sizeContent = w * h;
        int sizeTb = (textBox1.Width * textBox1.Height);
        if (sizeContent < sizeTb)
        {
            while (sizeContent < sizeTb)
            {
                textBox1.Font = new Font(textBox1.Font.FontFamily, textBox1.Font.SizeInPoints + 1);
                w = TextRenderer.MeasureText(textBox1.Text, textBox1.Font).Width;
                h = TextRenderer.MeasureText(textBox1.Text, textBox1.Font).Height;
                sizeContent = w * h;
            }
        }
        if (sizeContent >= sizeTb)
        {
            while (sizeContent >= sizeTb)
            {
                textBox1.Font = new Font(textBox1.Font.FontFamily, textBox1.Font.SizeInPoints - 1);
                w = TextRenderer.MeasureText(textBox1.Text, textBox1.Font).Width;
                h = TextRenderer.MeasureText(textBox1.Text, textBox1.Font).Height;
                sizeContent = w * h;
            }
        }
    }
}
private void textBox1\u TextChanged(对象发送者,事件参数e)
{
如果(textBox1.Text!=“”.Trim())
{
int w=TextRenderer.MeasureText(textBox1.Text,textBox1.Font).Width;
inth=TextRenderer.MeasureText(textBox1.Text,textBox1.Font).Height;
int-sizeContent=w*h;
int sizeTb=(textBox1.Width*textBox1.Height);
if(sizeContent=sizeTb)
{
而(sizeContent>=sizeTb)
{
textBox1.Font=新字体(textBox1.Font.FontFamily,textBox1.Font.SizeInPoints-1);
w=TextRenderer.MeasureText(textBox1.Text,textBox1.Font).Width;
h=TextRenderer.MeasureText(textBox1.Text,textBox1.Font).Height;
sizeContent=w*h;
}
}
}
}