# How to find largest number within a listview column

Page 1 of 1

## 11 Replies - 8520 Views - Last Post: 07 October 2010 - 03:56 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=193944&amp;s=f4951c087caa1f64b68a227c54b354d9&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 vennesschan

Reputation: 0
• 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

Reputation: 16
• 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

```

### #3 vennesschan

Reputation: 0
• 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.

### #4 CharlieMay

• This space intentionally left blank

Reputation: 1727
• Posts: 5,706
• 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

### #5 vennesschan

Reputation: 0
• 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

```

### #6 CharlieMay

• This space intentionally left blank

Reputation: 1727
• Posts: 5,706
• 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

### #7 vennesschan

Reputation: 0
• 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.

### #8 vennesschan

Reputation: 0
• 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?

### #9 CharlieMay

• This space intentionally left blank

Reputation: 1727
• Posts: 5,706
• 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.

### #10 vennesschan

Reputation: 0
• 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

```

### #11 CharlieMay

• This space intentionally left blank

Reputation: 1727
• Posts: 5,706
• 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

### #12 vennesschan

Reputation: 0
• 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