11 Replies - 5736 Views - Last Post: 07 October 2010 - 03:56 PM Rate Topic: -----

#1 vennesschan  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 112
  • Joined: 20-April 10

How to find largest number within a listview column

Posted 07 October 2010 - 11:24 AM

Hi,

How to create a function to find the largest value(subitem) within a listview column

Thank you.
Is This A Good Question/Topic? 0
  • +

Replies To: How to find largest number within a listview column

#2 chrixko  Icon User is offline

  • D.I.C Head

Reputation: 16
  • View blog
  • Posts: 63
  • Joined: 25-November 08

Re: How to find largest number within a listview column

Posted 07 October 2010 - 12:08 PM

Hi vennesschan,

declare a variable like "BiggestNumber", then loop through the ListView and if the value is bigger than "biggestnumber" then set "biggestnumber" = value.

Some Pseudo-Code:
BiggestNumber = 0
For Each number in List
  if number > Biggestnumber then Biggestnumber = number
Next



Was This Post Helpful? 2
  • +
  • -

#3 vennesschan  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 112
  • Joined: 20-April 10

Re: How to find largest number within a listview column

Posted 07 October 2010 - 12:32 PM

Hi,

 Dim biggestnumber As Integer = 0
 Dim itm As ListViewItem
 Dim number As Integer


 For Each number In [b][u]itm.SubItems(3).Text[/u][/b] 'i am getting an error here
 If number > biggestnumber Then biggestnumber = number
  Next



Can someone tell me what did i do wrong.

Thank you.
Was This Post Helpful? 0
  • +
  • -

#4 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: How to find largest number within a listview column

Posted 07 October 2010 - 12:35 PM

Try
For Each Number as listviewitem in listview1.items
if Number.SubItems(3).Text ... 


Assuming your listview is named ListView1

And depending on your settings, you may need to convert that to a numeric type.

This post has been edited by CharlieMay: 07 October 2010 - 12:37 PM

Was This Post Helpful? 1
  • +
  • -

#5 vennesschan  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 112
  • Joined: 20-April 10

Re: How to find largest number within a listview column

Posted 07 October 2010 - 12:51 PM

Hi Charlie,

Thank you for your help but getting 0 in the result.

 Dim biggestnumber As Double = 0
                    Dim n As Double

                    For Each itm As ListViewItem In ListView1.Items
                        If CDbl(itm.SubItems(3).Text) > biggestnumber Then biggestnumber = n
                    Next
                    bnumber.Text = n


Was This Post Helpful? 0
  • +
  • -

#6 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: How to find largest number within a listview column

Posted 07 October 2010 - 12:58 PM

n = 0

You're comparing cdbl(itm.subitems(3).text) and then setting biggestnumber to n which is still 0. You need to set it to cdbl(itm.subitmes(3).text) when it's greater.

This post has been edited by CharlieMay: 07 October 2010 - 12:58 PM

Was This Post Helpful? 1
  • +
  • -

#7 vennesschan  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 112
  • Joined: 20-April 10

Re: How to find largest number within a listview column

Posted 07 October 2010 - 01:19 PM

Sorry, I am still not understand.
Was This Post Helpful? 0
  • +
  • -

#8 vennesschan  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 112
  • Joined: 20-April 10

Re: How to find largest number within a listview column

Posted 07 October 2010 - 01:44 PM

Sorry, I got what you mean now. Thank you very much. :)

By the way, can i use the same function to find out the smallest number?
Was This Post Helpful? 0
  • +
  • -

#9 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: How to find largest number within a listview column

Posted 07 October 2010 - 01:57 PM

Well, it would work just the opposite, you would need to start with a large number or take the first item in the list as the comparator and then check to see if the next number is smaller, if so, store and continue.
Was This Post Helpful? 0
  • +
  • -

#10 vennesschan  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 112
  • Joined: 20-April 10

Re: How to find largest number within a listview column

Posted 07 October 2010 - 02:18 PM

Hi Charlie,

I did the following code but sometime it found me the correct smallest number but sometime not. What did i do wrong?


 Dim smallestnumber As Double

 For Each itm2 As ListViewItem In ListView1.Items
 If CDbl(itm2.SubItems(4).Text) < CDbl(ListView1.Items(0).SubItems(4).Text) Then smallestnumber = CDbl(itm2.SubItems(4).Text)
  Next

  snumber.Text = smallestnumber


Was This Post Helpful? 0
  • +
  • -

#11 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: How to find largest number within a listview column

Posted 07 October 2010 - 02:36 PM

OK, sorry I kinda didn't give it a lot of thought. You will need to set smallestnumber to a number larger than anything in the listview. The reason your code didn't work was because it remains static throughout the test. For example, lets say items(0).SubItems(4) = 50
now if the next number is 48 then smallest number = 48 but... you're still checking the next number against 50 so if the next number is 49 it will store 49 in the smallestnumber but that is larger than 48. Does that make sense?

So what you can do is
 Dim smallesNumber as double = 500000 'keep in mind this needs to be larger than any possible number in your list

then use:
if cdbl(itm2.SubItems(4).Text) < smallestnumber Then smallestnumber = cdbl(itm2.subitems(4).text)



Actually, you could also do
Dim smallestNumber as double = cdbl(listview.items(0).subitems(4).Text) and still make the change to the if statement.

This post has been edited by CharlieMay: 07 October 2010 - 02:41 PM

Was This Post Helpful? 1
  • +
  • -

#12 vennesschan  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 112
  • Joined: 20-April 10

Re: How to find largest number within a listview column

Posted 07 October 2010 - 03:56 PM

It works, thank you Charlie :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1