Hi,

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

Thank you.

## 11 Replies - 7827 Views - Last Post: 07 October 2010 - 03:56 PM

### #1

# How to find largest number within a listview column

Posted 07 October 2010 - 11:24 AM

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

### #2

## 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:

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

### #3

## Re: How to find largest number within a listview column

Posted 07 October 2010 - 12:32 PM

Hi,

Can someone tell me what did i do wrong.

Thank you.

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.

### #4

## Re: How to find largest number within a listview column

Posted 07 October 2010 - 12:35 PM

Try

Assuming your listview is named ListView1

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

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

### #5

## 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.

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

### #6

## 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.

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

### #7

## Re: How to find largest number within a listview column

Posted 07 October 2010 - 01:19 PM

Sorry, I am still not understand.

### #8

## 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?

By the way, can i use the same function to find out the smallest number?

### #9

## 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.

### #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?

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

### #11

## 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

then use:

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.

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

### #12

## Re: How to find largest number within a listview column

Posted 07 October 2010 - 03:56 PM

It works, thank you Charlie

Page 1 of 1