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

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

问题描述

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

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

我已经尝试过了,但是不幸的是,这种方式不起作用.也许有人知道如何总是可以使用代码将文本放在最后?

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

解决方案

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

设置

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

 使用系统;使用System.Drawing;使用System.Windows.Forms;公共类ListBoxMultiline:ListBox{TextFormatFlags标志= TextFormatFlags.WordBreak |TextFormatFlags.PreserveGraphicsClipping |TextFormatFlags.LeftAndRightPadding |TextFormatFlags.ExpandTabs |TextFormatFlags.VerticalCenter;公共ListBoxMultiline(){this.DrawMode = DrawMode.OwnerDrawVariable;}受保护的重写void OnDrawItem(DrawItemEventArgs e){如果(Items.Count == 0)返回;if(e.State.HasFlag(DrawItemState.Focus)|| e.State.HasFlag(DrawItemState.Selected)){使用(var selectionBrush = new SolidBrush(Color.Orange)){e.Graphics.FillRectangle(selectionBrush,e.Bounds);}}别的 {e.DrawBackground();}TextRenderer.DrawText(e.Graphics,GetItemText(Items [e.Index]),Font,e.Bounds,ForeColor,flags);if(e.Index!= Items.Count-1){点lineOffsetStart =新的Point(e.Bounds.X,e.Bounds.Bottom-1);点lineOffsetEnd =新Point(e.Bounds.Right,e.Bounds.Bottom-1);e.Graphics.DrawLine(Pens.LightGray,lineOffsetStart,lineOffsetEnd);}base.OnDrawItem(e);}受保护的重写void OnMeasureItem(MeasureItemEventArgs e){如果(Items.Count == 0)返回;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,字符串itemText){var size = TextRenderer.MeasureText(g,itemText,Font,ClientSize,flags);size.Height = Math.Max(Math.Min(size.Height,247),Font.Height + 8)+ 8;返回大小;}} 

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 +"\n" + string.Join(", ", f.Tag.Performers) + " - " + "\r" + 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

解决方案

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

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

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).

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

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).

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 ("\t") to the Text when needed. See the visual example.
"\n" can be used to generate line feeds.

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).

► 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.

This is how it works:

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天全站免登陆