C# 是否仍要在ListView中包装文本

C# 是否仍要在ListView中包装文本,c#,winforms,C#,Winforms,我有消息太长,无法放入该列。我有一个有四列的表,我的第四列是“Message”,它有一个长字符串,它不适合列宽。我想将列中的文本设置为扭曲,以便用户可以看到所有文本 ListViewItem lv = new ListViewItem(); lv.Text = det_view.filename; lv.SubItems.Add(det_view.number.ToString());

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

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

关于

除了拥有自己的listviewitems之外,您还可以看看。它可以很好地进行文字包装,并且可以满足您的需要。

除了拥有自己的listviewitems之外,您还可以查看一下。它可以很好地进行文字包装,可以满足您的需要。

您可以尝试组件,该组件支持多行项目,具有多种文字包装和修剪方法:

您可以尝试组件,该组件支持多行项目,具有多种文本包装和修剪方法:


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

它还可以在
view=Details
设置
smallImageList

(图片高度等于或大于32)


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

它还可以在
view=Details
设置
smallImageList

(图片高度等于或大于32)


这里有一个从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
        {

        }
    }

}
类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);
私有图形;
公共WordWrapListView()
{
this.graphics=this.CreateGraphics();
base.View=View.Details;
this.autosizeroweight=true;
}
//正在重写WndProc,因为没有添加项目的事件
受保护的覆盖无效WndProc(参考消息m)
{
开关(m.Msg)
{
//检测项目插入,必要时根据文本调整行大小
//添加LVM_DELETEITEM和LVM_DELETEALLITEMS并重置this.rowHeight(如果要在删除时降低行高)
案例WordWrapListView.LVM_INSERTITEMA:
案例WordWrapListView.LVM_INSERTITEMW:
{
ListViewItem lvi=this.Items[this.Items.Count-1];
对于(int i=0;i0)
{
tmpHeight=(int)数学天花板((stringSize.Width/maxWidth)*stringSize.Height);
如果(tmpHeight>此.rowHeight)
{
this.RowHeight=tmpHeight;
}
}
}
}
打破
违约:
打破
}
基准WndProc(参考m);
}
私有void updateRowHeight()
{
//小图像列表黑客
ImageList imgList=新的ImageList();
imgList.ImageSize=新大小(this.rowHeight,this.rowHeight);
this.SmallImageList=imgList;
}
[System.ComponentModel.DefaultValue(true)]
公共bool autosizeroweight{get;set;}
私人室内高度;
公共内线高度
{
得到
{
返回此值。行高;
}
专用设备
{
//如果要缩小删除项目的高度,请删除值>this.rowHeight
if(值>this.rowHeight&&this.autosizeroweight)
{
此值为.rowHeight=值;
this.updateRowHeight();
}
}
}
//仅允许查看详细信息
[可浏览(false)、可绑定(false)、DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)、EditorBrowsable(EditorBrowsableState.Never)]
公共新视野
{
得到
{
返回基视图;
}
设置
{
}
}
}

这里有一个从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
        {

        }
    }

}
类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);
私有图形;
公共WordWrapListView()
{
this.graphics=this.CreateGraphics();
base.View=View.Details;
this.autosizeroweight=true;
}
//正在重写WndProc,因为没有添加项目的事件
受保护的覆盖无效WndProc(参考消息m)
{
开关(m.Msg)
{
//检测项目插入,必要时根据文本调整行大小
//添加LVM_DELETEITEM和LVM_DELETEALLITEMS并重置this.rowHeight(如果要在删除时降低行高)
案例WordWrapListView.LVM_INSERTITEMA:
案例WordWrapListView.LVM_INSERTITEMW:
{
ListViewItem lvi=this.Items[this.Items.Count-1];
对于(int i=0;i