-
Sep 2nd, 2022, 04:58 PM
#1
Thread Starter
Addicted Member
[RESOLVED] Help making this listview sort demo work
Trying to learn how to sort listview rows by a clicked column header. This listview sort demo was found with google but unfortunately it doesn't work correctly. It apparently is supposed to start with 5 rows of 4 columns but what it actually does is start with 1 row of 5 row starting values.
Can someone help me fix this so I can learn how to sort listviews? Alternatively, a pointer to a working demo would be good.
Code:
Public Class Form1
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
'Keep track of the last selected sort order using the Tag property
For Each col As Object In ListView1.Columns
col.tag = -1
Next
'populate a four column listview with some data
AddRow(ListView1, {"87013", "87013", "Fred", "Stalder"})
AddRow(ListView1, {"90215", "90215", "Virgil", "Samms"})
AddRow(ListView1, {"600021", "600021", "Rod", "Kinnison"})
AddRow(ListView1, {"2457", "2457", "Fred", "Flintstone"})
AddRow(ListView1, {"7259", "7259", "Nils", "Bergenholm"})
End Sub
Private Sub AddRow(lvw As System.Windows.Forms.ListView, data() As String)
'add a row to the given listview
Dim item As New ListViewItem()
item.Text = data(0)
For i As Integer = 1 To UBound(data)
item.SubItems.Add(data(i))
Next
lvw.Items.Add(item)
End Sub
Private Sub ListView1_ColumnClick(sender As System.Object, e As System.Windows.Forms.ColumnClickEventArgs) Handles ListView1.ColumnClick
Dim lvw As System.Windows.Forms.ListView = sender
'sort on the clicked column
Select Case e.Column
Case 0 : lvw.ListViewItemSorter = New NumericSorter
Case 1 : lvw.ListViewItemSorter = New SumSorter
Case 2 : lvw.ListViewItemSorter = New NameSorter(2, 3)
Case 3 : lvw.ListViewItemSorter = New NameSorter(3, 2)
End Select
'Toggle sort order
lvw.Sorting = IIf(lvw.Columns(e.Column).Tag = -1, SortOrder.Ascending, SortOrder.Descending)
lvw.Columns(e.Column).Tag *= -1
End Sub
Class NumericSorter 'sorts on the first column based on the numeric value
Implements IComparer
Public Function Compare(x As Object, y As Object) As Integer Implements System.Collections.IComparer.Compare
Dim item1 As ListViewItem = x
Dim item2 As ListViewItem = y
If item1.ListView.Sorting = SortOrder.Ascending Then
Return Math.Sign(CInt(item1.SubItems(0).Text) - CInt(item2.SubItems(0).Text))
Else
Return -Math.Sign(CInt(item1.SubItems(0).Text) - CInt(item2.SubItems(0).Text))
End If
End Function
End Class
Class SumSorter 'sorts on the second column based on the sum of the digits
Implements IComparer
Public Function Compare(x As Object, y As Object) As Integer Implements System.Collections.IComparer.Compare
Dim item1 As ListViewItem = x
Dim item2 As ListViewItem = y
Dim sum1 As Integer = 0
Dim sum2 As Integer = 0
For Each ch As Char In item1.SubItems(1).Text.ToCharArray
sum1 += Val(ch)
Next
For Each ch As Char In item2.SubItems(1).Text.ToCharArray
sum2 += Val(ch)
Next
If item1.ListView.Sorting = SortOrder.Ascending Then
Return Math.Sign(sum1 - sum2)
Else
Return -Math.Sign(sum1 - sum2)
End If
End Function
End Class
Class NameSorter 'sorts on the two columns given in the constructor
Implements IComparer
Private _columns() As Integer
Public Sub New(column1 As Integer, column2 As Integer)
_columns = {column1, column2}
End Sub
Public Function Compare(x As Object, y As Object) As Integer Implements System.Collections.IComparer.Compare
Dim item1 As ListViewItem = x
Dim item2 As ListViewItem = y
Dim text1 As String = item1.SubItems(_columns(0)).Text & item1.SubItems(_columns(1)).Text
Dim text2 As String = item2.SubItems(_columns(0)).Text & item2.SubItems(_columns(1)).Text
If item1.ListView.Sorting = SortOrder.Ascending Then
Return StrComp(text1, text2)
Else
Return -StrComp(text1, text2)
End If
End Function
End Class
End Class
-
Sep 2nd, 2022, 08:38 PM
#2
Re: Help making this listview sort demo work
I have to ask, Why are you using a ListView. It sounds like the DataGridView would be the correct control to use. It has the column sorting built into it and can be configured in a wide variety of visual looks. What does the ListView offer that can't be accomplished with a DGV?
-
Sep 2nd, 2022, 09:10 PM
#3
Thread Starter
Addicted Member
Re: Help making this listview sort demo work
I have never used a DGV and I will have to look and see what it is and what it is used for.
Just for reference, I will post my actual code and perhaps you could tell me if a DGV would work here in place of the LV?
Code:
Imports System.IO
Imports System.Reflection.Emit
Imports System.Runtime
Imports System.Windows.Forms
Imports System.Windows.Forms.VisualStyles.VisualStyleElement
Public Class Form1
Dim Panel1 As New System.Windows.Forms.Panel
Dim Panel2 As New System.Windows.Forms.Panel
Dim TextBoxDirectory As New System.Windows.Forms.TextBox
Dim TextBoxSearch As New System.Windows.Forms.TextBox
Dim TextBoxMatching As New System.Windows.Forms.TextBox
Dim TextBoxTotalFileCount As New System.Windows.Forms.TextBox
Dim LabelDirectory As New System.Windows.Forms.Label
Dim LabelSearch As New System.Windows.Forms.Label
Dim LabelMatching As New System.Windows.Forms.Label
Dim LabelTotalFileCount As New System.Windows.Forms.Label
Dim ButtonQuit As New System.Windows.Forms.Button
Dim ButtonFolder As New System.Windows.Forms.Button
Dim ButtonSearch As New System.Windows.Forms.Button
Dim ButtonSelectFolder As New System.Windows.Forms.Button
Dim CheckBoxes As New List(Of CheckBox)()
Dim ListView1 As New System.Windows.Forms.ListView
Dim ProgressBar1 As New System.Windows.Forms.ProgressBar
Dim OpenFileDialog1 As New System.Windows.Forms.OpenFileDialog
Dim FolderBrowserDialogDialog1 As System.Windows.Forms.FolderBrowserDialog
Dim columnWidths() As Double = {0.2, 0.5, 0.0, 0.0}
Dim exts As ArrayList = New ArrayList()
Dim first As Boolean = True
Dim Chkdata As String() = New String() {"*.*", "*.txt", "*.ino", "*.h",
"*.cpp", "*.doc", "*.xls", "*.vb",
"*.sln", "*.vbproj", "*.jpg", "*.mov",
"*.docx", "*.xlsx", "*.gif", "*.bmp"}
Private Async Function GetAllFilesAsync(ByVal folder As String) As Task(Of String())
Dim files As New List(Of String)
For Each f As String In Await Task.Run(Function()
Try
Return IO.Directory.GetDirectories(folder)
Catch ex As Exception
Return New String() {}
End Try
End Function)
files.AddRange(Await GetAllFilesAsync(f))
Next
files.AddRange(Await Task.Run(Function()
Try
Return IO.Directory.GetFiles(folder)
Catch ex As Exception
Return Array.Empty(Of String)()
End Try
End Function))
Return files.ToArray
End Function
Private Async Sub ButtonSearch_Click()
If first Then
Return
End If
Dim extMatched As Boolean = False
Dim FileDetails(6) As String
Dim iTem As ListViewItem
Dim info As FileInfo
Dim l As Integer
Dim fileCount = 0
Dim fileNames As String()
Dim isMatch As Boolean = False
Dim match = False
Dim allFiles As FileInfo
ListView1.Items.Clear()
TextBoxMatching.Text = 0
TextBoxTotalFileCount.Text = 0
ButtonSearch.Enabled = False
' Get all the file names in folder TextboxFolder.Text and subdirectories (may be thousands)
fileNames = Await GetAllFilesAsync(TextBoxDirectory.Text)
TextBoxTotalFileCount.Text = fileNames.Count
Dim cntLowEnd As Integer = 0
'process all the files in filenames
For Each fileName As String In fileNames
' see if the filename matches any of the extensions
fileName = fileName.ToLower()
extMatched = False
For Each ext In exts
extMatched = fileName Like ext
If extMatched = True Then
Exit For
End If
Next
cntLowEnd += 1
ProgressBar1.Value = CDbl(cntLowEnd) / CDbl(TextBoxTotalFileCount.Text) * 100
If extMatched Then
If TextBoxSearch.Text.Length <> 0 Then
Dim lines = Await Task.Run(Function() System.IO.File.ReadAllLines(fileName))
allFiles = Nothing
match = False
For Each line In lines
line = line.ToLower()
If TextBoxSearch.Text.Length = 0 Then ' make everthing match
match = True
End If
If Await Task.Run(Function() line.Contains(TextBoxSearch.Text.ToLower)) Then 'make TextBoxSearch.Text match
match = True
End If
Next
End If
If match Then
info = My.Computer.FileSystem.GetFileInfo(fileName)
Try
Dim dataMatch As Boolean = False
FileDetails(0) = info.Name
FileDetails(1) = info.DirectoryName
FileDetails(2) = info.LastWriteTime
If info.Length < 1024 Then
l = 1
Else
l = info.Length
End If
FileDetails(3) = l.ToString("###,###,###") + " KB"
iTem = New ListViewItem(FileDetails)
fileCount += 1
ListView1.Items.Add(iTem)
TextBoxMatching.Text = fileCount
Catch ex As Exception
End Try
End If
End If
Next
ButtonSearch.Enabled = True
End Sub
Private Sub ItemChecked(sender As Object, e As EventArgs)
Dim fileName, directoryName, extension, lastwritetime As String
If ListView1.SelectedItems.Count > 0 Then
'*********** transfer selected data ************'
fileName = ListView1.SelectedItems(0).SubItems(0).Text
directoryName = ListView1.SelectedItems(0).SubItems(1).Text
extension = ListView1.SelectedItems(0).SubItems(2).Text
lastwritetime = ListView1.SelectedItems(0).SubItems(3).Text
Process.Start("explorer", directoryName + "\" + fileName)
End If
End Sub
Private Sub CheckChange(sender As Object, e As EventArgs) 'comes here for changing any checkbox in the group CheckBoxes
StoreExtensions()
End Sub
Private Sub ButtonFolder_Click(sender As Object, e As EventArgs)
End Sub
Private Sub ButtonQuit_Click(sender As Object, e As EventArgs)
Application.Exit()
End Sub
Private Sub Form1_Resize(sender As Object, e As EventArgs) Handles Me.Resize
ResizeWindow()
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim buttonLeft = Me.Width - 100
Dim row = 105
Me.Text = "My Search"
Me.Width = 800
Me.Height = 600
Me.MinimumSize = New Point(800, 600)
' Me.WindowState = FormWindowState.Maximized
'Add a Progress Bar
Me.Controls.Add(ProgressBar1)
'Directory label
LabelDirectory.Text = "Search Directory"
LabelDirectory.Location = New Point(255, 30)
LabelDirectory.Size = New Point(95, 23)
Me.Controls.Add(LabelDirectory)
'directory textbox
TextBoxDirectory.Text = "C:\Users\Mike\Documents"
TextBoxDirectory.Location = New Point(350, 30)
TextBoxDirectory.Size = New Point(250, 200)
Me.Controls.Add(TextBoxDirectory)
'search label
LabelSearch.Text = "Search Value"
LabelSearch.Location = New Point(255, 55)
LabelSearch.Size = New Point(95, 23)
Me.Controls.Add(LabelSearch)
'search textbox
TextBoxSearch.Text = "Mike"
TextBoxSearch.Location = New Point(350, 55)
TextBoxSearch.Size = New Point(250, 50)
Me.Controls.Add(TextBoxSearch)
'matching label
LabelMatching.Text = "Matching Files"
LabelMatching.Location = New Point(255, 83)
LabelMatching.Size = New Point(85, 23)
Me.Controls.Add(LabelMatching)
'matching textbox
TextBoxMatching.Location = New Point(350, 80)
TextBoxMatching.Size = New Point(75, 23)
TextBoxMatching.Text = 0
Me.Controls.Add(TextBoxMatching)
'total files label
LabelTotalFileCount.Text = "Total Files"
LabelTotalFileCount.Location = New Point(450, 83)
LabelTotalFileCount.Size = New Point(70, 23)
Me.Controls.Add(LabelTotalFileCount)
'totalfiles textbox
TextBoxTotalFileCount.Text = 0
TextBoxTotalFileCount.Location = New Point(525, 80)
TextBoxTotalFileCount.Size = New Point(75, 23)
Me.Controls.Add(TextBoxTotalFileCount)
'Add buttons
ButtonQuit.Text = "Quit"
Me.Controls.Add(ButtonQuit)
AddHandler ButtonQuit.Click, AddressOf ButtonQuit_Click
ButtonFolder.Text = "Folder"
Me.Controls.Add(ButtonFolder)
AddHandler ButtonFolder.Click, AddressOf ButtonFolder_Click
ButtonSearch.Text = "Search"
Me.Controls.Add(ButtonSearch)
AddHandler ButtonSearch.Click, AddressOf ButtonSearch_Click
'Create Columns Headers
ListView1.Columns.Add("Name", 90, HorizontalAlignment.Center)
ListView1.Columns.Add("Folder", 90, HorizontalAlignment.Center)
ListView1.Columns.Add("Last Modified", 90, HorizontalAlignment.Center)
ListView1.Columns.Add("Size", 90, HorizontalAlignment.Center)
Me.Controls.Add(ListView1)
AddHandler ListView1.SelectedIndexChanged, AddressOf ItemChecked
'Add the Panel
Panel1.Location = New Point(10, 15)
Panel1.Size = New Point(600, 110)
Panel1.BorderStyle = BorderStyle.FixedSingle
Panel1.Font = New Font("Arial", 12)
Me.Controls.Add(Panel1)
Dim cbStartCol As Integer = 10
Dim cbStartRow As Integer = 10
Dim cbCol = cbStartCol
Dim cbRow = cbStartRow
Dim hOffset = 80
Dim vOffset = 24
'create 2 rows of 6 columns of checkboxes
For r = 0 To 2 ' rows
For c = 0 To 2 ' columns
Dim checkBox = New CheckBox()
Panel1.Controls.Add(checkBox)
checkBox.Location = New Point(cbCol + (hOffset * c), cbRow)
If c = 7 Or c = 15 Then
cbRow = cbStartRow + vOffset + (r * vOffset) - vOffset
End If
checkBox.Text = Chkdata(r * 4 + c)
Dim cf As New Font("Arial", 12)
checkBox.Font = cf
checkBox.Checked = False
checkBox.Size = New Size(80, 25)
CheckBoxes.Add(checkBox)
AddHandler checkBox.CheckedChanged, AddressOf CheckChange
Next
cbRow += vOffset
Next
ProgressBar1.Style = ProgressBarStyle.Blocks
ProgressBar1.Location = New Point(22, 110)
ProgressBar1.Size = New Point(576, 5)
CheckBoxes(1).Checked = True
StoreExtensions()
first = False
ResizeWindow()
End Sub
Public Sub StoreExtensions()
exts.Clear()
For i = 0 To CheckBoxes.Count - 1
If CheckBoxes(i).Checked Then
exts.Add(CheckBoxes(i).Text)
End If
Next
End Sub
Private Sub ResizeWindow()
If first Then
Return
End If
Dim W As Double = Me.Width - 60
ListView1.Location = New Point(10, 140)
ListView1.Size = New Size(Me.Width - 35, Me.Height - 205)
ListView1.BackColor = Color.White
ListView1.ForeColor = Color.Black
ListView1.View = View.Details
ListView1.GridLines = True
ListView1.Columns(0).Width() = W * columnWidths(0)
ListView1.Columns(1).Width() = W * columnWidths(1)
ListView1.Columns(2).Width() = 136
ListView1.Columns(3).Width() = 90
ListView1.Columns(0).TextAlign = HorizontalAlignment.Left
ListView1.Columns(1).TextAlign = HorizontalAlignment.Left
ListView1.Columns(2).TextAlign = HorizontalAlignment.Left
ListView1.Columns(3).TextAlign = HorizontalAlignment.Right
ButtonQuit.Location = New Point(Me.Width - 100, 70)
ButtonQuit.Size = New Point(75, 23)
ButtonSearch.Location = New Point(Me.Width - 100, 40)
ButtonSearch.Size = New Point(75, 23)
ButtonFolder.Location = New Point(Me.Width - 100, 10)
ButtonFolder.Size = New Point(75, 23)
End Sub
End Class
-
Sep 2nd, 2022, 09:14 PM
#4
Re: Help making this listview sort demo work
Generally speaking, if you aren't using at least one view other than Details and you aren't using groups then don't use a ListView. The DataGridView will almost certainly be superior. With a DataGridView, the cells can contain data of any type and data-binding is supported, which makes sorting by numbers or dates or the like a doddle. In a ListView, everything is text, so sorting by anything other than text is a right pain and even text is more trouble than in a DataGridView.
-
Sep 2nd, 2022, 09:33 PM
#5
Thread Starter
Addicted Member
Re: Help making this listview sort demo work
Tomorrow I will start working on a conversion from LV to DGV.
Thanks for the pointers.
-
Sep 2nd, 2022, 09:50 PM
#6
Re: Help making this listview sort demo work
Code:
Select Case e.Column
Case 0 : lvw.ListViewItemSorter = New NumericSorter
Case 1 : lvw.ListViewItemSorter = New SumSorter
Case 2 : lvw.ListViewItemSorter = New NameSorter(2, 3)
Case 3 : lvw.ListViewItemSorter = New NameSorter(3, 2)
End Select
'Toggle sort order
lvw.Sorting = IIf(lvw.Columns(e.Column).Tag = -1, SortOrder.Ascending, SortOrder.Descending)
lvw.Columns(e.Column).Tag *= -1
lvw.Sort
lvw.ListViewItemSorter = Nothing
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Sep 2nd, 2022, 10:37 PM
#7
Re: Help making this listview sort demo work
Originally Posted by .paul.
Code:
lvw.Sorting = IIf(lvw.Columns(e.Column).Tag = -1, SortOrder.Ascending, SortOrder.Descending)
Code:
lvw.Sorting = If(lvw.Columns(e.Column).Tag = -1, SortOrder.Ascending, SortOrder.Descending)
If you never use IIf, you can never accidentally use it when it matters.
-
Sep 2nd, 2022, 10:55 PM
#8
Re: Help making this listview sort demo work
Originally Posted by jmcilhinney
Code:
lvw.Sorting = If(lvw.Columns(e.Column).Tag = -1, SortOrder.Ascending, SortOrder.Descending)
If you never use IIf, you can never accidentally use it when it matters.
I noticed that after posting my amended version of PickyBiker's code. That wasn't the point i was trying to make...
Code:
lvw.Sort
lvw.ListViewItemSorter = Nothing
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Sep 2nd, 2022, 11:30 PM
#9
Re: Help making this listview sort demo work
Originally Posted by .paul.
I noticed that after posting my amended version of PickyBiker's code.
Didn't realise that that was a copy from the original code. The point stands for the OP's sake though.
-
Sep 3rd, 2022, 03:25 AM
#10
Re: Help making this listview sort demo work
Originally Posted by PickyBiker
It apparently is supposed to start with 5 rows of 4 columns but what it actually does is start with 1 row of 5 row starting values.
Do you have your ListView View property set to Details? Did you add 4 Columns in the designer?
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Sep 3rd, 2022, 10:38 AM
#11
Thread Starter
Addicted Member
Re: Help making this listview sort demo work
wes4dbt suggested using DataGridView instead of List View would be a better solution because it has a built-in sort and other features.
This morning I made that conversion and it works very well, including the sort.
Thank you wes4dbt!!!
Here is the converted code:
Code:
Imports System.Data.Common
Imports System.IO
Imports System.Reflection.Emit
Imports System.Runtime
Imports System.Windows.Forms
Imports System.Windows.Forms.VisualStyles.VisualStyleElement
Public Class Form1
Dim Panel1 As New System.Windows.Forms.Panel
Dim Panel2 As New System.Windows.Forms.Panel
Dim TextBoxDirectory As New System.Windows.Forms.TextBox
Dim TextBoxSearch As New System.Windows.Forms.TextBox
Dim TextBoxMatching As New System.Windows.Forms.TextBox
Dim TextBoxTotalFileCount As New System.Windows.Forms.TextBox
Dim LabelDirectory As New System.Windows.Forms.Label
Dim LabelSearch As New System.Windows.Forms.Label
Dim LabelMatching As New System.Windows.Forms.Label
Dim LabelTotalFileCount As New System.Windows.Forms.Label
Dim ButtonQuit As New System.Windows.Forms.Button
Dim ButtonFolder As New System.Windows.Forms.Button
Dim ButtonSearch As New System.Windows.Forms.Button
Dim ButtonSelectFolder As New System.Windows.Forms.Button
Dim CheckBoxes As New List(Of CheckBox)()
Dim dgv As New System.Windows.Forms.DataGridView
Dim ProgressBar1 As New System.Windows.Forms.ProgressBar
Dim OpenFileDialog1 As New System.Windows.Forms.OpenFileDialog
Dim FolderBrowserDialogDialog1 As System.Windows.Forms.FolderBrowserDialog
Dim columnWidths() As Double = {0.3, 0.7}
Dim exts As ArrayList = New ArrayList()
Dim first As Boolean = True
Dim Chkdata As String() = New String() {"*.*", "*.txt", "*.ino", "*.h",
"*.cpp", "*.doc", "*.xls", "*.vb",
"*.sln"}
Private Async Function GetAllFilesAsync(ByVal folder As String) As Task(Of String())
Dim files As New List(Of String)
For Each f As String In Await Task.Run(Function()
Try
Return IO.Directory.GetDirectories(folder)
Catch ex As Exception
Return New String() {}
End Try
End Function)
files.AddRange(Await GetAllFilesAsync(f))
Next
files.AddRange(Await Task.Run(Function()
Try
Return IO.Directory.GetFiles(folder)
Catch ex As Exception
Return Array.Empty(Of String)()
End Try
End Function))
Return files.ToArray
End Function
Private Async Sub ButtonSearch_Click()
If first Then
Return
End If
Dim extMatched As Boolean = False
Dim FileDetails(4) As String
' Dim iTem As DataGridViewItem
Dim info As FileInfo
Dim l As Integer
Dim fileCount = 0
Dim fileNames As String()
Dim isMatch As Boolean = False
Dim match = False
Dim allFiles As FileInfo
' DataGridView1.Items.Clear()
TextBoxMatching.Text = 0
TextBoxTotalFileCount.Text = 0
ButtonSearch.Enabled = False
' Get all the file names in folder TextboxFolder.Text and subdirectories (may be thousands)
fileNames = Await GetAllFilesAsync(TextBoxDirectory.Text)
TextBoxTotalFileCount.Text = fileNames.Count
Dim cntLowEnd As Integer = 0
'process all the files in filenames
For Each fileName As String In fileNames
' see if the filename matches any of the extensions
fileName = fileName.ToLower()
extMatched = False
For Each ext In exts
extMatched = fileName Like ext
If extMatched = True Then
Exit For
End If
Next
cntLowEnd += 1
ProgressBar1.Value = CDbl(cntLowEnd) / CDbl(TextBoxTotalFileCount.Text) * 100
If extMatched Then
If TextBoxSearch.Text.Length <> 0 Then
Dim lines = Await Task.Run(Function() System.IO.File.ReadAllLines(fileName))
allFiles = Nothing
match = False
For Each line In lines
line = line.ToLower()
If TextBoxSearch.Text.Length = 0 Then ' make everthing match
match = True
End If
If Await Task.Run(Function() line.Contains(TextBoxSearch.Text.ToLower)) Then 'make TextBoxSearch.Text match
match = True
End If
Next
End If
If match Then
info = My.Computer.FileSystem.GetFileInfo(fileName)
Try
Dim dataMatch As Boolean = False
FileDetails(0) = info.Name
FileDetails(1) = info.DirectoryName
FileDetails(2) = info.LastWriteTime
If info.Length < 1024 Then
l = 1
Else
l = info.Length
End If
FileDetails(3) = l.ToString("###,###,###") + " KB"
dgv.Rows.Add(FileDetails)
fileCount += 1
TextBoxMatching.Text = fileCount
Catch ex As Exception
End Try
End If
End If
Next
ButtonSearch.Enabled = True
End Sub
Private Sub ItemChecked(sender As Object, e As EventArgs)
'Dim fileName, directoryName, extension, lastwritetime As String
'If dgv.SelectedItems.Count > 0 Then
' '*********** transfer selected data ************'
' fileName = dgv.SelectedItems(0).SubItems(0).Text
' directoryName = dgv.SelectedItems(0).SubItems(1).Text
' extension = dgv.SelectedItems(0).SubItems(2).Text
' lastwritetime = dgv.SelectedItems(0).SubItems(3).Text
' Process.Start("explorer", directoryName + "\" + fileName)
'End If
End Sub
Private Sub CheckChange(sender As Object, e As EventArgs) 'comes here for changing any checkbox in the group CheckBoxes
StoreExtensions()
End Sub
Private Sub ButtonFolder_Click(sender As Object, e As EventArgs)
End Sub
Private Sub ButtonQuit_Click(sender As Object, e As EventArgs)
Application.Exit()
End Sub
Private Sub Form1_Resize(sender As Object, e As EventArgs) Handles Me.Resize
ResizeWindow()
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim buttonLeft = Me.Width - 100
Dim row = 105
Me.Text = "My Search"
Me.Width = 800
Me.Height = 600
Me.MinimumSize = New Point(800, 600)
' Me.WindowState = FormWindowState.Maximized
'Add a Progress Bar
Me.Controls.Add(ProgressBar1)
'Directory label
LabelDirectory.Text = "Search Directory"
LabelDirectory.Location = New Point(255, 30)
LabelDirectory.Size = New Point(95, 23)
Me.Controls.Add(LabelDirectory)
'directory textbox
TextBoxDirectory.Text = "C:\Users\Mike\Documents"
TextBoxDirectory.Location = New Point(350, 30)
TextBoxDirectory.Size = New Point(250, 200)
Me.Controls.Add(TextBoxDirectory)
'search label
LabelSearch.Text = "Search Value"
LabelSearch.Location = New Point(255, 55)
LabelSearch.Size = New Point(95, 23)
Me.Controls.Add(LabelSearch)
'search textbox
TextBoxSearch.Text = "Mike"
TextBoxSearch.Location = New Point(350, 55)
TextBoxSearch.Size = New Point(250, 50)
Me.Controls.Add(TextBoxSearch)
'matching label
LabelMatching.Text = "Matching Files"
LabelMatching.Location = New Point(255, 83)
LabelMatching.Size = New Point(85, 23)
Me.Controls.Add(LabelMatching)
'matching textbox
TextBoxMatching.Location = New Point(350, 80)
TextBoxMatching.Size = New Point(75, 23)
TextBoxMatching.Text = 0
Me.Controls.Add(TextBoxMatching)
'total files label
LabelTotalFileCount.Text = "Total Files"
LabelTotalFileCount.Location = New Point(450, 83)
LabelTotalFileCount.Size = New Point(70, 23)
Me.Controls.Add(LabelTotalFileCount)
'totalfiles textbox
TextBoxTotalFileCount.Text = 0
TextBoxTotalFileCount.Location = New Point(525, 80)
TextBoxTotalFileCount.Size = New Point(75, 23)
Me.Controls.Add(TextBoxTotalFileCount)
'Add buttons
ButtonQuit.Text = "Quit"
Me.Controls.Add(ButtonQuit)
AddHandler ButtonQuit.Click, AddressOf ButtonQuit_Click
ButtonFolder.Text = "Folder"
Me.Controls.Add(ButtonFolder)
AddHandler ButtonFolder.Click, AddressOf ButtonFolder_Click
ButtonSearch.Text = "Search"
Me.Controls.Add(ButtonSearch)
AddHandler ButtonSearch.Click, AddressOf ButtonSearch_Click
'Create Columns Headers
dgv.Columns.Add("Name", "Name")
dgv.Columns.Add("Folder", "Folder")
dgv.Columns.Add("Last Modified", "Last Modified")
dgv.Columns.Add("Size", "Size")
dgv.RowHeadersVisible = False
Me.Controls.Add(dgv)
'Add the Panel
Panel1.Location = New Point(10, 15)
Panel1.Size = New Point(600, 110)
Panel1.BorderStyle = BorderStyle.FixedSingle
Panel1.Font = New Font("Arial", 12)
Me.Controls.Add(Panel1)
Dim cbStartCol As Integer = 10
Dim cbStartRow As Integer = 10
Dim cbCol = cbStartCol
Dim cbRow = cbStartRow
Dim hOffset = 80
Dim vOffset = 24
'create 2 rows of 6 columns of checkboxes
For r = 0 To 2 ' rows
For c = 0 To 2 ' columns
Dim checkBox = New CheckBox()
Panel1.Controls.Add(checkBox)
checkBox.Location = New Point(cbCol + (hOffset * c), cbRow)
If c = 7 Or c = 15 Then
cbRow = cbStartRow + vOffset + (r * vOffset) - vOffset
End If
checkBox.Text = Chkdata(r * 3 + c)
Dim cf As New Font("Arial", 12)
checkBox.Font = cf
checkBox.Checked = False
checkBox.Size = New Size(80, 25)
CheckBoxes.Add(checkBox)
AddHandler checkBox.CheckedChanged, AddressOf CheckChange
Next
cbRow += vOffset
Next
ProgressBar1.Style = ProgressBarStyle.Blocks
ProgressBar1.Location = New Point(22, 110)
ProgressBar1.Size = New Point(576, 5)
CheckBoxes(1).Checked = True
StoreExtensions()
first = False
ResizeWindow()
End Sub
Public Sub StoreExtensions()
exts.Clear()
For i = 0 To CheckBoxes.Count - 1
If CheckBoxes(i).Checked Then
exts.Add(CheckBoxes(i).Text)
End If
Next
End Sub
Private Sub ResizeWindow()
If first Then
Return
End If
Dim W As Double = Me.Width - 60
dgv.Location = New Point(10, 140)
dgv.Size = New Size(Me.Width - 35, Me.Height - 205)
dgv.BackColor = Color.White
dgv.ForeColor = Color.Black
dgv.Columns(0).Width() = (W - 205) * columnWidths(0)
dgv.Columns(1).Width() = (W - 205) * columnWidths(1)
dgv.Columns(2).Width() = 132
dgv.Columns(3).Width() = 80
'Align the columns here
dgv.Columns(3).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
ButtonQuit.Location = New Point(Me.Width - 100, 70)
ButtonQuit.Size = New Point(75, 23)
ButtonSearch.Location = New Point(Me.Width - 100, 40)
ButtonSearch.Size = New Point(75, 23)
ButtonFolder.Location = New Point(Me.Width - 100, 10)
ButtonFolder.Size = New Point(75, 23)
End Sub
End Class
-
Sep 3rd, 2022, 10:46 AM
#12
Thread Starter
Addicted Member
Re: Help making this listview sort demo work
Originally Posted by .paul.
Do you have your ListView View property set to Details? Did you add 4 Columns in the designer?
When set to details, nothing is displayed. Partial info is displayed when view is set to small icons or list.
All: I never got the demo code to display more than one row, so no possibility to test sorting. It's is a pretty bad demo or, I don't have something set right in the Listview.
Last edited by PickyBiker; Sep 3rd, 2022 at 10:51 AM.
-
Sep 3rd, 2022, 01:24 PM
#13
Re: Help making this listview sort demo work
It has to be set to details,AND add four columns
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Sep 3rd, 2022, 01:28 PM
#14
Re: Help making this listview sort demo work
Originally Posted by PickyBiker
wes4dbt suggested using DataGridView instead of List View would be a better solution because it has a built-in sort and other features.
This morning I made that conversion and it works very well, including the sort.
Thank you wes4dbt!!!
Here is the converted code:
Code:
Imports System.Data.Common
Imports System.IO
Imports System.Reflection.Emit
Imports System.Runtime
Imports System.Windows.Forms
Imports System.Windows.Forms.VisualStyles.VisualStyleElement
Public Class Form1
Dim Panel1 As New System.Windows.Forms.Panel
Dim Panel2 As New System.Windows.Forms.Panel
Dim TextBoxDirectory As New System.Windows.Forms.TextBox
Dim TextBoxSearch As New System.Windows.Forms.TextBox
Dim TextBoxMatching As New System.Windows.Forms.TextBox
Dim TextBoxTotalFileCount As New System.Windows.Forms.TextBox
Dim LabelDirectory As New System.Windows.Forms.Label
Dim LabelSearch As New System.Windows.Forms.Label
Dim LabelMatching As New System.Windows.Forms.Label
Dim LabelTotalFileCount As New System.Windows.Forms.Label
Dim ButtonQuit As New System.Windows.Forms.Button
Dim ButtonFolder As New System.Windows.Forms.Button
Dim ButtonSearch As New System.Windows.Forms.Button
Dim ButtonSelectFolder As New System.Windows.Forms.Button
Dim CheckBoxes As New List(Of CheckBox)()
Dim dgv As New System.Windows.Forms.DataGridView
Dim ProgressBar1 As New System.Windows.Forms.ProgressBar
Dim OpenFileDialog1 As New System.Windows.Forms.OpenFileDialog
Dim FolderBrowserDialogDialog1 As System.Windows.Forms.FolderBrowserDialog
Dim columnWidths() As Double = {0.3, 0.7}
Dim exts As ArrayList = New ArrayList()
Dim first As Boolean = True
Dim Chkdata As String() = New String() {"*.*", "*.txt", "*.ino", "*.h",
"*.cpp", "*.doc", "*.xls", "*.vb",
"*.sln"}
Private Async Function GetAllFilesAsync(ByVal folder As String) As Task(Of String())
Dim files As New List(Of String)
For Each f As String In Await Task.Run(Function()
Try
Return IO.Directory.GetDirectories(folder)
Catch ex As Exception
Return New String() {}
End Try
End Function)
files.AddRange(Await GetAllFilesAsync(f))
Next
files.AddRange(Await Task.Run(Function()
Try
Return IO.Directory.GetFiles(folder)
Catch ex As Exception
Return Array.Empty(Of String)()
End Try
End Function))
Return files.ToArray
End Function
Private Async Sub ButtonSearch_Click()
If first Then
Return
End If
Dim extMatched As Boolean = False
Dim FileDetails(4) As String
' Dim iTem As DataGridViewItem
Dim info As FileInfo
Dim l As Integer
Dim fileCount = 0
Dim fileNames As String()
Dim isMatch As Boolean = False
Dim match = False
Dim allFiles As FileInfo
' DataGridView1.Items.Clear()
TextBoxMatching.Text = 0
TextBoxTotalFileCount.Text = 0
ButtonSearch.Enabled = False
' Get all the file names in folder TextboxFolder.Text and subdirectories (may be thousands)
fileNames = Await GetAllFilesAsync(TextBoxDirectory.Text)
TextBoxTotalFileCount.Text = fileNames.Count
Dim cntLowEnd As Integer = 0
'process all the files in filenames
For Each fileName As String In fileNames
' see if the filename matches any of the extensions
fileName = fileName.ToLower()
extMatched = False
For Each ext In exts
extMatched = fileName Like ext
If extMatched = True Then
Exit For
End If
Next
cntLowEnd += 1
ProgressBar1.Value = CDbl(cntLowEnd) / CDbl(TextBoxTotalFileCount.Text) * 100
If extMatched Then
If TextBoxSearch.Text.Length <> 0 Then
Dim lines = Await Task.Run(Function() System.IO.File.ReadAllLines(fileName))
allFiles = Nothing
match = False
For Each line In lines
line = line.ToLower()
If TextBoxSearch.Text.Length = 0 Then ' make everthing match
match = True
End If
If Await Task.Run(Function() line.Contains(TextBoxSearch.Text.ToLower)) Then 'make TextBoxSearch.Text match
match = True
End If
Next
End If
If match Then
info = My.Computer.FileSystem.GetFileInfo(fileName)
Try
Dim dataMatch As Boolean = False
FileDetails(0) = info.Name
FileDetails(1) = info.DirectoryName
FileDetails(2) = info.LastWriteTime
If info.Length < 1024 Then
l = 1
Else
l = info.Length
End If
FileDetails(3) = l.ToString("###,###,###") + " KB"
dgv.Rows.Add(FileDetails)
fileCount += 1
TextBoxMatching.Text = fileCount
Catch ex As Exception
End Try
End If
End If
Next
ButtonSearch.Enabled = True
End Sub
Private Sub ItemChecked(sender As Object, e As EventArgs)
'Dim fileName, directoryName, extension, lastwritetime As String
'If dgv.SelectedItems.Count > 0 Then
' '*********** transfer selected data ************'
' fileName = dgv.SelectedItems(0).SubItems(0).Text
' directoryName = dgv.SelectedItems(0).SubItems(1).Text
' extension = dgv.SelectedItems(0).SubItems(2).Text
' lastwritetime = dgv.SelectedItems(0).SubItems(3).Text
' Process.Start("explorer", directoryName + "\" + fileName)
'End If
End Sub
Private Sub CheckChange(sender As Object, e As EventArgs) 'comes here for changing any checkbox in the group CheckBoxes
StoreExtensions()
End Sub
Private Sub ButtonFolder_Click(sender As Object, e As EventArgs)
End Sub
Private Sub ButtonQuit_Click(sender As Object, e As EventArgs)
Application.Exit()
End Sub
Private Sub Form1_Resize(sender As Object, e As EventArgs) Handles Me.Resize
ResizeWindow()
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim buttonLeft = Me.Width - 100
Dim row = 105
Me.Text = "My Search"
Me.Width = 800
Me.Height = 600
Me.MinimumSize = New Point(800, 600)
' Me.WindowState = FormWindowState.Maximized
'Add a Progress Bar
Me.Controls.Add(ProgressBar1)
'Directory label
LabelDirectory.Text = "Search Directory"
LabelDirectory.Location = New Point(255, 30)
LabelDirectory.Size = New Point(95, 23)
Me.Controls.Add(LabelDirectory)
'directory textbox
TextBoxDirectory.Text = "C:\Users\Mike\Documents"
TextBoxDirectory.Location = New Point(350, 30)
TextBoxDirectory.Size = New Point(250, 200)
Me.Controls.Add(TextBoxDirectory)
'search label
LabelSearch.Text = "Search Value"
LabelSearch.Location = New Point(255, 55)
LabelSearch.Size = New Point(95, 23)
Me.Controls.Add(LabelSearch)
'search textbox
TextBoxSearch.Text = "Mike"
TextBoxSearch.Location = New Point(350, 55)
TextBoxSearch.Size = New Point(250, 50)
Me.Controls.Add(TextBoxSearch)
'matching label
LabelMatching.Text = "Matching Files"
LabelMatching.Location = New Point(255, 83)
LabelMatching.Size = New Point(85, 23)
Me.Controls.Add(LabelMatching)
'matching textbox
TextBoxMatching.Location = New Point(350, 80)
TextBoxMatching.Size = New Point(75, 23)
TextBoxMatching.Text = 0
Me.Controls.Add(TextBoxMatching)
'total files label
LabelTotalFileCount.Text = "Total Files"
LabelTotalFileCount.Location = New Point(450, 83)
LabelTotalFileCount.Size = New Point(70, 23)
Me.Controls.Add(LabelTotalFileCount)
'totalfiles textbox
TextBoxTotalFileCount.Text = 0
TextBoxTotalFileCount.Location = New Point(525, 80)
TextBoxTotalFileCount.Size = New Point(75, 23)
Me.Controls.Add(TextBoxTotalFileCount)
'Add buttons
ButtonQuit.Text = "Quit"
Me.Controls.Add(ButtonQuit)
AddHandler ButtonQuit.Click, AddressOf ButtonQuit_Click
ButtonFolder.Text = "Folder"
Me.Controls.Add(ButtonFolder)
AddHandler ButtonFolder.Click, AddressOf ButtonFolder_Click
ButtonSearch.Text = "Search"
Me.Controls.Add(ButtonSearch)
AddHandler ButtonSearch.Click, AddressOf ButtonSearch_Click
'Create Columns Headers
dgv.Columns.Add("Name", "Name")
dgv.Columns.Add("Folder", "Folder")
dgv.Columns.Add("Last Modified", "Last Modified")
dgv.Columns.Add("Size", "Size")
dgv.RowHeadersVisible = False
Me.Controls.Add(dgv)
'Add the Panel
Panel1.Location = New Point(10, 15)
Panel1.Size = New Point(600, 110)
Panel1.BorderStyle = BorderStyle.FixedSingle
Panel1.Font = New Font("Arial", 12)
Me.Controls.Add(Panel1)
Dim cbStartCol As Integer = 10
Dim cbStartRow As Integer = 10
Dim cbCol = cbStartCol
Dim cbRow = cbStartRow
Dim hOffset = 80
Dim vOffset = 24
'create 2 rows of 6 columns of checkboxes
For r = 0 To 2 ' rows
For c = 0 To 2 ' columns
Dim checkBox = New CheckBox()
Panel1.Controls.Add(checkBox)
checkBox.Location = New Point(cbCol + (hOffset * c), cbRow)
If c = 7 Or c = 15 Then
cbRow = cbStartRow + vOffset + (r * vOffset) - vOffset
End If
checkBox.Text = Chkdata(r * 3 + c)
Dim cf As New Font("Arial", 12)
checkBox.Font = cf
checkBox.Checked = False
checkBox.Size = New Size(80, 25)
CheckBoxes.Add(checkBox)
AddHandler checkBox.CheckedChanged, AddressOf CheckChange
Next
cbRow += vOffset
Next
ProgressBar1.Style = ProgressBarStyle.Blocks
ProgressBar1.Location = New Point(22, 110)
ProgressBar1.Size = New Point(576, 5)
CheckBoxes(1).Checked = True
StoreExtensions()
first = False
ResizeWindow()
End Sub
Public Sub StoreExtensions()
exts.Clear()
For i = 0 To CheckBoxes.Count - 1
If CheckBoxes(i).Checked Then
exts.Add(CheckBoxes(i).Text)
End If
Next
End Sub
Private Sub ResizeWindow()
If first Then
Return
End If
Dim W As Double = Me.Width - 60
dgv.Location = New Point(10, 140)
dgv.Size = New Size(Me.Width - 35, Me.Height - 205)
dgv.BackColor = Color.White
dgv.ForeColor = Color.Black
dgv.Columns(0).Width() = (W - 205) * columnWidths(0)
dgv.Columns(1).Width() = (W - 205) * columnWidths(1)
dgv.Columns(2).Width() = 132
dgv.Columns(3).Width() = 80
'Align the columns here
dgv.Columns(3).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
ButtonQuit.Location = New Point(Me.Width - 100, 70)
ButtonQuit.Size = New Point(75, 23)
ButtonSearch.Location = New Point(Me.Width - 100, 40)
ButtonSearch.Size = New Point(75, 23)
ButtonFolder.Location = New Point(Me.Width - 100, 10)
ButtonFolder.Size = New Point(75, 23)
End Sub
End Class
Bear in mind, a DGV is editable by the user, columns can be reordered, text changed etc. There are a lot of properties you can set to change the appearance and behaviour of the control
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Sep 3rd, 2022, 02:11 PM
#15
Thread Starter
Addicted Member
Re: Help making this listview sort demo work
Originally Posted by .paul.
It has to be set to details,AND add four columns
.Paul, Added the view=Details and added 4 columns, and then it worked. I should probably go back to where I found the demo and add the code with changes that makes it work.
Thank you!
-
Sep 3rd, 2022, 02:24 PM
#16
Re: Help making this listview sort demo work
Originally Posted by PickyBiker
.Paul, Added the view=Details and added 4 columns, and then it worked. I should probably go back to where I found the demo and add the code with changes that makes it work.
Thank you!
Looking at the code, a few things I noticed and the IIF that jmcilhinney pointed out, it appears to be a very old example. But most .Net code will run in later versions which is why there were only a few issues to sort out. Anyway, my point is that you probably got it from a very old article, and there’s not much need to point out their errors…
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Sep 3rd, 2022, 03:00 PM
#17
Thread Starter
Addicted Member
Re: Help making this listview sort demo work
-
Sep 3rd, 2022, 09:43 PM
#18
Re: Help making this listview sort demo work
Originally Posted by PickyBiker
I should probably go back to where I found the demo and add the code with changes that makes it work.
You would normally set the View property and add the columns in the designer rather than in code, so I expect that this demo assumes that has been done. It might even instruct that that be done. It wouldn't be the first time I've seen someone copy code and ignore instructions.
-
Sep 3rd, 2022, 10:42 PM
#19
Thread Starter
Addicted Member
Re: Help making this listview sort demo work
Originally Posted by jmcilhinney
You would normally set the View property and add the columns in the designer rather than in code, so I expect that this demo assumes that has been done. It might even instruct that that be done. It wouldn't be the first time I've seen someone copy code and ignore instructions.
You mean like this?
To create the form for this project you only have to add a ListView control
' to a blank form. Put the listview into details mode and add four columns.
' Set the widths of all columns to 100. Set the font to Courier New (or some
' other monospaced font). All listview entries for this demo are added at run
' time.
-
Sep 3rd, 2022, 10:46 PM
#20
Re: [RESOLVED] Help making this listview sort demo work
That's the instructions...
To create the form for this project you only have to add a ListView control
' to a blank form. Put the listview into details mode and add four columns.
' Set the widths of all columns to 100. Set the font to Courier New (or some
' other monospaced font). All listview entries for this demo are added at run
' time.
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Sep 4th, 2022, 02:00 AM
#21
Re: [RESOLVED] Help making this listview sort demo work
I'm lost. lol
You started with a ListView, then went to a DataGridView (which you said worked for you) and now your back to a ListView.
-
Sep 4th, 2022, 03:19 AM
#22
Re: [RESOLVED] Help making this listview sort demo work
Originally Posted by wes4dbt
I'm lost. lol
You started with a ListView, then went to a DataGridView (which you said worked for you) and now your back to a ListView.
It’s worth learning why it didn’t work, even if the OP chooses to use a DGV in the end.
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Sep 4th, 2022, 04:49 AM
#23
Re: Help making this listview sort demo work
Originally Posted by PickyBiker
When set to details, nothing is displayed. Partial info is displayed when view is set to small icons or list.
All: I never got the demo code to display more than one row, so no possibility to test sorting. It's is a pretty bad demo or, I don't have something set right in the Listview.
here a sample how to fill a Listview with some settings in the Form_Load
Code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
With ListView1
.Items.Clear()
.Columns.Clear()
.View = View.Details
.CheckBoxes = True
.Columns.Add("Column 0", 150, HorizontalAlignment.Center)
.Columns.Add("Column 1", 150, HorizontalAlignment.Center)
.Columns.Add("Column 2", 50)
.Columns.Add("Column 3", 50)
.Columns.Add("Column 4", 50)
For i As Integer = 0 To 10
Dim Li As New ListViewItem
Li.UseItemStyleForSubItems = False
Li.Text = "Item " & i.ToString & ".xyz"
For j As Integer = 0 To 3
Li.SubItems.Add("Item " & i.ToString & "." & (j + 1).ToString)
If (j = 1) And ((i Mod 2) = 1) Then
'set the Font you want:
Li.SubItems(1).Font = New Font(New FontFamily("Comic Sans MS"), 10, FontStyle.Regular)
Li.SubItems(2).Font = New Font(New FontFamily("Courier New"), 8, FontStyle.Bold)
If j <> Nothing Then
Li.Checked = True
End If
Exit For
End If
Next
.Items.Add(Li)
Next
End With
End Sub
to hunt a species to extinction is not logical !
since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.
-
Sep 4th, 2022, 05:04 AM
#24
Re: Help making this listview sort demo work
Good example, but...
Originally Posted by ChrisE
Code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
With ListView1
.Items.Clear()
.Columns.Clear()
.View = View.Details
.CheckBoxes = True
.Columns.Add("Column 0", 150, HorizontalAlignment.Center)
.Columns.Add("Column 1", 150, HorizontalAlignment.Center)
.Columns.Add("Column 2", 50)
.Columns.Add("Column 3", 50)
.Columns.Add("Column 4", 50)
For i As Integer = 0 To 10
Dim Li As New ListViewItem
Li.UseItemStyleForSubItems = False
Li.Text = "Item " & i.ToString & ".xyz"
For j As Integer = 0 To 3
Li.SubItems.Add("Item " & i.ToString & "." & (j + 1).ToString)
If (j = 1) And ((i Mod 2) = 1) Then
'set the Font you want:
Li.SubItems(1).Font = New Font(New FontFamily("Comic Sans MS"), 10, FontStyle.Regular)
Li.SubItems(2).Font = New Font(New FontFamily("Courier New"), 8, FontStyle.Bold)
If j <> Nothing Then ' is it likely to ever be nothing?
Li.Checked = True
End If
Exit For
End If
Next
.Items.Add(Li)
Next
End With
End Sub
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Sep 4th, 2022, 07:02 AM
#25
Re: Help making this listview sort demo work
Originally Posted by .paul.
Good example, but...
As j is type Integer, Nothing will be interpreted as zero, so I guess that code will check all but the first item.
-
Sep 4th, 2022, 07:05 AM
#26
Re: Help making this listview sort demo work
Originally Posted by jmcilhinney
As j is type Integer, Nothing will be interpreted as zero, so I guess that code will check all but the first item
I ran the code. It checks every other item. The j loop is the sub items loop… Execution only reaches my highlighted comment if j = 1
Last edited by .paul.; Sep 4th, 2022 at 07:09 AM.
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Sep 4th, 2022, 08:06 AM
#27
Re: Help making this listview sort demo work
Originally Posted by .paul.
I ran the code. It checks every other item. The j loop is the sub items loop… Execution only reaches my highlighted comment if j = 1
I see what you're saying. I should have read the code more closely
-
Sep 4th, 2022, 08:40 AM
#28
Thread Starter
Addicted Member
Re: [RESOLVED] Help making this listview sort demo work
Things I learned in this thread:
1: Don't just copy the example code without reading the INSTRUCTIONS
2: Don't be afraid to change course if someone proposes a better idea.
3: Even old demo code presents learning opportunities.
4: There are many expert coders on this site that are truly dedicated to helping others.
I now have the demo code working and I have improved my own code by using DGV instead of LV.
Thanks to all who helped.
-
Sep 4th, 2022, 10:24 AM
#29
Re: Help making this listview sort demo work
Originally Posted by .paul.
Good example, but...
If j <> Nothing Then ' is it likely to ever be nothing?
yep, my bad
to hunt a species to extinction is not logical !
since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|