Results 1 to 7 of 7

Thread: [2005] help in finding max number in listbox

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2008
    Posts
    7

    [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

  2. #2
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    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

  3. #3
    Frenzied Member MaximilianMayrhofer's Avatar
    Join Date
    Aug 2007
    Location
    IM IN YR LOOP
    Posts
    2,001

    Re: [2005] help in finding max number in listbox

    Your code is inefficient:

    vb.net Code:
    1. Private Sub maxbtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles maxbtn.Click
    2.     Dim _Max As Double = 0
    3.     For i = 0 To MarkList.Items.Count - 1
    4.         If _Max < CDbl(MarkList.Items(i)) Then
    5.             _Max = CDbl(MarkList.Items(i))
    6.             MarkList.SelectedIndex = i
    7.         End If
    8.     Next
    9. End Sub
    Last edited by MaximilianMayrhofer; Apr 12th, 2008 at 09:02 AM.

  4. #4
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: [2005] help in finding max number in listbox

    Quote Originally Posted by MaximilianMayrhofer
    Your code is inefficient:

    vb.net Code:
    1. Private Sub maxbtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles maxbtn.Click
    2.     Dim _Max As Double = 0
    3.     For i = 0 To MarkList.Items.Count - 1
    4.         If _Max < CDbl(MarkList.Items(i)) Then
    5.             _Max = CDbl(MarkList.Items(i)
    6.             MarkList.SelectedIndex = i
    7.         End If
    8.     Next
    9. 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.

  5. #5
    Frenzied Member MaximilianMayrhofer's Avatar
    Join Date
    Aug 2007
    Location
    IM IN YR LOOP
    Posts
    2,001

    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.

  6. #6
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    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.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  7. #7

    Thread Starter
    New Member
    Join Date
    Mar 2008
    Posts
    7

    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
  •  



Click Here to Expand Forum to Full Width