0

我的消息太长,无法放入该列。我有四列的表格,我的第四列是“消息”,它有字符串长字符串,它不适合列宽。我想将列中的文本设置为 Warp,以便用户可以看到所有文本。

  ListViewItem lv = new ListViewItem();
                    lv.Text = det_view.filename;
                    lv.SubItems.Add(det_view.number.ToString());
                    lv.SubItems.Add(det_view.Date_Time.ToString());
                    lv.SubItems.Add(det_view.Message); // here the string too long and need wrap the message
                    listView1.Items.Add(lv);

问候

4

5 回答 5

2

如果您觉得某些许可证问题ObjectListView,您可以使用本机 .Net ListView

它还可以自动换行view=Details 设置smallImageList

(图片高度 = 32 或更高)。

原生 ListView 示例

于 2013-06-26T11:54:53.930 回答
2

如果没有您的 listviewitems ownerdrawn,您可能会看一下ObjectListView。它会自动换行,并且可以满足您的需求。

于 2012-09-06T19:32:37.357 回答
1

这是一个继承自 ListView 的类,它将增加行高以适合列中的文本。相信默认不会断字。所以如果你想要的话,你需要实现分词。

class WordWrapListView : ListView
{
    private const int LVM_FIRST = 0x1000;
    private const int LVM_INSERTITEMA = (WordWrapListView.LVM_FIRST + 7);
    private const int LVM_INSERTITEMW = (WordWrapListView.LVM_FIRST + 77);

    private Graphics graphics;

    public WordWrapListView()
    {
        this.graphics = this.CreateGraphics();
        base.View = View.Details;

        this.AutoSizeRowHeight = true;

    }

    //overriding WndProc because there are no item added events
    protected override void WndProc(ref Message m)
    {
        switch (m.Msg)
        {
            // Detect item insert and adjust the row size if necessary based on the text
            // add in LVM_DELETEITEM and LVM_DELETEALLITEMS and reset this.rowHeight if you want to reduce the row height on remove
            case WordWrapListView.LVM_INSERTITEMA:
            case WordWrapListView.LVM_INSERTITEMW:
                {
                    ListViewItem lvi = this.Items[this.Items.Count - 1];

                    for (int i = 0; i< lvi.SubItems.Count; ++i)
                    {
                        ListViewItem.ListViewSubItem lvsi = lvi.SubItems[i];

                        string text = lvsi.Text;

                        int tmpHeight = 0;
                        int maxWidth = this.Columns[i].Width;

                        SizeF stringSize = this.graphics.MeasureString(text, this.Font);

                        if (stringSize.Width > 0)
                        {
                            tmpHeight = (int)Math.Ceiling((stringSize.Width / maxWidth) * stringSize.Height);

                            if (tmpHeight > this.rowHeight)
                            {
                                this.RowHeight = tmpHeight;
                            }
                        }
                    }
                }
                break;

            default:
                break;
        }
        base.WndProc(ref m);
    }

    private void updateRowHeight()
    {
        //small image list hack
        ImageList imgList = new ImageList();
        imgList.ImageSize = new Size(this.rowHeight, this.rowHeight);
        this.SmallImageList = imgList;
    }

    [System.ComponentModel.DefaultValue(true)]
    public bool AutoSizeRowHeight { get; set; }

    private int rowHeight;
    public int RowHeight 
    {
        get
        {
            return this.rowHeight;
        }
        private set
        {
            //Remove value > this.rowHeight if you ever want to scale down the height on remove item
            if (value > this.rowHeight && this.AutoSizeRowHeight)
            {
                this.rowHeight = value;
                this.updateRowHeight();
            }
        }
    }

    // only allow details view
    [Browsable(false), Bindable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), EditorBrowsable(EditorBrowsableState.Never)]
    public new View View
    {
        get
        {
            return base.View;
        }
        set
        {

        }
    }

}
于 2015-02-02T14:07:45.467 回答
1

你可以试试Better ListView组件,它支持多行项目,具有多种文本换行和修剪方法:

在此处输入图像描述

于 2012-10-12T11:13:21.437 回答
1

可能不是您需要的,但我的解决方案是切换到内置 DataGridView 控件。

于 2015-05-19T20:56:30.013 回答