|
-
Apr 12th, 2008, 08:33 AM
#1
Thread Starter
New Member
[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
-
Apr 12th, 2008, 08:46 AM
#2
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
-
Apr 12th, 2008, 08:55 AM
#3
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
Last edited by MaximilianMayrhofer; Apr 12th, 2008 at 09:02 AM.
-
Apr 12th, 2008, 09:01 AM
#4
Re: [2005] help in finding max number in listbox
 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.
-
Apr 12th, 2008, 09:05 AM
#5
Re: [2005] help in finding max number in listbox
Haha I did that on purpose. It's fun 
But anyway, if there is a SelectedIndexChanged Event, then just adjust the code slightly to look like a more compact version of yours.
-
Apr 12th, 2008, 09:16 AM
#6
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))
Last edited by dbasnett; Apr 12th, 2008 at 09:36 AM.
-
Apr 12th, 2008, 10:12 AM
#7
Thread Starter
New Member
Re: [2005] help in finding max number in listbox
lol very cool .. thanks everyone for helping me out and entertaining us with this problem
I need to read about functions like these CDbl where do I find them? I don't use them at all
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|