-
Jul 25th, 2015, 12:42 PM
#1
Thread Starter
Hyperactive Member
[RESOLVED] Find smallest value ListView
Hi, all.
I want to find the lowest value in a ListView's subItem. I have been trying with the code below, but it only gives me the last values in the ListView.
Code:
Dim num As Integer
num = Me.ListView1.Items.Count - 1
For x = 0 To num
Dim smallestNumber As Double = CDbl(ListView1.Items(x).SubItems(1).Text)
If CDbl(Me.ListView1.Items(x).SubItems(1).Text) < smallestNumber Then smallestNumber = CDbl(Me.ListView1.Items(x).SubItems(1).Text)
lblResult.Text = "Best Match @ " & " Freq: " & ListView1.Items(x).SubItems(0).Text & "Hz " & "SWR: " & smallestNumber.ToString & " R: " & ListView1.Items(x).SubItems(2).Text & _
" X: " & ListView1.Items(x).SubItems(3).Text & " Z: " & ListView1.Items(x).SubItems(4).Text
Next
-
Jul 25th, 2015, 12:53 PM
#2
Re: Find smallest value ListView
Try this,
move the Dim smallestNumber outside the loop, otherwise your just resetting it every loop
Code:
Dim num As Integer
num = Me.ListView1.Items.Count - 1
Dim smallestNumber As Double = CDbl(ListView1.Items(0).SubItems(1).Text)
For x = 1 To num
If CDbl(Me.ListView1.Items(x).SubItems(1).Text) < smallestNumber Then smallestNumber = CDbl(Me.ListView1.Items(x).SubItems(1).Text)
lblResult.Text = "Best Match @ " & " Freq: " & ListView1.Items(x).SubItems(0).Text & "Hz " & "SWR: " & smallestNumber.ToString & " R: " & ListView1.Items(x).SubItems(2).Text & _
" X: " & ListView1.Items(x).SubItems(3).Text & " Z: " & ListView1.Items(x).SubItems(4).Text
Next
-
Jul 25th, 2015, 01:11 PM
#3
Thread Starter
Hyperactive Member
Re: Find smallest value ListView
Hi, thank's.
Yes, that worked. However not for the other subItems they are still the last row values.
I guess I need to declare them first as well.
-
Jul 25th, 2015, 01:17 PM
#4
Re: Find smallest value ListView
Your only testing SubItem(1)
-
Jul 25th, 2015, 01:20 PM
#5
Thread Starter
Hyperactive Member
Re: Find smallest value ListView
Yes, but I want the other values at the same row's subItems.
-
Jul 25th, 2015, 02:01 PM
#6
Thread Starter
Hyperactive Member
Re: Find smallest value ListView
This code only give the first number for the frequency, but it looks for the same value.
Code:
Dim num As Integer
num = Me.ListView1.Items.Count - 1
Dim smallestNumber As Double = CDbl(ListView1.Items(0).SubItems(1).Text)
Dim Freq As Double = CDbl(ListView1.Items(0).SubItems(0).Text)
For x = 1 To num
If CDbl(Me.ListView1.Items(x).SubItems(1).Text) < smallestNumber Then smallestNumber = CDbl(Me.ListView1.Items(x).SubItems(1).Text)
If CDbl(Me.ListView1.Items(x).SubItems(1).Text) < smallestNumber Then Freq = CDbl(Me.ListView1.Items(x).SubItems(0).Text)
lblResult.Text = "Best Match @ " & " Freq: " & Freq & "Hz " & "SWR: " & smallestNumber.ToString & " R: " & ListView1.Items(x).SubItems(2).Text & _
" X: " & ListView1.Items(x).SubItems(3).Text & " Z: " & ListView1.Items(x).SubItems(4).Text
Next
-
Jul 25th, 2015, 02:56 PM
#7
Re: Find smallest value ListView
Iterate through all the rows in the ListView. keep a record of the smallest value found, and also keep a record of the row that it occurs on.
wes4dbt's code starts off by making the assumption that the first row has the smallest value, so you only need to iterate from the second row (index = 1) looking for smaller values.
When the loop finishes, you will have a record of the row that contains the smallest value. Use that to find your other values.
Update the label only after the loop finishes.
Code:
Dim filteredRowIndex As Integer = 0
Dim smallestValue As Double = CDbl(ListView1.Items(0).SubItems(1).Text)
For rowIndex As Integer = 1 To ListView1.Items.Count - 1
Dim thisValue As Double = CDbl(ListView1.Items(rowIndex).SubItems(1).Text)
If thisValue < smallestValue Then
filteredRowIndex = rowIndex
smallestValue = thisValue
End If
Next
lblResult.Text = String.Format("Best Match @ Freq: {0}Hz SWR: {1} R: {2} X: {3} Z: {4}", freq,
smallestValue,
ListView1.Items(filteredRowIndex).SubItems(2).Text,
ListView1.Items(filteredRowIndex).SubItems(3).Text,
ListView1.Items(filteredRowIndex).SubItems(4).Text)
If two or more SWR's can have the same minimum value, the code will find the first row that has that minimum value. To find the last row, change If thisValue < smallestValue Then to If thisValue <= smallestValue Then
-
Jul 25th, 2015, 03:19 PM
#8
Thread Starter
Hyperactive Member
Re: Find smallest value ListView
Thank's
Worked fine. I needed to change this:
Code:
lblResult.Text = String.Format("Best Match @ Freq: {0}Hz SWR: {1} R: {2} X: {3} Z: {4}", ListView1.Items(filteredRowIndex).SubItems(0).Text,
smallestValue,
ListView1.Items(filteredRowIndex).SubItems(2).Text,
ListView1.Items(filteredRowIndex).SubItems(3).Text,
ListView1.Items(filteredRowIndex).SubItems(4).Text)
You are right, there is more values which is equal. The best would be to compare with the ListView1.Items(filteredRowIndex).SubItems(4).Text), which is the impedance value.
The value nearest 50 is the best match. Meaning something like:
When smallestValue=true and ListView1.Items(filteredRowIndex).SubItems(4).Text) = closest to 50 then
...
thanks for your help.
-
Jul 25th, 2015, 06:25 PM
#9
Re: [RESOLVED] Find smallest value ListView
wes4dbt's code starts off by making the assumption that the first row has the smallest value, so you only need to iterate from the second row (index = 1) looking for smaller values.
No I don't, there is just no need to test index 0 because you just assigned it to "smallestNumber". If you tested index 0, you would be testing the same two values.
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
|