C# 根据控件的宽度决定要显示的字符串

C# 根据控件的宽度决定要显示的字符串,c#,winforms,C#,Winforms,我在SplitterPanel中有一个列表框。我已经重写了它的MeasureItem()和DrawItem()方法 我想做的是,根据Listbox.Width,返回整个字符串或它的缩短版本,如“Dance toni…” 我浏览了SO,发现了两个与我的问题相关的问题。其中一个问题是测量文本的宽度,我正在使用DrawItem()中的e.Graphics.MeasureString()进行此操作 摘要-我有列表框的宽度和字符串的宽度(以像素为单位)。如果字符串短于列表框的宽度,我希望显示整个字符串。但

我在SplitterPanel中有一个列表框。我已经重写了它的MeasureItem()和DrawItem()方法

我想做的是,根据Listbox.Width,返回整个字符串或它的缩短版本,如“Dance toni…”

我浏览了SO,发现了两个与我的问题相关的问题。其中一个问题是测量文本的宽度,我正在使用DrawItem()中的e.Graphics.MeasureString()进行此操作

摘要-我有列表框的宽度和字符串的宽度(以像素为单位)。如果字符串短于列表框的宽度,我希望显示整个字符串。但是,如果它更长,我想返回一个类似“Hello every…”的版本,该版本适合列表框的宽度

到目前为止,我已经:

        private string FitText(string s)
    {
        int width = (TextRenderer.MeasureText(s, titleFont)).Width;
        if (width <= mailList.Width)
        {
            return s;
        }
        else if (width > mailList.Width)
        {
            // What goes here?
        }
    }
专用字符串FitText(字符串s)
{
int width=(textrender.MeasureText(s,titleFont)).width;
if(邮件列表宽度)
{
//这里有什么?
}
}
我很确定这只是简单的数学问题,但我还是搞不懂。


这是我要用的伪代码

1) 以某种形式缩短字符串(删除最后一个字符/单词)
2) 根据
宽度重新测试长度

3) 如果不是,则插入有效的重复项
4) 如果字符串太短,请使用一些默认形式

String s = "A long string that you're trying to fit into the button."
while (width > mailList.Width) {
   s = s.SubString(0,s.lastIndexOf(" ")-1); //Change this to whatever you'd want to shorten the string by
  width = (TextRenderer.MeasureText(s, titleFont)).Width; 
  if (width < 5) { //Some value indicating it's too short
    s = "Button...";
    break;
  }
}

return s;
String s=“一个试图放入按钮的长字符串。”
while(宽度>邮件列表宽度){
s=s.SubString(0,s.lastIndexOf(“”-1);//将其更改为希望将字符串缩短多少
宽度=(TextRenderer.MeasureText(s,titleFont)).width;
如果(宽度<5){//某个值表示它太短
s=“按钮…”;
打破
}
}
返回s;

这是我要使用的伪代码

1) 以某种形式缩短字符串(删除最后一个字符/单词)
2) 根据
宽度重新测试长度

3) 如果不是,则插入有效的重复项
4) 如果字符串太短,请使用一些默认形式

String s = "A long string that you're trying to fit into the button."
while (width > mailList.Width) {
   s = s.SubString(0,s.lastIndexOf(" ")-1); //Change this to whatever you'd want to shorten the string by
  width = (TextRenderer.MeasureText(s, titleFont)).Width; 
  if (width < 5) { //Some value indicating it's too short
    s = "Button...";
    break;
  }
}

return s;
String s=“一个试图放入按钮的长字符串。”
while(宽度>邮件列表宽度){
s=s.SubString(0,s.lastIndexOf(“”-1);//将其更改为希望将字符串缩短多少
宽度=(TextRenderer.MeasureText(s,titleFont)).width;
如果(宽度<5){//某个值表示它太短
s=“按钮…”;
打破
}
}
返回s;

我认为您需要检查它是否合适,如果合适,请返回整个字符串,否则请修改代码以运行一个循环,不断测量s的大小减去一个字符,再加上省略号,直到合适或不再剩下字符,然后返回该值,否则仅返回省略号

string EllipsisString = "..."; // you could also just set this as the unicode ellipsis char if that displays properly

private string FitText(string s)
{
    bool WidthFound = true;
    string TestString = s;
    string StringToReturn = s;

    int width = (TextRenderer.MeasureText(s, titleFont)).Width;

    if (width > mailList.Width)
    {
        WidthFound = false;

        for (int i=1; i < s.Length; ++i)
        {
           TestString = s.Substring(0, s.Length - i) + EllipsisString;
           width = (TextRenderer.MeasureText(TestString, titleFont)).Width;

           if (width <= mailList.Width)
           {
              StringToReturn = TestString;
              WidthFound = true;
              break;
           }
        }
    }

    if (WidthFound)
        return StringToReturn;
    else
        return EllipsisString;
}
string EllipsisString=“…”;//如果显示正确,也可以将其设置为unicode省略号字符
专用字符串FitText(字符串s)
{
bool WidthFound=true;
字符串TestString=s;
字符串StringToReturn=s;
int width=(textrender.MeasureText(s,titleFont)).width;
如果(宽度>邮件列表宽度)
{
发现的宽度=假;
对于(int i=1;iif(width我认为您需要检查它是否合适,如果合适,返回整个字符串,否则修改代码以运行一个循环,不断测量s的大小减去一个字符,再加上省略号,直到它合适或不再剩下字符,然后返回该值,否则只返回省略号

string EllipsisString = "..."; // you could also just set this as the unicode ellipsis char if that displays properly

private string FitText(string s)
{
    bool WidthFound = true;
    string TestString = s;
    string StringToReturn = s;

    int width = (TextRenderer.MeasureText(s, titleFont)).Width;

    if (width > mailList.Width)
    {
        WidthFound = false;

        for (int i=1; i < s.Length; ++i)
        {
           TestString = s.Substring(0, s.Length - i) + EllipsisString;
           width = (TextRenderer.MeasureText(TestString, titleFont)).Width;

           if (width <= mailList.Width)
           {
              StringToReturn = TestString;
              WidthFound = true;
              break;
           }
        }
    }

    if (WidthFound)
        return StringToReturn;
    else
        return EllipsisString;
}
string EllipsisString=“…”;//如果正确显示,也可以将其设置为unicode省略号字符
专用字符串FitText(字符串s)
{
bool WidthFound=true;
字符串TestString=s;
字符串StringToReturn=s;
int width=(textrender.MeasureText(s,titleFont)).width;
如果(宽度>邮件列表宽度)
{
发现的宽度=假;
对于(int i=1;iif(width)这将简单地在最大宽度处截断字符串,如果你想以一个真实的单词结尾,你需要按照CAPN显示的行实现一些东西。那么当你有一个宽度为
width
=50的列表框和一个15个字符的字符串时会发生什么?(提示:
width
的值以像素为单位):p很好的更新。但是您必须将
+3
替换为
+textrender.MeasureText(“…”,titleFont.Width
)或其他内容,因为我向您保证省略号大于3像素。查看代码,它似乎可以工作,但1)它总是留下“…”(尽管这是一个简单的修复方法)它似乎仍然会被截断文本。这只会在最大宽度处截断字符串,如果你想以一个真实的单词结尾,你需要按照CAPN显示的行实现一些东西。那么当你有一个宽度为
50的列表框和一个15个字符的字符串时会发生什么呢?(提示:
Width
的值以像素为单位):PGood update。但是您必须将
+3
替换为
+textrender.MeasureText(“…”,titleFont.Width
)或其他内容,因为我保证省略号大于3像素。查看代码,它似乎可以工作,但1)它总是留下“…”(虽然这是一个简单的修复方法)而且它似乎仍然被截断。seesh,我编辑了很多次以更正不太明智的错误;如果您正在验证,请确保重新检查:)TestString=s.Substring(0,Length-i)中的Length值是多少?`?哇,对于所有连枷,我不得不更改为test(width-mailList.width)对self 100x重复:“永远不要发布你没有测试过的代码”:)仍在测试,但我很抱歉