Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27415

VS 2010 Custom ListView control max characters.

$
0
0
Hey.

I'm using a custom ListView control Chris001 made for someone here, add it to your project and build.

Code:

Public Class ListViewEx
    Inherits ListView

    Private imgList As New ImageList

    Public Sub New()
        Me.OwnerDraw = True

        ' The control flickers if it's not double-buffered
        Me.SetStyle(ControlStyles.OptimizedDoubleBuffer, True)
        Me.SetStyle(ControlStyles.AllPaintingInWmPaint, True)

        ' Configure the columns and such
        Me.View = View.Details
        Me.BackColor = Color.White
        Me.FullRowSelect = True
        Me.Columns.Add("Functions", 200)
        Me.ShowItemToolTips = True

        ' Set the imagelist to change the height of the Listview items.
        imgList.ColorDepth = ColorDepth.Depth32Bit
        imgList.ImageSize = New Size(32, 32)
        Me.SmallImageList = imgList
    End Sub

    Private Sub Me_DrawColumnHeader(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DrawListViewColumnHeaderEventArgs) Handles Me.DrawColumnHeader
        e.DrawDefault = True
    End Sub

    Private Sub Me_DrawItem(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DrawListViewItemEventArgs) Handles Me.DrawItem
        e.DrawDefault = False
    End Sub

    Private Sub Me_DrawSubItem(ByVal sender As Object, ByVal e As DrawListViewSubItemEventArgs) Handles Me.DrawSubItem
        Dim lvw As ExtraData = DirectCast(e.Item.Tag, ExtraData)
        Dim f1 As New Font("Segoe UI", 10, FontStyle.Bold)
        Dim f2 As New Font("Segoe UI", 8, FontStyle.Regular)

        Dim Rectf1 As RectangleF = New Rectangle(e.Bounds.X + 35, e.Bounds.Y + 3, e.Bounds.Width - 35, f1.Height)
        Dim Rectf2 As RectangleF = New Rectangle(e.Bounds.X + 35, e.Bounds.Y + 18, e.Bounds.Width - 35, f2.Height)
        Dim sf As New StringFormat
        sf.Alignment = StringAlignment.Near
        sf.Trimming = StringTrimming.EllipsisCharacter
        Dim TextColor As Brush

        ' Draw the highlighting of the Image and Text
        'e.Graphics.MeasureString(lvw.HeaderText, f1).Width + 5

        If (e.ItemState And ListViewItemStates.Selected) <> 0 Then
            e.Graphics.FillRectangle(SystemBrushes.Highlight, e.Bounds.X + 1, e.Bounds.Y + 1, 31, 31) 'item is highlighted

            If e.Graphics.MeasureString(lvw.HeaderText, f1).Width + 5 > (e.Bounds.Width - 35) Then
                e.Graphics.FillRectangle(SystemBrushes.Highlight, e.Bounds.X + 35, e.Bounds.Y + 2, e.Bounds.Width - 35, f1.Height)
            Else
                e.Graphics.FillRectangle(SystemBrushes.Highlight, e.Bounds.X + 35, e.Bounds.Y + 2, e.Graphics.MeasureString(lvw.HeaderText, f1).Width, f1.Height)
            End If

            If e.Graphics.MeasureString(lvw.MinorText, f2).Width > (e.Bounds.Width - 35) Then
                e.Graphics.FillRectangle(SystemBrushes.Highlight, e.Bounds.X + 35, e.Bounds.Y + 17, e.Bounds.Width - 35, f2.Height)
            Else
                e.Graphics.FillRectangle(SystemBrushes.Highlight, e.Bounds.X + 35, e.Bounds.Y + 17, e.Graphics.MeasureString(lvw.MinorText, f2).Width, f2.Height)
            End If

            TextColor = Brushes.White
        Else
            TextColor = Brushes.Gray
        End If

        ' Draw the image and text
        If e.ColumnIndex = 0 Then
            e.Graphics.DrawImage(lvw.MainImage, e.Bounds.X + 1, e.Bounds.Y + 1, 32, 32)
            e.Graphics.DrawString(lvw.HeaderText, f1, TextColor, Rectf1, sf)
            e.Graphics.DrawString(lvw.MinorText, f2, TextColor, Rectf2, sf)
        Else
            e.DrawDefault = True
        End If

    End Sub


End Class

This ListView control makes each item to have an image with a 'Header text' and a 'Minor text'. To add an item just to test my app, add the following:

Code:

Public Sub insertLvGroup(ByVal groupKey As String, ByVal groupName As String)
        Dim lvwgroup As New ListViewGroup(groupKey, groupName)
        frmMain.ListViewEx1.Groups.Add(lvwgroup)
    End Sub

Code:

Public Sub insertLvItem(ByVal imgLocation As String, ByVal headerText As String, ByVal minorText As String, ByVal toolTip As String, ByVal groupLoc As String)
        Dim bm As New Bitmap(imgLocation)

        Dim lvwExtra As New ExtraData
        lvwExtra.HeaderText = headerText
        lvwExtra.MinorText = minorText
        lvwExtra.MainImage = bm

        Dim lvw As ListViewItem = frmMain.ListViewEx1.Items.Add("")
        lvw.Tag = lvwExtra
        lvw.ToolTipText = minorText

        frmMain.ListViewEx1.Groups(groupLoc).Items.Add(lvw)
    End Sub

And then add the following in your Form Load event:

Code:

insertLvGroup("Test1", "Test")
Code:

insertLvItem("C:\image.png", "Test Header", "Test description. This example shows that the max characters I can enter is about 25 and it adds "...".", "", "Test1")
Now of course add the ListViewEx control and see what happens. The "Minor text" gets cut at about 25 characters and then adds "..." instead of writing the full text (even if I maximize my app it says the same.

Viewing all articles
Browse latest Browse all 27415

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>