【问题标题】:How to create a line break with Listbox using C# with Asp.net?如何使用带有 Asp.net 的 C# 使用 Listbox 创建换行符?
【发布时间】:2017-12-01 09:27:22
【问题描述】:

我想在列表框中创建一个换行符,这样文本就不会一直拖到用户无法阅读文本的全部内容的地方。

这是我当前输出的样子:

我希望当文本到达列表框的末尾时,它会下降到下一行,简单来说,就是自动换行。上网一搜,发现无法使用listbox实现自动换行功能。我设法在网上找到了一个自动换行算法并决定使用它,但是我不确定如何将它实现到我希望它所在的列表框中。

这是我找到的代码:

//https://www.codeproject.com/Articles/51488/Implementing-Word-Wrap-in-C

public static string WordWrap(string text, int width)
{
    int pos, next;
    StringBuilder sb = new StringBuilder();

    // Lucidity check
    if (width < 1)
        return text;

    // Parse each line of text
    for (pos = 0; pos < text.Length; pos = next)
    {
        // Find end of line
        int eol = text.IndexOf(Environment.NewLine, pos);
        if (eol == -1)
            next = eol = text.Length;
        else
            next = eol + Environment.NewLine.Length;

        // Copy this line of text, breaking into smaller lines as needed
        if (eol > pos)
        {
            do
            {
                int len = eol - pos;
                if (len > width)
                    len = BreakLine(text, pos, width);
                sb.Append(text, pos, len);
                sb.Append(Environment.NewLine);

                // Trim whitespace following break
                pos += len;
                while (pos < eol && Char.IsWhiteSpace(text[pos]))
                    pos++;
            } while (eol > pos);
        }
        else sb.Append(Environment.NewLine); // Empty line
    }
    return sb.ToString();
}

/// <summary>
/// Locates position to break the given line so as to avoid
/// breaking words.
/// </summary>
/// <param name="text">String that contains line of text</param>
/// <param name="pos">Index where line of text starts</param>
/// <param name="max">Maximum line length</param>
/// <returns>The modified line length</returns>
private static int BreakLine(string text, int pos, int max)
{
    // Find last whitespace in line
    int i = max;
    while (i >= 0 && !Char.IsWhiteSpace(text[pos + i]))
        i--;

    // If no whitespace found, break at maximum length
    if (i < 0)
        return max;

    // Find start of whitespace
    while (i >= 0 && Char.IsWhiteSpace(text[pos + i]))
        i--;

    // Return length of text before whitespace
    return i + 1;
}

目前我把它作为一个单独的方法,我应该把这个方法直接放在列表框方法本身吗?

如果是,我该如何修改上述代码以使其工作? 顺便说一句 descLb 是我的列表框的名称

请不要建议我将我的列表框更改为另一种形式(例如文本框),我只知道如何从数据库中提取文本以输入到列表框并保持简单,我想使用列表框。

【问题讨论】:

  • 我刚才已经提供了答案。请检查并告诉我。

标签: c# asp.net listbox


【解决方案1】:

您不需要所有复杂的服务器端 C# 代码来将文本换行到 asp.net 的列表框中。你所需要的只是一些特殊的 CSS。例如,下面的 CSS 将自动将文本换行到 asp.net 列表框中。

如果您在下面 CSS 中提到的宽度不足以容纳列表框选项的文本,则会自动进行换行。

在列表框中换行的 CSS

select option {
 white-space: normal;
 width: 100px

}

这里是 aspx 标记,您可以尝试使用它来查看文本是否自动换行。

请注意,

  • 我正在使用带有 id 的 CSS 选择器来关注特定的列表框以进行包装
  • 而且我还没有指定宽度,因为任何不能 在100px 的标记宽度内容纳列表框获取 自动变形

.

<%@ Page Language="C#" AutoEventWireup="true" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Wrtap text in ListBox</title>
    <style>
        select[id*='ListBox1'] option {
            white-space:normal;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:ListBox ID="ListBox1"
                Rows="6"
                Width="150px"
                SelectionMode="Single"
                runat="server">
                <asp:ListItem>Item 1 dsdds  sdsdsdsd sdsd sdsd sds xyz</asp:ListItem>
                <asp:ListItem>Item 2</asp:ListItem>
                <asp:ListItem>Item 3 gfgf kgkgkg kgkkg abc</asp:ListItem>
                <asp:ListItem>Item 4</asp:ListItem>
                <asp:ListItem>Item 5</asp:ListItem>
                <asp:ListItem>Item 6</asp:ListItem>
            </asp:ListBox>
        </div>
    </form>
</body>
</html>

下面是通过提供上述特殊 CSS 样式,在 asp.net 列表框中的包装外观。

asp.net ListBox 中文字换行的屏幕截图

【讨论】:

  • 嘿,非常感谢!我尝试了你的方法,它对我有用,为我省去了很多麻烦,而且也更容易理解。再次感谢您:)
  • 您是否知道如何在悬停时选择多个列表框?我在我发布的列表旁边有另一个列表框,它列出了描述旁边的国家/地区。我想这样做,如果我将鼠标悬停在国家或描述上,两者都会突出显示为一个
  • 你能发布一个新问题,说明你的确切要求吗?在您发布详细问题后,我会尝试提出解决方案。
  • 这是我的确切问题的链接:stackoverflow.com/questions/47615899/…,如果你能提供帮助将不胜感激!
  • 您的列表框将回发,因此就选择而言,这可能在 C# 中发生,但是当将鼠标悬停在列表框项目上时,不会发生服务器端事件,因此您需要使用 jQuery 或 JavaScript使两个列表框以相同的方式悬停。
猜你喜欢
  • 2021-03-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多