IT KNOWLEDGE

IT KNOWLEDGE
BC

Tuesday, May 20, 2014

Sort Acceding and Descending in Listbox(VB.Net)

This tutorial, we show you how to sort acceding and descending in Listbox using VB.Net. We use one Listbox to get all list of one computer process running and 2 Button use Click_Event which one to sort Acceding and other one is sorted Descending.

Application Design

No Control Type Name Text
1 Listbox ListboxProcess
2 Button1 ButtonAccending Acceding
3 Button2 ButtonDecending Descending


Code

Load data to ListboxProcess
   Private Sub FormSort_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ListBoxProcess.Items.Clear()
        Dim pro As System.Diagnostics.Process
        For Each pro In System.Diagnostics.Process.GetProcesses()
            ListBoxProcess.Items.Add(pro.ProcessName)
        Next
    End Sub

Sort Acceding
Private Sub ButtonAccending_Click(sender As Object, e As EventArgs) Handles ButtonAccending.Click
        ListBoxProcess.Sorted = True
End Sub

Sort Descending
Private Sub ButtonDecending_Click(sender As Object, e As EventArgs) Handles ButtonDecending.Click
        ListBoxProcess.Sorted = True
        ListBoxProcess.Sorted = False
        Dim i As Integer
        Dim count = ListBoxProcess.Items.Count
        For i = ListBoxProcess.Items.Count - 1 To 0 Step -1
            ListBoxProcess.Items.Add(ListBoxProcess.Items(i))
        Next
        For i = 0 To count - 1
            ListBoxProcess.Items.RemoveAt(ListBoxProcess.SelectedItem)
        Next
    End Sub



Whole Code
Public Class FormSort

    Private Sub FormSort_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ListBoxProcess.Items.Clear()
        Dim pro As System.Diagnostics.Process
        For Each pro In System.Diagnostics.Process.GetProcesses()
            ListBoxProcess.Items.Add(pro.ProcessName)
        Next
    End Sub

    Private Sub ButtonAccending_Click(sender As Object, e As EventArgs) Handles ButtonAccending.Click
        ListBoxProcess.Sorted = True
    End Sub

    Private Sub ButtonDecending_Click(sender As Object, e As EventArgs) Handles ButtonDecending.Click
        ListBoxProcess.Sorted = True
        ListBoxProcess.Sorted = False
        Dim i As Integer
        Dim count = ListBoxProcess.Items.Count
        For i = ListBoxProcess.Items.Count - 1 To 0 Step -1
            ListBoxProcess.Items.Add(ListBoxProcess.Items(i))
        Next
        For i = 0 To count - 1
            ListBoxProcess.Items.RemoveAt(ListBoxProcess.SelectedItem)
        Next
    End Sub
End Class








































Please Give Us Your 1 Minute In Sharing This Post!
SOCIALIZE IT →
FOLLOW US →
SHARE IT →
Powered By: itech-9999

0 comments: