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
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!
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
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.
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
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.
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.