14,981,116 members
See more:
Hi every one,

I am a beginner for c#.

I am trying to show the small and largest number in a text box from the listview; however, it did not succeed after I spent 3 hours to google and try to fix it.

The simple view looks like this:

Sydney,5230330,City
Melbourne,4936349,City
Brisbane,2462637,City
Nelson Bay,28051,Town
Maryborough,27282,Town

What I have tried:

C#
```private void calculateButton_Click(object sender, EventArgs e)
{
//Find the larget population

Decimal iMax = 0;
Decimal iMin = 0;

foreach (ListViewItem o in this.listView1.Items)
{
iMax = iMax + Convert.ToDecimal(o.SubItems[1].Text);
}
Decimal maximum1 = Math.Max(iMax, iMin);
largestTextBox.Text = maximum1.ToString();

foreach (ListViewItem o in this.listView1.Items)
{
iMin = iMin + Convert.ToDecimal(o.SubItems[1].Text);
}
Decimal minimum = Math.Min(iMin, iMax);
smallestTextBox.Text = minimum.ToString();
}```
Posted
Updated 5-Nov-20 15:05pm
v2
George Swan 6-Nov-20 3:19am

Try working on the data in the backing collection that is used to produce the display rather than the displayed list.

Solution 1

Why are you "summing"? Min / max doesn't use sums.

For the first item, it is both the Min and the Max (assignment).

After that, the "new" min is the next item if it is less than Min,
else the new Max is the next item greater than Max.

At the end, you have a Min and a Max.

Solution 2

Quote:
I am trying to show the small and largest number in a text box from the listview; however, it did not succeed after I spent 3 hours to google and try to fix it.

First train yourself to find min a&nd max in a list of numbers by hand with a sheet of paper and a pencil.
Obviously you already found functions to find minimum or maximum of 2 numbers, try to take advantage of them.
Note that max and min are solved using the same procedure.
- in a list of 1 number, how do you find minimum ?
- in a list of 2 numbers, how do you find minimum ?
- you know minimum of a list, if you add 1 number, how do you find new minimum ?

Top Experts
Last 24hrsThis month
 Richard Deeming 165 Richard MacCutchan 120 OriginalGriff 105 Maciej Los 60 Wendelius 60
 OriginalGriff 369 Patrice T 135 Richard Deeming 90 Dave Kreskowiak 85 Richard MacCutchan 80

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900