.NET Framework Bookmark and Share   
 index > .NET Base Class Library > Displaying the lowest number in a list box
 

Displaying the lowest number in a list box

I am looking for code that would help me find the lowest number in a list box
RKramer


 Private Function GetMinInListBox() As Integer
        Dim min As Integer
        If ListBox1.Items.Count > 0 Then
            min = ListBox1.Items(0) 'Set the value of first item
            For Each i As Integer In ListBox1.Items
                If i < min Then
                    min = i
                End If
            Next

        End If

        Return min
    End Function

Gaurav Khanna
  • Proposed As Answer byKhanna Gaurav Tuesday, September 22, 2009 6:19 PM
  •  
Khanna Gaurav
Hello
you can try the following

Private

Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Try

Dim apf(10) As String

ListBox1.Items.CopyTo(apf, 0)

Dim ap() As Integer = Array.ConvertAll(apf, New Converter(Of String, Integer)(AddressOf PointFToPoint))

Console.WriteLine()

Catch ex As Exception

End Try

End Sub



Public

Shared Function PointFToPoint(ByVal pf As String) As Integer

Return CInt(pf)

End Function

And now you can do your all rest of the operations

  • Proposed As Answer byAmolpbhavsar Monday, September 21, 2009 2:35 PM
  •  
Amolpbhavsar
A ListBox stores a collection of objects. Two conversions are required, first to a string, then to a number:

public static decimal? ListBoxMinimum(ListBox list) {
if (list.Items.Count == 0) return null;
decimal min = decimal.MaxValue;
foreach (object item in list.Items)
min = Math.Min(Convert.ToDecimal(item.ToString()), min);
return min;
}


Hans Passant.
  • Proposed As Answer byvarun007 Tuesday, September 22, 2009 6:12 AM
  •  
nobugz

You can use google to search for other answers

Custom Search

More Threads

• The name ‘ConfigurationManager?does not exist in the current context
• Read a text file and store in 2 dimentional array
• Returning a single element from an ArrayList
• Reverse string
• How to enter string with embedded empty double qoutes in the app.config file?
• ActiveDocumentHost
• WinForms GUI TEST tools?
• WMI Disk Drive Question & System.Media Question
• VS 2008 TabControl appearance / lok and feel
• DateTime.NowUtc offset problems