Results 1 to 9 of 9

Thread: [RESOLVED] Find smallest value ListView

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2007
    Location
    Sweden
    Posts
    359

    Resolved [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

  2. #2
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,196

    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

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2007
    Location
    Sweden
    Posts
    359

    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.

  4. #4
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,196

    Re: Find smallest value ListView

    Your only testing SubItem(1)

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2007
    Location
    Sweden
    Posts
    359

    Re: Find smallest value ListView

    Yes, but I want the other values at the same row's subItems.

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2007
    Location
    Sweden
    Posts
    359

    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

  7. #7
    Frenzied Member
    Join Date
    Jul 2011
    Location
    UK
    Posts
    1,335

    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

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2007
    Location
    Sweden
    Posts
    359

    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.

  9. #9
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,196

    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
  •  



Click Here to Expand Forum to Full Width