display sorted array

  • Thread starter Thread starter Murt
  • Start date Start date
M

Murt

if i perform a sort on an array, how do i get the sorted array to be
displayed in a listbox?

thanks

murt
 
* Murt said:
if i perform a sort on an array, how do i get the sorted array to be
displayed in a listbox?

Sample for binding an arraylist to a listbox:

\\\
Private Sub Test()
FillListBox("F1#Function1|F2#Function3", Me.ListBox1)
End Sub

Private Function FillListBox( _
ByVal Data As String, _
ByVal List As ListBox _
)
Dim s, t() As String, x As Item
Dim al As New ArrayList()
For Each s In Strings.Split(Data, "|")
x = New Item()
t = Strings.Split(s, "#")
x.TheText = t(1)
x.TheValue = t(0)
al.Add(x)
Next s
List.DataSource = al
List.DisplayMember = "TheText"
List.ValueMember = "TheValue"
End Function

Private Class Item
Private m_TheText As String
Private m_TheValue As String

Public Property TheText() As String
Get
Return m_TheText
End Get
Set(ByVal Value As String)
m_TheText = Value
End Set
End Property

Public Property TheValue() As String
Get
Return m_TheValue
End Get
Set(ByVal Value As String)
m_TheValue = Value
End Set
End Property
End Class
///
 
Try the following code snippet.

BTW, a listbox has a sorted property; if your sorting is alphabetical, why
not just get the listbox sort itself. This is also shown below.

To use:

Create a new winforms application, pop a button on the form (called
Button1), pop a listbox on the form (called Listbox1), and paste in the
following.

Regards
Hexathioorthooxalate



Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Dim stringArray() As String = "apple bear aardvark zebra lion".Split("
".ToCharArray())
ListBox1.Items.Clear()
ListBox1.Sorted = False
For i As Integer = 0 To stringArray.Length - 1
ListBox1.Items.Add(stringArray(i))
Next
MsgBox("Press ok to sort")
ListBox1.Sorted = True
End Sub
 
Back
Top