[2005] help in finding max number in listbox
hello
I have a listbox which contain grades I need to select the maximum grade.
Code:
Private Sub maxbtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles maxbtn.Click
Dim i As Integer
Dim m As Integer
m = Me.MarkList.Items.Count - 1
Dim Max As Double
For i = 0 To m
Max = MarkList.Items(0)
If Max < MarkList.Items(i) Then
Max = MarkList.Items(i)
End If
Next i
Me.MarkList.SelectedIndex = i
End Sub
should I write Me.MarkList.SelectedIndex = i to select the index? I have an error in this step I couldn't know what is the error in my statement.
Thanks
Re: [2005] help in finding max number in listbox
You are going in the right direction, just that you didn't save the index of the item with the max value. This should do it for you.
Code:
Private Sub maxbtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles maxbtn.Click
Dim i, indexMax As Integer
Dim m As Integer
m = Me.MarkList.Items.Count - 1
Dim Max As Double
If m > 0 Then
Max = CDble(MarkList.Items(0))
indexMax = 0
For i = 1 To m
If Max < CDble(MarkList.Items(i)) Then
Max = CDble(MarkList.Items(i))
indexMax = i
End If
Next i
Me.MarkList.SelectedIndex = indexMax
End If
End Sub
Re: [2005] help in finding max number in listbox
Your code is inefficient:
vb.net Code:
Private Sub maxbtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles maxbtn.Click
Dim _Max As Double = 0
For i = 0 To MarkList.Items.Count - 1
If _Max < CDbl(MarkList.Items(i)) Then
_Max = CDbl(MarkList.Items(i))
MarkList.SelectedIndex = i
End If
Next
End Sub
Re: [2005] help in finding max number in listbox
Quote:
Originally Posted by MaximilianMayrhofer
Your code is inefficient:
vb.net Code:
Private Sub maxbtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles maxbtn.Click
Dim _Max As Double = 0
For i = 0 To MarkList.Items.Count - 1
If _Max < CDbl(MarkList.Items(i)) Then
_Max = CDbl(MarkList.Items(i)
MarkList.SelectedIndex = i
End If
Next
End Sub
You may see the selected item keeps moving down the list as the code is executed. Besides, if the listbox.selectedindexchanged event is handled, that code will cause unexpected results.
Re: [2005] help in finding max number in listbox
Haha I did that on purpose. It's fun :D
But anyway, if there is a SelectedIndexChanged Event, then just adjust the code slightly to look like a more compact version of yours.
Re: [2005] help in finding max number in listbox
Code:
Dim tstA(ListBox1.Items.Count) As Double, max As Double = 0
For ctr As Integer = 0 To ListBox1.Items.Count - 1
If Not Decimal.TryParse(ListBox1.Items(ctr), tstA(ctr)) Then
tstA(ctr) = -1 'non numeric in listbox
End If
If tstA(ctr) > max Then
max = tstA(ctr) : ListBox1.SelectedIndex = ctr
End If
Next
'sort the scores
Array.Sort(tstA) 'high score is at tstA(tstA.Length - 1)
Debug.WriteLine("HIGH SCORE is " & tstA(tstA.Length - 1))
Re: [2005] help in finding max number in listbox
lol :D very cool .. thanks everyone for helping me out and entertaining us with this problem :cool:
I need to read about functions like these CDbl where do I find them? I don't use them at all