CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Jun 2010
    Posts
    15

    [RESOLVED] Find Max/Min value in a ListBox

    Hi Guys,
    I am new at c sharp and I would like to know how I can find a min and max value in a listBox.
    lets say I have a listbox like which get the elements from a text box and by a btn click events like this:

    int intItems = (int)Convert.ToInt32(txtInput.Text);
    listBox1.Items.Add(intItems);

    I know how to get count and sum and average of these elements

    could you please let me know how I can find these values(Max, Min)?
    Regards

  2. #2
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Find Max/Min value in a ListBox

    In VB I would set the sorted property to true on the list box.
    The first and the last item would be the min and max.
    Always use [code][/code] tags when posting code.

  3. #3
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Cool Re: Find Max/Min value in a ListBox

    The problem with having the ListBox's Sorted property is that it doesn't work too nicely with numbers and give the wrong results

    If I were to enter this into the ListBox - with the Sorted Property set to True :

    1
    2
    11
    3
    114
    5

    I would get this as a result :

    1
    11
    114
    2
    3
    5

    You see, it will group all the digits starting with 1 together... Which is wrong

    To get the Max ( or the highest entered value ) you could make use of Math.Max, as in the following example :

    Code:
            public int getMax(int[] arr)
            {
                int max = 0;
                foreach (int val in arr)
                    max = Math.Max(max, val);
                return max;
            }
    
            private void button3_Click(object sender, EventArgs e)
            {
                int[] array = new int[listBox1.Items.Count];
    
                for (int i = 0; i < listBox1.Items.Count; i++)
                {
                    object s = listBox1.Items[i];
                    array[i] = Convert.ToInt16(s);
                }
    
                MessageBox.Show(getMax(array).ToString());
    
               
            }
    Because we are dealing with a listbox, we are also dealing with an array. So we loop through the array ( which is the listbox's items ), convert the strings in the listbox to Integers, then fire the getMax method

    Simple

    To get the Min ( Lowest enetered number ) we can simply do the following :

    Code:
            public int getMin( params int[] arr)
            {
    
                int min = int.MaxValue;
                foreach (int value in arr)
                {
                    if (value < min)
                    {
                        min = value;
                    }
                }
                return min;
    
            } 
    
            private void button2_Click(object sender, EventArgs e)
            {
                int[] array = new int[listBox1.Items.Count];
    
                for (int i = 0; i < listBox1.Items.Count; i++)
                {
                    object s = listBox1.Items[i];
                    array[i] = Convert.ToInt16(s);
                }
    
                MessageBox.Show(getMin(array).ToString());
            }
    Voila!

    I am attaching a full working project here for you!

    I hope it helps
    Attached Files Attached Files

  4. #4
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Smile Re: Find Max/Min value in a ListBox

    Quote Originally Posted by DataMiser View Post
    The first and the last item would be the min and max.
    Thinking a bit further now.

    this is indeed a very easy way to identify the smallest value ( ie. the MIN value ), as it will be on the top of the list. You could just identify the first item and there you go The MAX is the whole problem as it will not be at the bottom of the list

  5. #5
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Find Max/Min value in a ListBox

    Ahh yes alpha sort, was late when I posted that.

    I now remember that I wrote some code for someone in the vb6 forum to do this very thing a while back.

    The general idea is to create two variables. Let's say Min and Max.
    Initialize Min with a high value and Max with a low value.

    Loop through the list checking each item against Min and Max.
    If item < Min then set Min equal to that item
    If item > Max then set Max equal to that item

    When the end of the loop is reached Min and Max will have the correct values.


    ETA: Sort is not needed.
    Last edited by DataMiser; June 18th, 2010 at 06:33 AM.
    Always use [code][/code] tags when posting code.

  6. #6
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Find Max/Min value in a ListBox

    btw even Min will not always work when dealing with numbers in a sorted list.

    e.g.


    100
    110
    2
    200
    3
    Always use [code][/code] tags when posting code.

  7. #7
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: Find Max/Min value in a ListBox

    Ahh, good one
    It seems as if the numeric sort depends on the next digit, so my previous post ( post #4 ), obviously didn't account for the 0 after the 1


  8. #8
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Find Max/Min value in a ListBox

    Actually the sort is based from left to right all numbers starting with 1 will be before any number starting with 2/ The same holds true for each position as we go to the right.

    1100000
    124000
    12500
    13
    135

    The only way to get a correct sort on numbers in a alpha sort is by left padding 0s

    0000013
    0000135
    0001250
    0012400
    1100000
    Last edited by DataMiser; June 18th, 2010 at 07:23 AM.
    Always use [code][/code] tags when posting code.

  9. #9
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: Find Max/Min value in a ListBox

    Good explanaition to all of us Thanx!

  10. #10
    Join Date
    Jun 2010
    Posts
    15

    Resolved Re: Find Max/Min value in a ListBox

    Thanks HanneSThEGreaT

    I got the idea completely.It was very helpful

  11. #11
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: Find Max/Min value in a ListBox

    I am glad I could help

    Please remember to mark your thread Resolved

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured