How to get the textheight of a textbox?

limengchen 2003-11-15 12:07:51
I want to set the height property of a textbox to just the same as its textheight so that there won't be scrollbars. How to do that?
...全文
93 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
ssdjmcj8048 2003-11-21
  • 打赏
  • 举报
回复
用Control.CreateGraphics().MeasureString
limengchen 2003-11-20
  • 打赏
  • 举报
回复
to drag2003:
How can I show multiline strings if I set the multiline property to false?
gpontop 2003-11-18
  • 打赏
  • 举报
回复
两边加起来同时控制还有些难度,假如把“WordWrap”设置为true,就可以单行字符自动换行,然而控制行数就有些麻烦了,还没搞定,再看看
gpontop 2003-11-17
  • 打赏
  • 举报
回复
我只处理了高度,没处理宽度,其实处理方式是一样的。
drag2003 2003-11-17
  • 打赏
  • 举报
回复
You should set the Multiline property to "false" so as to make it's height fit into the size of it's texts.

就应该把"Multiline" 属性设成"false",这样就能使它的高度适应文本的大小。
limengchen 2003-11-17
  • 打赏
  • 举报
回复
to gpontop():
你那段代码不能处理自动换行。
gpontop 2003-11-17
  • 打赏
  • 举报
回复
This is my example ,try it please!
----------------------------------------------------------------------------------
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace WindowsApplication2
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.ComboBox comboBox1;
private System.Windows.Forms.FontDialog fontDialog1;
private System.Windows.Forms.Button button1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();

//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}

/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.comboBox1 = new System.Windows.Forms.ComboBox();
this.fontDialog1 = new System.Windows.Forms.FontDialog();
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.AcceptsReturn = true;
this.textBox1.BorderStyle = System.Windows.Forms.BorderStyle.None;
this.textBox1.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.textBox1.Location = new System.Drawing.Point(8, 48);
this.textBox1.Multiline = true;
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(272, 272);
this.textBox1.TabIndex = 0;
this.textBox1.Text = "textBox1";
this.textBox1.BorderStyleChanged += new System.EventHandler(this.textBox1_BorderStyleChanged);
this.textBox1.FontChanged += new System.EventHandler(this.textBox1_FontChanged);
this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);
//
// comboBox1
//
this.comboBox1.Items.AddRange(new object[] {
"None",
"FixedSingle",
"Fixed3D"});
this.comboBox1.Location = new System.Drawing.Point(8, 16);
this.comboBox1.Name = "comboBox1";
this.comboBox1.Size = new System.Drawing.Size(121, 20);
this.comboBox1.TabIndex = 2;
this.comboBox1.Text = "BorderStyle";
this.comboBox1.SelectedIndexChanged += new System.EventHandler(this.comboBox1_SelectedIndexChanged);
//
// button1
//
this.button1.Location = new System.Drawing.Point(208, 16);
this.button1.Name = "button1";
this.button1.TabIndex = 3;
this.button1.Text = "font";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(288, 325);
this.Controls.Add(this.button1);
this.Controls.Add(this.comboBox1);
this.Controls.Add(this.textBox1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void ChangeBounds()
{
int intLineNum = this.textBox1.Lines.GetLength(0);//The line number of the textbox
float fFontHeight = this.textBox1.Font.GetHeight();//The height of the currently used font in pixel
int intBorderHeight = 0;//The height of the currently used border style
Font font = this.textBox1.Font;

int ifontEmSize = font.FontFamily.GetEmHeight(font.Style);//The EmHeight of the font
int ifontDescent = font.FontFamily.GetCellDescent(font.Style);//Descent height of the font
int ifontAscent = font.FontFamily.GetCellAscent(font.Style);//Ascent height of the font
int ilineSpace = font.FontFamily.GetLineSpacing(font.Style);//Line distence of the font
double dCurRate = fFontHeight / (double) ilineSpace;//The ratio of pixel/DesignUnits
int iLineSpaceHeight = ilineSpace - ifontDescent - ifontAscent;//The height of external leading



switch(this.textBox1.BorderStyle)
// Set the Curently used border height
{
case System.Windows.Forms.BorderStyle.None:
intBorderHeight = 0;
break;
case System.Windows.Forms.BorderStyle.FixedSingle:
intBorderHeight = System.Windows.Forms.SystemInformation.BorderSize.Height;
break;
case System.Windows.Forms.BorderStyle.Fixed3D:
intBorderHeight = System.Windows.Forms.SystemInformation.Border3DSize.Height;
break;
}

if (intLineNum == 0)
//To make the minimised height to the height of one line
{
intLineNum = 1;
}

//Please refer to the following msdn topic
//----http://msdn.microsoft.com/library/en-us/cpguide/html/_gdiplus_obtaining_font_metrics_usecsharp.asp
double d = (double) (intLineNum * ifontEmSize + iLineSpaceHeight) * dCurRate;

int t = (int) ( d + 0.5 ) + 2 * intBorderHeight;
this.textBox1.Height = t;

}

private void textBox1_BorderStyleChanged(object sender, System.EventArgs e)
{
this.ChangeBounds();
}

private void comboBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
switch(this.comboBox1.SelectedItem.ToString())
{
case "None":
this.textBox1.BorderStyle = System.Windows.Forms.BorderStyle.None;
break;
case "FixedSingle":
this.textBox1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
break;
case "Fixed3D":
this.textBox1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
break;
}
this.textBox1.Update();
}

private void button1_Click(object sender, System.EventArgs e)
{
this.fontDialog1.ShowDialog(this.textBox1);
this.textBox1.Font = this.fontDialog1.Font;
}

private void textBox1_FontChanged(object sender, System.EventArgs e)
{
this.ChangeBounds();
}

private void textBox1_TextChanged(object sender, System.EventArgs e)
{
this.ChangeBounds();
}
}
}
FileNewExit 2003-11-16
  • 打赏
  • 举报
回复
TextBox.Font.Height
feigehao 2003-11-15
  • 打赏
  • 举报
回复
textBox1.Bounds.Height
gpontop 2003-11-15
  • 打赏
  • 举报
回复
You should set the Multiline property to "false" so as to make it's height fit into the size of it's texts.

110,571

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧