Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 我如何确定一根绳子在一定宽度内能容纳多少?_C#_Winforms_String - Fatal编程技术网

C# 我如何确定一根绳子在一定宽度内能容纳多少?

C# 我如何确定一根绳子在一定宽度内能容纳多少?,c#,winforms,string,C#,Winforms,String,嗨 我有一个问题,调整文本的长度,使其适合形式的长度。 在我的表格中,我放置了一个标签,负责显示一些长文本。然而,文本可能太长,不适合形式。这就是为什么我想缩短这段文字并添加。。。到最后 问题是,我不知道如何有效地计算最大长度的子串,它将符合形式。到目前为止,我只检查给定的文本是否太长(我使用Graphicsclass中的方法MeasureString)。 计算符合表单的最大子字符串(宽度width

嗨 我有一个问题,调整文本的长度,使其适合形式的长度。 在我的表格中,我放置了一个标签,负责显示一些长文本。然而,文本可能太长,不适合形式。这就是为什么我想缩短这段文字并添加。。。到最后

问题是,我不知道如何有效地计算最大长度的子串,它将符合形式。到目前为止,我只检查给定的文本是否太长(我使用
Graphics
class中的方法
MeasureString
)。
计算符合表单的最大子字符串(宽度
width
)的最佳方法是什么?

据我所知,唯一可靠的方法是将字符串缩短到合适的长度,您可以使用
MeasureString()
进行检查,不幸的是没有相反的方法(给定宽度,这根绳子能容纳多少),所以您必须自己构建它。

自从今天我自己遇到这个问题以来,我有一个解决方案。我的ToolStripStatusLabel设置为“自动大小”,但它超出了父控件的宽度,因此隐藏了自己。我的解决方案专门用于该控件。因为这种情况下,该控件不会提供MaxWidth属性或AutoEllipsis属性。它也不会为我生成图形对象,因为它不是控件族的一部分。此方法可以更改为使用两个控件对象,除了参数之外没有太多更改。我的选项非常有限,因此我制作了此方法:

/// <summary>
/// If a child ToolStripStatusLabel is wider than it's parent then this method will attempt to
/// make the child's text fit inside of the parent's boundaries. An ellipsis can be appended
/// at the end of the text to indicate that it has been truncated to fit.
/// </summary>
/// <param name="child">Child ToolStripStatusLabel</param>
/// <param name="parent">Parent control where the ToolStripStatusLabel resides</param>
/// <param name="appendEllipsis">Append an "..." to the end of the truncated text</param>
public static void TruncateChildTextAccordingToControlWidth(ToolStripStatusLabel child, Control parent, bool appendEllipsis)
{
    //If the child's width is greater than that of the parent's
    if(child.Size.Width > parent.Size.Width)
    {
        //Get the number of times that the child is oversized [child/parent]
        decimal decOverSized = (decimal)(child.Size.Width) / (decimal)(parent.Size.Width);

        //Get the new Text length based on the number of times that the child's width is oversized.
        int intNewLength = (int)(child.Text.Length / (2M * decOverSized)); //Doubling as a buffer (Magic Number).

        //If the ellipsis is to be appended
        if(appendEllipsis) //then 3 more characters need to be removed to make room for it.
            intNewLength = intNewLength - 3;

        //If the new length is negative for whatever reason
        if(intNewLength < 0) 
            intNewLength = 0; //Then default it to zero

        //Truncate the child's Text accordingly
        child.Text = child.Text.Substring(0, intNewLength);

        //If the ellipsis is to be appended
        if(appendEllipsis) //Then do this last
            child.Text += "...";
    }
}
//
///如果子ToolStripStatusLabel比其父ToolStripStatusLabel宽,则此方法将尝试
///使孩子的文本适合父母的边界。可以附加省略号
///在文本的末尾,表示已将其截断以适合。
/// 
///子ToolStripStatusLabel
///ToolStripStatusLabel所在的父控件
///在截断文本的末尾附加“…”
public static void TruncateChildTextAccordingToControlWidth(ToolStripStatusLabel子控件、控件父控件、bool appendEllipsis)
{
//如果子对象的宽度大于父对象的宽度
if(child.Size.Width>parent.Size.Width)
{
//获取子项过大的次数[子项/父项]
decimal去版本=(decimal)(child.Size.Width)/(decimal)(parent.Size.Width);
//根据子项宽度过大的次数获取新文本长度。
int intNewLength=(int)(child.Text.Length/(2M*非版本化));//作为缓冲区(幻数)加倍。
//如果要添加省略号
if(appendEllipsis)//则需要再删除3个字符以腾出空间。
intNewLength=intNewLength-3;
//如果出于任何原因,新长度为负值
if(intNewLength<0)
intNewLength=0;//然后将其默认为零
//相应地截断孩子的文本
child.Text=child.Text.Substring(0,intNewLength);
//如果要添加省略号
if(appendEllipsis)//然后最后执行此操作
child.Text+=“…”;
}
}
我将是第一个指出我做了一件可怕的事情,并包括了一个神奇的数字!我道歉,但2M是一个缓冲区。我还没有找到更好的方法来做到这一点,当/如果我找到一种方法,我一定会回来并在这里更新它。我需要这个神奇的数字,因为我计算的百分比是准确的,但为了适合孩子在父母的内部控制足够好,我需要大约两倍于被返回的东西


我敢肯定,根据标签字体大小和其他因素,这不会完全准确,但这是一个开始。

您可以逐字书写文本,同时将光标点向前移动,直到到达行尾(表单宽度).我有一个类似的问题,但我在表单上绘制文本,我更改了代码以满足您的需要

private bool WriteWord(string word)
{
    Size size = TextRenderer.MeasureText(word + "  ", Font);
    if (cursor.X + size.Width > Width)
    {
        // you reached the end of the line
        return false;
    }
    label1.Text += word + "  "; // add the word to label.
    cursor.X += size.Width;
    return true;
}
Point Cursor;
private string FindMaxSubstring(string text)
{
    string[] tokens = text.Split(new string[] { " "}); 
    string substring ="";
    Cursor.X = 0;
    foreach (var t in tokens)
    {
        if (!string.IsNullOrEmpty(t.Trim()))
        {
            if (!WriteWord(t))
                return substring;
            substring += t;
        }
    }
}

将标签的
AutoSize
属性设置为true,然后将文本拆分为令牌,并将它们逐个令牌添加到
标签.text
直到
标签.Width>宽度。

我使用此代码通过从字符串开头剥离字符来调整状态栏文本。也许您可以根据需要修改它

        status.Text = status.Text + " | " + newText;
        while (TextRenderer.MeasureText(status.Text, status.Font).Width > status.Width)
            status.Text = status.Text.Substring(1);

有一段时间,我认为这是一个笑话,问一根绳子有多长……也许你只需要设置这个属性。你必须考虑到字母没有相同的大小(在许多字体中,除了单行空间)。:例如,“W”没有“i”小,所以你通常不能假设一个字母数相同的单词大小相同。@Rob-oooh,shiny!我今天学到了一些东西:)@Rob:谢谢,我错过了那个。如果你对字符串进行二元搜索,就不难找出它在表单上的长度。