User123
User123

Reputation: 373

Is there anyway to wrap the text in the ListView

I have message is too long to fit in the column. I have table with four columns my fourth column is "Message" which has string long string and it doesnot fit in columnwidth. I want to Make the text in column as Warp so that all the text is visible to the user.

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

Regards

Upvotes: 0

Views: 25560

Answers (5)

Tom Winter
Tom Winter

Reputation: 1923

May not be what you need but my solution was to switch to the build-in DataGridView control.

Upvotes: 1

curlyhairedgenius
curlyhairedgenius

Reputation: 922

Here is a class inheriting from ListView that will grow your row height to fit the text in a column. I believe the default will not word break. So you would need to implement wordbreak if that's something you want.

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
        {

        }
    }

}

Upvotes: 1

DimDim
DimDim

Reputation: 148

If You feel some license problems with ObjectListView, you can use native .Net ListView.

It can also wordwrap in view=Details and setting smallImageList

(with picture height = 32 or more).

Example of native ListView

Upvotes: 2

Libor
Libor

Reputation: 3303

You may try Better ListView component, which supports multi-line items with varous text wrapping and trimming methods:

enter image description here

Upvotes: 1

itsmatt
itsmatt

Reputation: 31416

Short of having your listviewitems ownerdrawn, you might have a look at ObjectListView. It will wordwrap just fine and may suite your needs.

Upvotes: 2

Related Questions