@@ -27,8 +27,8 @@ Module ListViewExtensions
2727 End Sub
2828
2929 <Extension()>
30- Public Function AddItem(listViewControl As ListView, Text As String , Value As String , WrapText As Boolean ) As ListViewItem
31- Return AddItem(listViewControl, Nothing , "" , Text, Value, Color.Empty, WrapText , Nothing )
30+ Public Function AddItem(listViewControl As ListView, Text As String , Value As String , WrapValue As Boolean ) As ListViewItem
31+ Return AddItem(listViewControl, Nothing , "" , Text, Value, Color.Empty, WrapValue , Nothing )
3232 End Function
3333
3434 <Extension()>
@@ -42,8 +42,8 @@ Module ListViewExtensions
4242 End Function
4343
4444 <Extension()>
45- Public Function AddItem(listViewControl As ListView, Group As ListViewGroup, Text As String , Value As String , WrapText As Boolean ) As ListViewItem
46- Return AddItem(listViewControl, Group , "" , Text, Value, Color.Empty, WrapText , Nothing )
45+ Public Function AddItem(listViewControl As ListView, Group As ListViewGroup, Text As String , Value As String , WrapValue As Boolean ) As ListViewItem
46+ Return AddItem(listViewControl, Group , "" , Text, Value, Color.Empty, WrapValue , Nothing )
4747 End Function
4848
4949 <Extension()>
@@ -52,31 +52,31 @@ Module ListViewExtensions
5252 End Function
5353
5454 <Extension()>
55- Public Function AddItem(listViewControl As ListView, Group As ListViewGroup, Text As String , Value As String , ForeColor As Color, WrapText As Boolean ) As ListViewItem
56- Return AddItem(listViewControl, Group , "" , Text, Value, ForeColor, WrapText , Nothing )
55+ Public Function AddItem(listViewControl As ListView, Group As ListViewGroup, Text As String , Value As String , ForeColor As Color, WrapValue As Boolean ) As ListViewItem
56+ Return AddItem(listViewControl, Group , "" , Text, Value, ForeColor, WrapValue , Nothing )
5757 End Function
5858
5959 <Extension()>
60- Public Function AddItem(listViewControl As ListView, Group As ListViewGroup, Text As String , Value As String , ForeColor As Color, WrapText As Boolean , Width? As Integer ) As ListViewItem
61- Return AddItem(listViewControl, Group , "" , Text, Value, ForeColor, WrapText , Width)
60+ Public Function AddItem(listViewControl As ListView, Group As ListViewGroup, Text As String , Value As String , ForeColor As Color, WrapValue As Boolean , Width? As Integer ) As ListViewItem
61+ Return AddItem(listViewControl, Group , "" , Text, Value, ForeColor, WrapValue , Width)
6262 End Function
6363
6464 <Extension()>
65- Public Function AddItem(listViewControl As ListView, Group As ListViewGroup, Name As String , Text As String , Value As String , ForeColor As Color, WrapText As Boolean , Width? As Integer ) As ListViewItem
65+ Public Function AddItem(listViewControl As ListView, Group As ListViewGroup, Name As String , Text As String , Value As String , ForeColor As Color, WrapValue As Boolean , Width? As Integer ) As ListViewItem
6666 Dim Item As ListViewItem = Nothing
6767 Dim SubItem As ListViewItem.ListViewSubItem
6868 Dim StringList As List( Of String ) = Nothing
6969 Dim AddTags As Boolean = False
7070
71- If WrapText Then
72- Dim CalcWidth As Integer
73- If Width.HasValue Then
74- CalcWidth = Width.Value
75- Else
76- CalcWidth = listViewControl.Columns.Item( 1 ).Width - 6
71+ If WrapValue Then
72+ Dim HasNewLine = Value.Contains(vbCrLf) OrElse Value.Contains(vbCr) OrElse Value.Contains(vbLf)
73+ Dim TargetWidth As Integer = listViewControl.Columns.Item( 1 ).Width - 6
74+ Dim MaxWidth As Integer = If ( Width.HasValue, Width. Value, TargetWidth)
75+ If HasNewLine Then
76+ TargetWidth = MaxWidth
7777 End If
78- If TextRenderer.MeasureText(Value, listViewControl.Font).Width > CalcWidth OrElse Value.Contains(vbNewLine) Then
79- StringList = Value.WordWrap(CalcWidth , listViewControl.Font)
78+ If TextRenderer.MeasureText(Value, listViewControl.Font).Width > TargetWidth OrElse HasNewLine Then
79+ StringList = Value.WordWrap(MaxWidth, TargetWidth , listViewControl.Font)
8080 AddTags = True
8181 End If
8282 End If
@@ -118,55 +118,6 @@ Module ListViewExtensions
118118 Return Item
119119 End Function
120120
121- <Extension()>
122- Public Function InsertItem(listViewControl As ListView, Index As Integer , Group As ListViewGroup, Text As String , Value As String , ForeColor As Color, WrapText As Boolean ) As ListViewItem
123- Dim Item As ListViewItem = Nothing
124- Dim SubItem As ListViewItem.ListViewSubItem
125- Dim StringList As List( Of String ) = Nothing
126- Dim AddTags As Boolean = False
127-
128- If WrapText Then
129- Dim Width = listViewControl.Columns.Item( 1 ).Width - 5
130- If TextRenderer.MeasureText(Value, listViewControl.Font).Width > Width Then
131- StringList = Value.WordWrap(Width, listViewControl.Font)
132- AddTags = True
133- End If
134- End If
135-
136- If StringList Is Nothing Then
137- StringList = New List( Of String ) From {
138- Value
139- }
140- End If
141-
142- For Counter = 0 To StringList.Count - 1
143- Dim ItemText As String
144- If Counter = 0 Then
145- ItemText = Text
146- Else
147- ItemText = ""
148- End If
149- Dim NewItem As New ListViewItem(ItemText, Group ) With {
150- .UseItemStyleForSubItems = False
151- }
152- If AddTags Then
153- NewItem.Tag = Text
154- End If
155- SubItem = NewItem.SubItems.Add(StringList(Counter))
156- SubItem.ForeColor = ForeColor
157- If AddTags Then
158- SubItem.Tag = Value
159- End If
160- listViewControl.Items.Insert(Index, NewItem)
161-
162- If Item Is Nothing Then
163- Item = NewItem
164- End If
165- Next
166-
167- Return Item
168- End Function
169-
170121
171122 <Extension()>
172123 Public Sub AutoResizeColumnsContstrained(listViewControl As ListView, headerAutoResize As ColumnHeaderAutoResizeStyle)
0 commit comments