listbox是顯示copy了哪個文件與日期等,有此文件名太長了,而我的listbox長度不夠,這樣就會有些信息顯示不出來
所以如何在Items超過長度時就將超過長度的信息換行顯示啊?

解决方案 »

  1.   

    用Tooltip行吗?增加个tooltip控件,然后再ListBox的鼠标Hover时间里写下下面的代码
                string strTip = "";            //Get the item
                int nIdx = listBox1.IndexFromPoint(e.Location);
                if ((nIdx >= 0) && (nIdx < listBox1.Items.Count))
                    strTip = listBox1.Items[nIdx].ToString();            toolTip1.SetToolTip(listBox1, strTip);这里有实例
    http://download.csdn.net/source/1614
      

  2.   

    你可以通过结合使用ListBox控件和Tooltip控件来实现需要的效果。如下提供一段示例代码,供你参考:   
      private   void   Form1_Load(object   sender,   System.EventArgs   e)   
      {   
      string   connString   =   "   server=SHA-RICKIE-01;database=pubs;uid=user;pwd=user";   
      string   sqlString   =   "   Select   title   from   titles";   
      DataSet   ds   =   new   DataSet();   
      SqlConnection   conn   =   new   SqlConnection(connString);   
      SqlDataAdapter   myDataAdapter   =   new   SqlDataAdapter(sqlString,conn);   
      myDataAdapter.Fill(ds,"titles");   
        
      listBox1.DataSource   =   ds.Tables["titles"].DefaultView   ;   
      //   Set   Field   Name   you   want   to   get   data   from   
      listBox1.DisplayMember   =   "title";   
      }   
        
      private   void   listBox1_SelectedIndexChanged(object   sender,   System.EventArgs   e)   
      {   
      toolTip1.SetToolTip(listBox1,listBox1.Text);   
      toolTip1.Active   =   true;   
      }   
      

  3.   

    重绘控件吧。
    我有vb的代码如下#Region "重绘ListBox控件"
        Private Sub lstDockItem_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles lstDockItem.DrawItem
            If e.Index = -1 Then Exit Sub        Dim txtBrush As SolidBrush = New SolidBrush(Color.Blue)
            Dim bgBrush As SolidBrush = New SolidBrush(Color.LightCyan)
            Dim txtFnt As Font = New Font(Me.Font.Name, Me.Font.Size)        Try
                If e.Index Mod 2 = 0 Then txtBrush = New SolidBrush(Color.Red)
                If e.State And System.Windows.Forms.DrawItemState.Selected Then
                    txtFnt = New Font(Me.Font.Name, Me.Font.Size, FontStyle.Italic Or FontStyle.Bold)
                    bgBrush = New SolidBrush(Color.LightSteelBlue)
                End If            e.Graphics.FillRectangle(bgBrush, e.Bounds)            Dim R As New RectangleF(e.Bounds.X + e.Bounds.Height, e.Bounds.Y, e.Bounds.Width - e.Bounds.Height, e.Bounds.Height)
                Dim SF As New StringFormat : SF.LineAlignment = StringAlignment.Center
                e.Graphics.DrawImage(Me.DockManager.Item(e.Index).Icon, e.Bounds.Left + 2, e.Bounds.Top + 2, e.Bounds.Height - 4, e.Bounds.Height - 4)
                e.Graphics.DrawString(lstDockItem.Items(e.Index).ToString, txtFnt, txtBrush, R, SF)
                e.DrawFocusRectangle()
            Catch ex As Exception
            End Try
            txtBrush.Dispose() : bgBrush.Dispose() : txtFnt.Dispose() '释放资源
        End Sub    Private Sub lstDockItem_MeasureItem(ByVal sender As Object, ByVal e As System.Windows.Forms.MeasureItemEventArgs) Handles lstDockItem.MeasureItem
            Try
                Dim S As New Size(Me.Width, 400)
                Dim itmSize As SizeF = e.Graphics.MeasureString(lstDockItem.Items(e.Index).ToString, Me.Font, S)
                e.ItemHeight = itmSize.Height * 2 : e.ItemWidth = itmSize.Width
            Catch ex As Exception
            End Try
        End Sub
    #End Region
      

  4.   

    ListBox有一个属性可以设置的 就可以有横向滚动条
      

  5.   

    listBox本身与textbox文本是有差异的
    它存在的格式只能是一行一行显示,可以设置下拉和右拉
    但不能设置自动换行,因为没有这个属性如果想换行,可以判断字符串长度后重新add一下