Link to home
Start Free TrialLog in
Avatar of Jason
JasonFlag for Australia

asked on

Getting lowest item in listview greater than zero

I am getting an error with code if the first value in the listview is 0 the it does not move to the next item in listview

this is where the code mucks up

CType(lvin.SubItems(3).Text, Decimal) < CType(lplvin.SubItems(3).Text, Decimal)

if this CType(lvin.SubItems(3).Text, Decimal) = 10 and this CType(lplvin.SubItems(3).Text, Decimal) = 0
goes to endif with out calling lplvin = lvin

lplvin = lvin is the indexed item in listview

Can any one help

This only happens when first item in listview = 0


Dim lplvin As ListViewItem = ListView1.Items(0)


            For Each lvin As ListViewItem In ListView1.Items
                If CType(lvin.SubItems(3).Text, Decimal) > 0 Then
                    If CType(lvin.SubItems(3).Text, Decimal) < CType(lplvin.SubItems(3).Text, Decimal) Then
                        lplvin = lvin
                    End If
                End If
            Next

Open in new window

Avatar of Darrell Porter
Darrell Porter
Flag of United States of America image

Do you have any error handling code wrapped around this?  Is it possible you're getting a runtime conversion error and its blowing out of the For..Next loop?
Avatar of Jason

ASKER

No there is no runtime error it only happens if the first item in lplvin.subitems is zero then it when it call the if CType(lvin.SubItems(3).Text, Decimal) < CType(lplvin.SubItems(3).Text, Decimal) this ends up being false whenit should be true where the lplvin would go to the next item in the lplvin listviewitem
Avatar of Jason

ASKER

Here is a little app to show

Create a form with listview and 2 labels

Add the code into the form load

first comment out the second bob listview item and uncomment the first.

you will find result = 0 when it should = 1234

now switch comment around again and result = 1234

if you do the same with comments with pete then code still works

So it relates to 0 as the first item in list

I hope the helps explain the problem


Public Class Form1

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        ListView1.View = View.Details
        ListView1.Columns.Add("Name", 100, HorizontalAlignment.Center)
        ListView1.Columns.Add("Number", 60, HorizontalAlignment.Center)


        With ListView1
            .Items.Add("Bob")
            With .Items(.Items.Count - 1).SubItems()
                '.Add("0000")
                .Add("1300")
            End With
        End With
        With ListView1
            .Items.Add("Joe")
            With .Items(.Items.Count - 1).SubItems()
                .Add("1234")
            End With
        End With
        With ListView1
            .Items.Add("Pete")
            With .Items(.Items.Count - 1).SubItems()
                '.Add("4223")
                .Add("0000")
            End With
        End With

        Dim lplvin As ListViewItem = ListView1.Items(0)

        For Each lvin As ListViewItem In ListView1.Items
            If CType(lvin.SubItems(1).Text, Decimal) > 0 Then
                If CType(lvin.SubItems(1).Text, Decimal) < CType(lplvin.SubItems(1).Text, Decimal) Then
                    lplvin = lvin
                End If
            End If
        Next

        Label2.Text = CType(lplvin.SubItems(1).Text, Decimal)


    End Sub
End Class

Open in new window

OK - Kludge inbound

Try this:

For Each lvin As ListViewItem In ListView1.Items
  DisposableVariable1 = CType(lvin.SubItems(1).Text, Decimal)
  DisposableVariable2 = CType(lplvin.SubItems(1).Text, Decimal)
  If DisposableVariable1 > 0 Then
    If DisposableVariable1 < DisposableVariable2 Then
      lplvin = lvin
    End If
  End If
Next


Avatar of Jason

ASKER

Same error if zero is the first in listviewItems the when DisposableVariable2 = 0
then statement is called If DisposableVariable1 < DisposableVariable2 Then
      lplvin = lvin
    End If

Even if DisposableVariable1 is greater than zero the statement still = false therefore not changing lplvin

So code does the same
As DECIMAL is a particular type, I'd suggest to convert the text to type LONG if the capacity of LONG is sufficient (-9,223,372,036,854,775,808 through 9,223,372,036,854,775,807.)
Reading again what you wrote I get this:

         For Each lvin As ListViewItem In ListView1.Items
                If 10 > 0 Then
                    If 10 <0 Then
                        lplvin = lvin
                    End If
                End If
            Next

as 10 is not less than 0, the assigment is skipped.

code it like this:

         
   Dim gotOne as boolean = false
            For Each lvin As ListViewItem In ListView1.Items
               If CType(lvin.SubItems(3).Text, Decimal) > 0 Then
                  if not gotOne then
                      lplvin = lvin
                      gotOne = true
                else
                    If CType(lvin.SubItems(3).Text, Decimal) < CType(lplvin.SubItems(3).Text, Decimal) Then
                        lplvin = lvin
                    End If
                End If
            Next

Open in new window

Avatar of Jason

ASKER

What it is there are two listviewitems called lplvin and lvin from the same listview

So both lists are identical both start with 0

The problem is when the code gets to

If CType(lvin.SubItems(1).Text, Decimal) < CType(lplvin.SubItems(1).Text, Decimal) Then

lplvin.SubItems(1).Text = 0 all number in lvin.SubItems(1).Text are equal or greater therefore resulting in false

what I need is if lplvin.SubItems(1).Text = 0 then lplvin.SubItems(1).Text to move to the next item in lplvin list


 
Dim lplvin As ListViewItem = ListView1.Items(0)

        If CType(lplvin.SubItems(1).Text, Double) = 0 Then
I need some code here to move to next list item
        End If

        For Each lvin As ListViewItem In ListView1.Items
            If CType(lvin.SubItems(1).Text, Double) > 0 Then
                If CType(lvin.SubItems(1).Text, Double) < CType(lplvin.SubItems(1).Text, Double) Then
                    lplvin = lvin
                End If
            End If
        Next

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Jason
Jason
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Let's review some logic.

For the case

 CType(lvin.SubItems(1).Text, Double) < CType(lplvin.SubItems(1).Text, Double)

If CType(lplvin.SubItems(1).Text, Double) is 0, and CType(lvin.SubItems(1).Text, Double) is a non-negative number then it will NOT set lplvin to lvin.
If CType(lplvin.SubItems(1).Text, Double) is 0, and CType(lvin.SubItems(1).Text, Double) is 0 then it will NOT set lplvin to lvin.

Is this what you're expecting?

Avatar of Jason

ASKER

Was helped by third party
And the solution was what?
Avatar of Jason

ASKER

The solution was a logic error where on the original CType(lplvin.SubItems(1).Text, was asigned 0 from
this statement Dim lplvin As ListViewItem = ListView1.Items(0)

Which meant because the first in the list was 0.

What the new code does is it does not assign anything to CType(lplvin.SubItems(1).Text therefore allowing the two lists to run through each other not catching 0 where item cant be less than 0

Hope this explains the solution.

If not clear copy and create the little app i created and replace with new code.
if you debug the code you will see the differences

Thanks for your help and help from others though I appreciate any help and advice
i GUESS YOU DID NOT EVEN READ MY SOLUTION, IT'S EXACTLY WHAT YOU ACCEPTED AS THE SOLUTION. Ig you change GotOne in Nothing, I don't see any differrence. Do you?
Avatar of Jason

ASKER

Ambusy I tried yours I copied you code and used what you had but did work I'll look again now.
Avatar of Jason

ASKER

did not work sorry
Avatar of Jason

ASKER

Truly Sorry Ambusy it does work I did try honestly but must have been me that stuffed not your code.  If i clicked on to this earlier I would have saved time.

If an admin is reading this I need to correct this and award to Ambusy for 10/28/11 10:35 PM, ID: 37044114 along with the points

Sorry Ambusy