TextRenderer.MeasureText() 和文本框最大文本长度 [英] TextRenderer.MeasureText() and textbox maximum text length

查看:40
本文介绍了TextRenderer.MeasureText() 和文本框最大文本长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个很长的字符串(~100k 个字符).我需要知道这个字符串的长度.我打电话

I have a long string (~100k characters). I need to know the length for this string. I call

Size s = TextRenderer.MeasureText(graphics, text, font);

但它返回的宽度等于 7.只有当文本长度 <= 43679 时它才返回正确的值!

But it returns width equals 7. Only if text length <= 43679 it returns correct value!

另外,如果我在文本框中插入文本,文本在文本框中是不可见的!我可以用鼠标选择文本,通过Ctrl+C"复制,但文本不可见.MaxLength 属性大于文本长度.

Also, if I insert the text in the text box, the text is not visible in the text box! I can select text with mouse, copy via "Ctrl+C", but text not visible. MaxLength property is more than the text length.

我查看了 msdn,但没有找到有关 MeasureText 和 TextBox 中使用的最大文本长度的任何信息.

I've looked on msdn, but haven't found any information about maximum text length which is used in MeasureText and TextBox.

在哪里可以找到关于此的文档?有没有办法增加最大文本长度?这些值是否取决于操作系统和计算机性能?

Where I can find documentation about this? Is there any way to increase maximum text length? Do these values depend on the operating system and computer performance?

推荐答案

给出了类似的例子 这里.他们还解决了 maxLength 问题msdn

Similar example was given Here . Also they addressed the maxLength issue on msdn

重要提示:请参阅文章中如何使用 MaxLength.

Important : See how the MaxLength is used in the article.

private static void DrawALineOfText(PaintEventArgs e)
{
    // Declare strings to render on the form. 
    string[] stringsToPaint = { "Tail", "Spin", " Toys" };

    // Declare fonts for rendering the strings.
    Font[] fonts = { new Font("Arial", 14, FontStyle.Regular), 
        new Font("Arial", 14, FontStyle.Italic), 
        new Font("Arial", 14, FontStyle.Regular) };

    Point startPoint = new Point(10, 10);

    // Set TextFormatFlags to no padding so strings are drawn together.
    TextFormatFlags flags = TextFormatFlags.NoPadding;

    // Declare a proposed size with dimensions set to the maximum integer value.
    Size proposedSize = new Size(int.MaxValue, int.MaxValue);

    // Measure each string with its font and NoPadding value and  
    // draw it to the form. 
    for (int i = 0; i < stringsToPaint.Length; i++)
    {
        Size size = TextRenderer.MeasureText(e.Graphics, stringsToPaint[i], 
            fonts[i], proposedSize, flags);
        Rectangle rect = new Rectangle(startPoint, size);
        TextRenderer.DrawText(e.Graphics, stringsToPaint[i], fonts[i],
            startPoint, Color.Black, flags);
        startPoint.X += size.Width;
    }

}

来源

这篇关于TextRenderer.MeasureText() 和文本框最大文本长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆