-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathListViewHelper.vb
More file actions
751 lines (601 loc) · 25.6 KB
/
ListViewHelper.vb
File metadata and controls
751 lines (601 loc) · 25.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
'2025-5-29
'Private Sub lvMain_ColumnClick(sender As Object, e As ColumnClickEventArgs) Handles lvMain.ColumnClick
' lvInstance.ColumnSortHandler(sender, e)
'End Sub
'Private Sub lvMain_MouseDown(sender As Object, e As MouseEventArgs) Handles lvMain.MouseDown
' lvInstance.RightClickHandler(sender, e)
'End Sub
Imports System.Windows.Forms
Public Class ListViewHelper
Private LV As ListView
Private _RowCount As Integer = 0
Private ColumnDict As New Dictionary(Of String, Integer)
Private ColumnCountVal As Integer
Private BlankStrArr() As String
Private ContextMenu1 As New ContextMenuStrip()
Private clickedItem As ListViewItem.ListViewSubItem = Nothing
Private clickedParent As ListViewItem
Private ToolTip_LV As New Windows.Forms.ToolTip
Private DefaultConfig As String = ""
Private SavedCheckedItems() As ListViewItem
Private SavedUncheckedItems() As ListViewItem
Private ListPrefiltered() As ListViewItem
Private CurrentShowState As HideMode = HideMode.ShowAll
Private AmFiltered As Boolean = False
Public _LastDPI As Integer
Private lvToolTip As ToolTip
Public Enum HideMode
ShowAll
HideChecked
HideUnchecked
'Filtered
End Enum
Public Sub ClearEntries()
If _RowCount > 0 Then
_RowCount = 0
LV.Items.Clear()
End If
End Sub
'Public Sub SaveList()
' ReDim FullListSaved(LV.Items.Count - 1)
' LV.Items.CopyTo(FullListSaved, 0)
'End Sub
Private Sub GenerateBaseContextMenu()
ContextMenu1.Items.Add("Copy Text", Nothing, AddressOf CopyText_Click)
ContextMenu1.Items.Add("Reset View", Nothing, AddressOf ResetView)
End Sub
Public Sub New(ByRef ListViewObj As Windows.Forms.ListView, Optional DefaultViewConfig As String = "", Optional EnableLongStrings As Boolean = False)
LV = ListViewObj
ColumnCountVal = LV.Columns.Count
ReDim BlankStrArr(0 To ColumnCountVal - 2) 'string array is size of -2 because the first entry is added seperately
For i As Integer = 0 To ColumnCountVal - 2
BlankStrArr(i) = ""
Next i
GenerateColumnIndexDict()
GenerateBaseContextMenu()
LV.ContextMenuStrip = ContextMenu1
If DefaultViewConfig = "" Then
DefaultConfig = GetConfigStr() 'If default is not read from .ini then initial state is set as the config
Else
DefaultConfig = DefaultViewConfig
End If
_LastDPI = LV.DeviceDpi
If EnableLongStrings = True Then
AddHandler LV.DrawColumnHeader, Sub(s, ev) ev.DrawDefault = True
AddHandler LV.DrawSubItem, AddressOf ListView_DrawSubItem
AddHandler LV.DrawItem, AddressOf ListView_DrawItem ' required but empty
LV.OwnerDraw = True
End If
'LV.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize)
End Sub
'Delegate Function CustomFunc(str As String) As Boolean
Public Overloads Sub AddContextMenuFunction(FunctionName As String, func As Func(Of String, Boolean), Optional HeaderName As String = "[header,ANY]")
'This allows adding arbitrary functions to the LV right click menu
'Functions must take a single string as input and output a boolean
If HeaderName = "ANY" Then
ContextMenu1.Items.Add(FunctionName, Nothing, Function() func(GetClickedCellEntry()))
Else
ContextMenu1.Items.Add(FunctionName, Nothing, Function() func(GetClickedCellEntry(HeaderName)))
End If
End Sub
Public Overloads Sub AddContextMenuFunction(FunctionName As String, subroutine As System.Action)
'Add a subroutine to the right-click menu
ContextMenu1.Items.Add(FunctionName, Nothing, Sub() subroutine())
End Sub
Private Function GetClickedCellEntry(Optional HeaderName As String = "") As String
If HeaderName = "" Then
Return clickedItem.Text
Else
Return clickedParent.SubItems(ColumnDict(HeaderName)).Text
End If
End Function
Public ReadOnly Property ListView As ListView
Get
Return LV
End Get
End Property
Public ReadOnly Property ColumnCount As Integer
Get
Return ColumnCountVal
End Get
End Property
Public ReadOnly Property RowCount As Integer
Get
Return _RowCount
End Get
End Property
Public ReadOnly Property CheckedItemShownState As HideMode
Get
Return CheckedItemShownState
End Get
End Property
Public Property Checked(Optional Index As Integer = -1) As Boolean
Get
If Index = -1 Then
Return LV.Items(_RowCount - 1).Checked
Else
Return LV.Items(Index).Checked
End If
End Get
Set(value As Boolean)
If Index = -1 Then
LV.Items(_RowCount - 1).Checked = value
Else
LV.Items(Index).Checked = value
End If
End Set
End Property
Public Sub EnableToolTips()
lvToolTip = New ToolTip()
lvToolTip.ShowAlways = True
AddHandler LV.MouseMove, AddressOf ToolTipTextHandler
'AddHandler LV.MouseHover, AddressOf ToolTipTextHandler
End Sub
Public Sub ToolTipTextHandler(sender As Object, e As MouseEventArgs)
Dim LVsender As ListView = CType(sender, ListView)
'Dim hitTestInfo As ListViewHitTestInfo = LVsender.HitTest(LV.PointToClient(Cursor.Position))
Dim hitTestInfo As ListViewHitTestInfo = LVsender.HitTest(e.Location)
If hitTestInfo.SubItem IsNot Nothing Then
Dim str As String = hitTestInfo.SubItem.Text
If str <> lvToolTip.GetToolTip(LVsender) Then 'tooltip text is very buggy on listviews. This code is a workaround
If str.Contains(vbCrLf) Then
lvToolTip = New ToolTip()
lvToolTip.Active = True
lvToolTip.SetToolTip(LVsender, str)
Else
lvToolTip.Active = False
End If
End If
End If
End Sub
Private Sub GenerateColumnIndexDict()
Dim ColHeader As String
For i As Integer = 0 To LV.Columns.Count - 1
ColHeader = LV.Columns(i).Text.Trim({"↑"c, "↓"c}).Trim
Try
ColumnDict.Add(LV.Columns(i).Text, i)
Catch ex As Exception
ColumnDict.Item(LV.Columns(i).Text) = i
End Try
Next
End Sub
Public Function AddRow() As ListViewItem
Dim LVitem As ListViewItem = LV.Items.Add("")
LVitem.SubItems.AddRange(BlankStrArr)
_RowCount += 1
Return LVitem
End Function
Public Sub SetCellVal(val As String, HeaderName As String, Optional Index As Integer = -1)
If Index = -1 Then
Index = _RowCount - 1
End If
Dim ColIndex As Integer = ColumnDict(HeaderName)
LV.Items(Index).SubItems(ColIndex).Text = val
End Sub
Public Sub SetCellVal2(val As String, HeaderName As String, LVitem As ListViewItem)
'Set cell by ListViewItem directly
Dim ColIndex As Integer = ColumnDict(HeaderName)
LVitem.SubItems(ColIndex).Text = val
End Sub
Public Function GetCellEntry(LVitemOrIndex As Object, Header As String) As String
Dim LVitem As ListViewItem
If LVitemOrIndex.GetType() Is GetType(ListViewItem) Then
LVitem = LVitemOrIndex
Else
LVitem = LV.Items(LVitemOrIndex)
End If
Dim ColIndex As Integer = ColumnDict(Header)
Return LVitem.SubItems(ColIndex).Text
End Function
Public Sub ExpandColumnToFitContents(HeaderOrIndex As Object)
Dim lvColumn As Integer
If HeaderOrIndex.GetType() Is GetType(String) Then
lvColumn = ColumnDict(HeaderOrIndex)
Else
lvColumn = HeaderOrIndex
End If
Dim str As String
str = LV.Columns(lvColumn).Text
Dim w1 As Integer = TextRenderer.MeasureText(str, LV.Font).Width
For i As Integer = 0 To RowCount - 1
Dim w As Integer = TextRenderer.MeasureText(LV.Items(i).SubItems(lvColumn).Text, LV.Font).Width
If w > w1 Then
w1 = w
End If
Next
LV.Columns(lvColumn).Width = w1 + 8 * _LastDPI / 96
End Sub
Public Function GetSelectedEntryValue(Optional Header As String = "") As String
'Returns the string of the user selected row. If no header is given, it returns all column entries separated by ||
Dim LVitem As ListViewItem, Colindex As Integer, entry As String = ""
Try
LVitem = LV.SelectedItems(0)
Catch ex As Exception
Return ""
End Try
If Header <> "" Then
Colindex = ColumnDict(Header)
entry = LVitem.SubItems(Colindex).Text
Else
For Each subItem As ListViewItem.ListViewSubItem In LVitem.SubItems
entry = entry & subItem.Text & "||"
Next
entry = entry.Substring(0, entry.Length - 2)
End If
Return entry
End Function
Public Function GetSelectedLVitem() As ListViewItem
Dim LVitem As ListViewItem
Try
LVitem = LV.SelectedItems(0)
Catch ex As Exception
Return Nothing
End Try
Return LVitem
End Function
Public Function GetSelectedIndexes() As Integer()
'Returns the row index of
Dim indicies() As Integer, count As Integer = 0
For Each LVitem As ListViewItem In LV.Items
If LVitem.Selected = True Then
ReDim Preserve indicies(0 To count)
indicies(count) = LVitem.Index
count += 1
End If
Next
Return indicies
End Function
Public Overloads Sub SetBackColor(ARGBcolor As Color, Optional HeaderName As String = "[header,AllColumns]", Optional Index As Integer = -1)
If Index = -1 Then
Index = _RowCount - 1
End If
SetRowBackgroundColor(ARGBcolor, HeaderName, LV.Items(Index))
End Sub
Public Overloads Sub SetBackColor(ARGBcolor As Color, Optional HeaderName As String = "[header,AllColumns]", Optional LVitem As ListViewItem = Nothing)
SetRowBackgroundColor(ARGBcolor, HeaderName, LVitem)
End Sub
Private Sub SetRowBackgroundColor(ARGBcolor As Color, HeaderName As String, LVitem As ListViewItem)
If HeaderName = "AllColumns" Then
LVitem.UseItemStyleForSubItems = False
For i = 0 To ColumnCountVal - 1
LVitem.SubItems(i).BackColor = ARGBcolor
Next
Else
Dim ColIndex As Integer = ColumnDict(HeaderName)
LVitem.SubItems(ColIndex).BackColor = ARGBcolor
LVitem.UseItemStyleForSubItems = False
End If 'Color.FromArgb(RGBint Or &HFF000000)
'LV.Items(Index).SubItems(0).BackColor = Color.Gray
End Sub
'Use if only adding
Public Sub RightClickHandler(sender As Object, e As MouseEventArgs)
If e.Button = MouseButtons.Right Then
Dim hitTestInfo = LV.HitTest(e.X, e.Y)
If hitTestInfo.Item IsNot Nothing AndAlso hitTestInfo.SubItem IsNot Nothing Then
clickedItem = hitTestInfo.SubItem
clickedParent = hitTestInfo.Item
LV.ContextMenuStrip.Show(LV, e.Location)
Else
clickedItem = Nothing
clickedParent = Nothing
End If
Else
clickedItem = Nothing
clickedParent = Nothing
End If
End Sub
Private Sub CopyText_Click(sender As Object, e As EventArgs)
If clickedItem IsNot Nothing Then
Clipboard.SetText(clickedItem.Text)
End If
End Sub
Public Sub ColumnSortHandler(sender As Object, e As ColumnClickEventArgs)
'Dim LV As Windows.Forms.ListView = sender
Dim ColIndex As Integer = e.Column.ToString
Dim ColName As String = LV.Columns(ColIndex).Text
Dim SortArrow As String = Strings.Right(ColName, 1)
Dim ReverseOrder As Boolean
If SortArrow = "↑" Then
ReverseOrder = True
Else
ReverseOrder = False
End If
LV.ListViewItemSorter = New ListViewItemComparer(e.Column, ReverseOrder)
ColName = ColName.Trim({"↑"c, "↓"c}).Trim()
If SortArrow = "↑" Then
LV.Columns(ColIndex).Text = ColName & " ↓"
Else
LV.Columns(ColIndex).Text = ColName & " ↑"
End If
For i = 0 To LV.Columns.Count - 1
If ColIndex <> i Then
LV.Columns(i).Text = LV.Columns(i).Text.Trim({"↑"c, "↓"c}).Trim()
End If
Next i
End Sub
Public Sub ResetColumnSort()
For i = 0 To LV.Columns.Count - 1
LV.Columns(i).Text = LV.Columns(i).Text.Trim({"↑"c, "↓"c}).Trim()
Next i
LV.ListViewItemSorter = Nothing
End Sub
Public Sub ShowHideCheckedItems(mode As HideMode)
Dim CheckedItems() As ListViewItem = SavedCheckedItems
Dim UncheckedItems() As ListViewItem = SavedUncheckedItems
LV.BeginUpdate()
If mode <> HideMode.ShowAll And CurrentShowState = HideMode.ShowAll Then
CheckedAndUncheckedItemsToArray(CheckedItems, UncheckedItems)
If CurrentShowState = HideMode.ShowAll Then
SavedCheckedItems = CheckedItems
SavedUncheckedItems = UncheckedItems
End If
End If
Try
Select Case mode
Case HideMode.HideChecked
If CurrentShowState = HideMode.ShowAll Then
LV.Items.Clear()
LV.Items.AddRange(UncheckedItems)
CurrentShowState = HideMode.HideChecked
ElseIf CurrentShowState = HideMode.HideUnchecked Then
LV.Items.AddRange(UncheckedItems)
CheckedAndUncheckedItemsToArray(CheckedItems, UncheckedItems)
SavedCheckedItems = CheckedItems : SavedUncheckedItems = UncheckedItems
LV.Items.Clear()
LV.Items.AddRange(UncheckedItems)
Else ' CurrentShowState = HideMode.HideChecked Then
LV.Items.AddRange(CheckedItems)
CheckedAndUncheckedItemsToArray(CheckedItems, UncheckedItems)
SavedCheckedItems = CheckedItems : SavedUncheckedItems = UncheckedItems
LV.Items.Clear()
LV.Items.AddRange(UncheckedItems)
End If
CurrentShowState = HideMode.HideChecked
Case HideMode.HideUnchecked
If CurrentShowState = HideMode.ShowAll Then
LV.Items.Clear()
LV.Items.AddRange(CheckedItems)
ElseIf CurrentShowState = HideMode.HideUnchecked Then
LV.Items.AddRange(UncheckedItems)
CheckedAndUncheckedItemsToArray(CheckedItems, UncheckedItems)
SavedCheckedItems = CheckedItems : SavedUncheckedItems = UncheckedItems
LV.Items.Clear()
LV.Items.AddRange(CheckedItems)
Else ' CurrentShowState = HideMode.HideChecked Then
LV.Items.AddRange(CheckedItems)
CheckedAndUncheckedItemsToArray(CheckedItems, UncheckedItems)
SavedCheckedItems = CheckedItems : SavedUncheckedItems = UncheckedItems
LV.Items.Clear()
LV.Items.AddRange(CheckedItems)
End If
CurrentShowState = HideMode.HideUnchecked
Case HideMode.ShowAll
If CurrentShowState = HideMode.HideChecked Then
LV.Items.AddRange(CheckedItems)
ElseIf CurrentShowState = HideMode.HideUnchecked Then
LV.Items.AddRange(UncheckedItems)
'ElseIf CurrentShowState = HideMode.Filtered Then
' LV.Items.Clear()
' LV.Items.AddRange(FullListSaved)
Else 'if CurrentShowState = HideMode.ShowAll then
'no action needed
End If
CurrentShowState = HideMode.ShowAll
End Select
Catch ex As System.NullReferenceException 'Occurs if there are zero checked or unchecked items
CurrentShowState = mode
Catch ex As Exception
Stop
End Try
LV.EndUpdate()
End Sub
Private Sub CheckedAndUncheckedItemsToArray(ByRef CheckedItems() As ListViewItem, ByRef UncheckedItems() As ListViewItem)
ReDim CheckedItems(0)
ReDim UncheckedItems(0)
Dim i1 As Integer = 0, i2 As Integer = 0
For Each LVitem As ListViewItem In LV.Items
If LVitem.Checked = True Then
ReDim Preserve CheckedItems(i1)
CheckedItems(i1) = LVitem
i1 += 1
Else
ReDim Preserve UncheckedItems(i2)
UncheckedItems(i2) = LVitem
i2 += 1
End If
Next
End Sub
Public Function GetConfigStr() As String
Dim ConfigStr As String = ""
Dim sX As Single = 96 / LV.DeviceDpi
For i As Integer = 0 To ColumnCount - 1
Dim LVcolumn As ColumnHeader = LV.Columns.Item(i)
ConfigStr = $"{ConfigStr}ColWidth--{LVcolumn.Text}={CInt(LVcolumn.Width * sX)}¦ColDisplayIndex--{LVcolumn.Text}={LVcolumn.DisplayIndex}¦"
Next i
ConfigStr = $"{ConfigStr}TableFontSize--WholeTable={LV.Font.SizeInPoints * sX}"
ConfigStr = ConfigStr.Replace(" ↓", "")
ConfigStr = ConfigStr.Replace(" ↑", "")
Return ConfigStr.Substring(0, ConfigStr.Length)
End Function
Public Sub ApplyConfig(ConfigStr As String)
If ConfigStr = "" Then Exit Sub
Try
Dim ConfigArr() As String = Split(ConfigStr, "¦")
For Each str As String In ConfigArr
Dim p As Integer = Strings.InStrRev(str, "=")
Dim Val As String = str.Substring(p)
Dim ParamAndColumn() As String = str.Substring(0, p - 1).Split({"--"}, StringSplitOptions.None)
Dim Param As String = ParamAndColumn(0)
Dim ColumnName As String = ParamAndColumn(1)
Dim sX As Single = _LastDPI / 96
Select Case Param
Case "ColWidth"
LV.Columns.Item(ColumnDict(ColumnName)).Width = Val * sX
Case "ColDisplayIndex"
LV.Columns.Item(ColumnDict(ColumnName)).DisplayIndex = Val
Case "TableFontSize"
'LV.Font = New Font("Microsoft Sans Serif", CSng(Val), FontStyle.Regular)
End Select
Next
Catch ex As Exception
Stop
MsgBox("Error applying list view config. Config string most likely invalid.")
End Try
End Sub
Private Sub ResetView()
ApplyConfig(DefaultConfig)
End Sub
Public Sub UpdateColumnsFromDPIchange(newDPI As Integer)
Dim sX As Single = newDPI / _LastDPI
_LastDPI = newDPI
For i As Integer = 0 To LV.Columns.Count - 1
LV.Columns(i).Width = LV.Columns(i).Width * sX
Next
End Sub
Public Function GetCheckedItemsToArray(ColumnHeader As String) As String()
Dim ind As Integer = ColumnDict(ColumnHeader) 'TODO changes these to lists
Dim strArr() As String, i As Integer = 0
For Each LVitem As ListViewItem In LV.CheckedItems
ReDim Preserve strArr(i)
strArr(i) = LVitem.SubItems(ind).Text
i += 1
Next
Return strArr
End Function
Public Function GetUncheckedItemsToArray(ColumnHeader As String) As String()
Dim ind As Integer = ColumnDict(ColumnHeader)
Dim strArr() As String, i As Integer = 0
For Each LVitem As ListViewItem In LV.Items
If LVitem.Checked = False Then
ReDim Preserve strArr(i)
strArr(i) = LVitem.SubItems(ind).Text
i += 1
End If
Next
Return strArr
End Function
Public Sub UnFilterKeyWord(Optional PauseGUI As Boolean = True)
If AmFiltered = True Then
If PauseGUI = True Then LV.BeginUpdate()
LV.Items.Clear()
LV.Items.AddRange(ListPrefiltered)
AmFiltered = False
LV.EndUpdate()
End If
End Sub
Public Sub FilterByKeyword(keyWord As String, ColumnHeader As String, Optional mode As String = "[IsContainedAnywhere]")
keyWord = Strings.LCase(keyWord)
LV.BeginUpdate()
If AmFiltered = True Then
UnFilterKeyWord(False)
Else
ReDim ListPrefiltered(LV.Items.Count - 1)
LV.Items.CopyTo(ListPrefiltered, 0)
End If
Dim str As String
Dim ind As Integer = ColumnDict(ColumnHeader)
Dim MatchArr() As ListViewItem
Select Case mode
Case "IsContainedAnywhere"
'Dim MatchedItems() As ListViewItem
Dim i1 As Integer = 0, i2 As Integer = 0
For Each LVitem As ListViewItem In LV.Items
str = Strings.LCase(LVitem.SubItems(ind).Text)
If str.Contains(keyWord) = True Then
ReDim Preserve MatchArr(i1)
MatchArr(i1) = LVitem
i1 += 1
End If
Next
Case Else
Stop
End Select
LV.Items.Clear()
If MatchArr IsNot Nothing Then
LV.Items.AddRange(MatchArr)
End If
AmFiltered = True
LV.EndUpdate()
End Sub
'Private Sub ListView_DrawSubItem2(sender As Object, e As DrawListViewSubItemEventArgs)
' ' Get full text from Tag (stored as a string array per item)
' 'Dim fullTexts As String() = TryCast(e.Item.Tag, String())
' 'Dim fullText As String = If(fullTexts IsNot Nothing, fullTexts(e.ColumnIndex), e.SubItem.Text)
' Dim fullText As String = e.SubItem.Text
' e.DrawBackground() ' Draw background (handles selection highlight etc.)
' ' Draw the full text yourself
' TextRenderer.DrawText(
' e.Graphics,
' fullText,
' e.SubItem.Font,
' e.Bounds,
' e.SubItem.ForeColor,
' TextFormatFlags.Left Or TextFormatFlags.VerticalCenter Or TextFormatFlags.EndEllipsis
')
'End Sub
Private Sub ListView_DrawSubItem(sender As Object, e As DrawListViewSubItemEventArgs)
'Dim fullTexts As String() = TryCast(e.Item.Tag, String())
'Dim fullText As String = If(fullTexts IsNot Nothing, fullTexts(e.ColumnIndex), e.SubItem.Text)
Dim fullText As String = e.SubItem.Text 'the native list view object doesn't support long strings. But apparently .NET supports it internally
' Determine if this item is selected
Dim isSelected As Boolean = e.Item.Selected
Dim lv As ListView = DirectCast(sender, ListView)
Dim textColor As Color
If isSelected AndAlso lv.Focused Then ' Draw background manually to support full row highlight
e.Graphics.FillRectangle(SystemBrushes.Highlight, e.Bounds)
textColor = SystemColors.HighlightText
ElseIf isSelected AndAlso Not lv.Focused Then
e.Graphics.FillRectangle(SystemBrushes.InactiveCaption, e.Bounds)
textColor = SystemColors.InactiveCaptionText
Else
e.Graphics.FillRectangle(New SolidBrush(e.SubItem.BackColor), e.Bounds)
textColor = e.SubItem.ForeColor
End If
' Draw the full text
TextRenderer.DrawText(
e.Graphics,
fullText,
e.SubItem.Font,
e.Bounds,
textColor,
TextFormatFlags.Left Or TextFormatFlags.VerticalCenter Or TextFormatFlags.EndEllipsis
)
End Sub
Private Sub ListView_DrawItem(sender As Object, e As DrawListViewItemEventArgs)
' Draw focus rectangle on the focused item
If e.State And ListViewItemStates.Focused Then
e.DrawFocusRectangle()
End If
End Sub
End Class
Class ListViewItemComparer
Implements IComparer
Private col As Integer
Private ReverseOrder As Boolean
Public Sub New()
col = 0
End Sub
Public Sub New(column As Integer, ReverseOrder As Boolean)
col = column
Me.ReverseOrder = ReverseOrder
End Sub
Public Function Compare(x As Object, y As Object) As Integer Implements IComparer.Compare
Dim itemX As ListViewItem = CType(x, ListViewItem)
Dim itemY As ListViewItem = CType(y, ListViewItem)
Dim result As Integer, valueX As Double, ValueY As Double
Try
If Double.TryParse(itemX.SubItems(col).Text, valueX) AndAlso Double.TryParse(itemY.SubItems(col).Text, ValueY) Then
result = valueX.CompareTo(ValueY)
Else
result = [String].Compare(itemX.SubItems(col).Text, itemY.SubItems(col).Text)
End If
Catch ex As Exception
result = [String].Compare(itemX.SubItems(0).Text, itemY.SubItems(0).Text)
End Try
If ReverseOrder = True Then
result = -result
End If
Return result
End Function
End Class