2012-09-06 70 views
0

我有消息太長,以適應列。我有四列的表,我的第四列是「消息」,它具有字符串長字符串,它不適合列寬。我想將列中的文本製作爲Warp,以便用戶可以看到所有文本。是否有包裹在ListView中的文本

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

問候

+8

爲什麼你沒有接受70%的問題的答案? –

回答

1

短期有你listviewitems ownerdrawn的,你可能有一個看ObjectListView。它將Wordwrap很好,可能會滿足您的需求。

0

您可以嘗試Better ListView組件,它支持與varous文字環繞和修整方法多線項目:

enter image description here

2

如果你覺得ObjectListView某些許可證的問題,你可以使用原生的.Net ListView

它也可以換行中view=Details設定smallImageList

(與圖像高度= 32個或更多)。

Example of native ListView

+0

但是如何?我正在使用VS 2008和Windows CE應用程序。是否有可能實現這一目標? – kbvishnu

+0

這似乎工作,認爲每一行都是相同的高度。還有一些單元格似乎不能包裝,但用省略號切斷一行。 –

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 
     { 

     } 
    } 

} 
+0

你的代碼是有用的,只是我想有一個關於LVM_FIRST的精度,爲什麼0x1000的值?而且在updateRowHeight中,我不太瞭解黑客,請你解釋一下嗎? –

+1

通過覆蓋WndProc,我們基本上攔截了Windows消息循環,以便我們可以響應未由listview明確定義的事件。 LVM_ *的定義來自Commctrl.h;他們是Windows常量。 更新行高的實際能力來自: http://stackoverflow.com/questions/6563863/c-sharp-change-listview-items-rows-height 這是不幸的我所知道的關於「黑客」。 – curlyhairedgenius

0

可能不是你所需要的,但我的解決方案是切換到內置的DataGridView控件。