无论如何都要将文本包装在ListView中

时间:2012-09-06 18:32:43

标签: c# winforms

我的消息太长,不适合列。我有四列的表我的第四列是“消息”,它有字符串长字符串,它不适合列宽。我想将列中的文本设为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);

此致

5 个答案:

答案 0 :(得分:2)

如果没有列出您的listviewitems所有者,您可以查看ObjectListView。它会自动包装,可以满足您的需求。

答案 1 :(得分:2)

如果您对ObjectListView感到某些许可问题,可以使用原生.Net ListView

它也可以在view=Details 设置smallImageList

中进行自动换行

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

Example of native ListView

答案 2 :(得分:0)

您可以尝试Better ListView组件,该组件支持多行文章换行和修剪方法:

enter image description here

答案 3 :(得分:0)

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

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
        {

        }
    }

}

答案 4 :(得分:0)

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