如何将多行文本添加到 ListBox 项? [英] How to add multiline Text to a ListBox item?

查看:22
本文介绍了如何将多行文本添加到 ListBox 项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小问题,找不到解决方案.
我想在 ListBox 项目的末尾放置一个文本,但我不知道如何...

I have a little problem and can't find a solution.
I would like to put a text at the end of a ListBox item and I have no idea how...

TagLib.File f = TagLib.File.Create(paths[i]);
listBox1.Items.Add("0" + i + ". " + f.Tag.Title +"
" + string.Join(", ", f.Tag.Performers) + " - " + "
" + f.Tag.Album + "                                               "  + f.Properties.Duration.TotalMinutes.ToString());

我已经尝试过了,但不幸的是它不能那样工作.也许有人知道如何始终将文本放在最后,并带有代码?

I've already tried it but unfortunately it doesn't work that way. Maybe someone knows how you can always put the text at the end, with a code?

我希望所有项目的文本相互匹配而且我找不到解决方案如何将文本放在最后以及文本如何相互匹配

I want the text of all items to match each other And I can't find a solution how to put the text at the end and how the text can match each other

推荐答案

要将多行文本添加到 ListBox 控件,您需要自己测量和绘制文本.

To add multiline text to a ListBox Control, you need to measure and draw the text yourself.

设置 ListBox.DrawModeDrawMode.OwnerDrawVariable,然后覆盖 OnMeasureItemOnDrawItem.

Set the ListBox.DrawMode to DrawMode.OwnerDrawVariable, then override OnMeasureItem and OnDrawItem.

OnMeasureItem 在绘制项目之前调用,以允许定义项目大小,设置 MeasureItemEventArgs e.ItemWidthe.ItemHeight 属性(在尝试测量一个之前,您必须验证 ListBox 包含项目).

OnMeasureItem is called before an Item is drawn, to allow to define the Item Size, setting the MeasureItemEventArgs e.ItemWidth and e.ItemHeight properties (you have to verify that the ListBox contains Items before trying to measure one).

→ 当 OnDrawItem 被调用时,e.Bounds 属性的 DrawItemEventArgs 将设置为 OnMeasureItem 中指定的度量.

→ When OnDrawItem is called, the e.Bounds property of its DrawItemEventArgs will be set to the measure specified in OnMeasureItem.

要测量文本,您可以使用 TextRendererMeasureText() 方法或 Graphics.MeasureString.前者是首选,因为我们将使用 TextRenderer 类来绘制 Items 的文本:TextRenderer.DrawText更可预测Graphics.DrawString() 在这种情况下并且它自然地呈现文本(就像 ListBox - 或 ListView - 那样).

To measure the Text, you can use both the TextRenderer class MeasureText() method or Graphics.MeasureString. The former is preferred, since we're going to use the TextRenderer class to draw the Items' text: TextRenderer.DrawText is more predictable than Graphics.DrawString() in this context and it renders the text naturally (as the ListBox - or ListView - does).

TextRendererTextFormatFlags 用于微调渲染行为.我已将 TextFormatFlags.ExpandTabs 添加到标志中,因此您还可以在需要时将 Tabs (" ") 添加到文本.请参阅视觉示例.
" 可用于生成换行符.

The TextRenderer's TextFormatFlags are used to fine-tune the rendering behavior. I've added TextFormatFlags.ExpandTabs to the flags, so you can also add Tabs (" ") to the Text when needed. See the visual example.
" " can be used to generate line feeds.

在示例代码中,我将 8 像素添加到 Items 的测量高度,因为还绘制了行分隔符以更好地定义 Item 的限制(否则,当 Item跨越多行,可能难以理解其文本的开始和结束位置).

In the sample code, I'm adding 8 pixels to the measured height of the Items, since a line separator is also drawn to better define the limits of an Item (otherwise, when a Item spans more than one line, it may be difficult to understand where its text starts and ends).

► 重要提示:最大 Item.Height255 像素.除此以外,项目的文本可能不会被渲染或被部分渲染(但它通常只是消失).示例代码中对项目高度进行了最小/最大检查.

► Important: the maximum Item.Height is 255 pixels. Beyond this measure, the Item's text may not be rendered or be rendered partially (but it usually just disappears). There's a Min/Max check on the item height in the sample code.

这是它的工作原理:

我建议使用类对象(如果还没有)来存储您的项目并描述它们.然后使用 List 作为ListBox.DataSource.这样,您可以更好地定义每个部分如何应该被渲染.某些部分可能使用粗体或不同的字体颜色.

I suggest to use a class object, if you haven't, to store your Items and describe them. Then use a List<class> as the ListBox.DataSource. This way, you can better define how each part should be rendered. Some parts may use a Bold Font or a different Color.

using System;
using System.Drawing;
using System.Windows.Forms;

public class ListBoxMultiline : ListBox
{
    TextFormatFlags flags = TextFormatFlags.WordBreak |
                            TextFormatFlags.PreserveGraphicsClipping |
                            TextFormatFlags.LeftAndRightPadding |
                            TextFormatFlags.ExpandTabs |
                            TextFormatFlags.VerticalCenter;

    public ListBoxMultiline() { this.DrawMode = DrawMode.OwnerDrawVariable; }

    protected override void OnDrawItem(DrawItemEventArgs e)
    {
        if (Items.Count == 0) return;
        if (e.State.HasFlag(DrawItemState.Focus) || e.State.HasFlag(DrawItemState.Selected)) {
            using (var selectionBrush = new SolidBrush(Color.Orange)) {
                e.Graphics.FillRectangle(selectionBrush, e.Bounds);
            }
        }
        else {
            e.DrawBackground();
        }
        
        TextRenderer.DrawText(e.Graphics, GetItemText(Items[e.Index]), Font, e.Bounds, ForeColor, flags);

        if (e.Index != Items.Count - 1) {
            Point lineOffsetStart = new Point(e.Bounds.X, e.Bounds.Bottom - 1);
            Point lineOffsetEnd = new Point(e.Bounds.Right, e.Bounds.Bottom - 1);
            e.Graphics.DrawLine(Pens.LightGray, lineOffsetStart, lineOffsetEnd);
        }
        base.OnDrawItem(e);
    }

    protected override void OnMeasureItem(MeasureItemEventArgs e)
    {
        if (Items.Count == 0) return;
        var size = GetItemSize(e.Graphics, GetItemText(Items[e.Index]));
        e.ItemWidth = size.Width;
        e.ItemHeight = size.Height;
        base.OnMeasureItem(e);
    }

    private Size GetItemSize(Graphics g, string itemText)
    {
        var size = TextRenderer.MeasureText(g, itemText, Font, ClientSize, flags);
        size.Height = Math.Max(Math.Min(size.Height, 247), Font.Height + 8) + 8;
        return size;
    }
}

这篇关于如何将多行文本添加到 ListBox 项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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