c# - C#中如何使用MeasureString设置表格列宽?

标签 c# string graphics width

我有一个非常简单的(我希望是基本的)问题。我正在为我公司的网站修改一些 C# 代码。该代码在固定列中为我绘制了一个表,其数据是从数据库中提取的。表的每一列的高度是固定的(当前),我需要改变它,所以如果字符串是一定的长度,因此换行,第二行文本是可见的(而不是被下一行隐藏) .

根据我的研究,我似乎可以使用 MeasureString(因为我知道字体和字符串)来查看字符串是否比我设置的表格列长/宽,如果是这样,则更改行的高度。但是,我对 C# 编程还很陌生(多年来除了网络内容外,总体上没有做过太多编程),所以我不确定如何实现所有这些。我有适当的逻辑,我知道如何改变高度,我只需要知道如何获得一个实际数字我可以使用逻辑来反对使用 MeasureString 方法(以及如何实例化我可能需要使用的任何变量和函数那个方法)。

最佳答案

我相信您需要为 MeasureString(string,font,int) 使用此重载:

The width parameter specifies the maximum value of the width component of the returned SizeF structure (Width). If the width parameter is less than the actual width of the string, the returned Width component is truncated to a value representing the maximum number of characters that will fit within the specified width. To accommodate the entire string, the returned Height component is adjusted to a value that allows displaying the string with character wrap.

-- 从上面链接的 MSDN 页面(强调我的)

// Measure string (you'll need to instansiate your own graphics object, 
// since you wont have PaintEventArgs)
SizeF stringSize = new SizeF();
stringSize = e.Graphics.MeasureString(measureString, stringFont, stringWidth);
int cellHeight = stringSize.Height;

关于c# - C#中如何使用MeasureString设置表格列宽?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11909119/

相关文章:

python - 如何根据窗口大小缩放图像?

algorithm - 给定一组点,找出这三个点中的任何一个是否共线

c# - 无法使用带有 Autofac 的 AutoMapper 4.2 解析 AutoMapper.IMapper

c# - 构建ASP.NET网站时如何查看Visual Studio生成的metaproj文件?

c# - 为什么 HttpWebRequest.BeginGetResponse() 同步完成?

c - 如何在C中打印出字符串的单个字符

Java2D的drawString很慢?

c# - 从 C# 读取非 .NET DLL 版本?

string - Boyer More exact 子串是否匹配动态规划的范例?

c - C中十六进制字符串到int的转换