Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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

Please help me to fix it and thank you all of you in advance.

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

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.
 
Share this answer
 
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.
The answers to those questions are your algorithm.
- 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 ?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



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