[HELP] need a code to organize a listbox or a list
hello again! i have a litte demand : i have a list like this wiht number:
200 jack
140 nina
120 lisa
110 karl
100 raul
etc...
and i would like this result with a code
jack 200
nina 140
lisa 120
karl 110
raul 100
etc...
my data are in a listbox .note: i am a beginner in coding.
Re: [HELP] need a code to organize a listbox or a list
Maybe you should look at how you are creating that list in the first place and change that to put the number name first instead of the number first. Maybe we could help you do that if you'd bothered to provide that code.
Re: [HELP] need a code to organize a listbox or a list
Assuming that your list item type is a String, then you'd need to split on space and then join the String back in reverse order. Something along the lines of:
Code:
collection = collection.Select(Function(item)
Dim parts() As String = item.Split(" ")
Return String.Join(" ", parts.Reverse())
End Function).ToList()
Fiddle: Live Demo
Re: [HELP] need a code to organize a listbox or a list
Quote:
Originally Posted by
dday9
Assuming that your list item type is a String, then you'd need to split on space and then join the String back in reverse order. Something along the lines of:
Code:
collection = collection.Select(Function(item)
Dim parts() As String = item.Split(" ")
Return String.Join(" ", parts.Reverse())
End Function).ToList()
Fiddle:
Live Demo
Thank you infinitely! that's it! i am looking for! thank you 1000x ! this forum and you are the best!
but my list is in a listbox what is the code in my case?
Re: [HELP] need a code to organize a listbox or a list
Your list can't START in a listbox. You can't type into a listbox, so the data has to have gotten there by some means. Make your changes on that source of the data rather than on the listbox.
Re: [HELP] need a code to organize a listbox or a list
Try this...
Code:
Dim newItems() As String = ListBox1.Items.Cast(Of String).Select(Function(s) s.Split(" "c).Last & " " & s.Split(" "c).First).ToArray
ListBox1.Items.Clear()
ListBox1.Items.AddRange(newItems)
Re: [HELP] need a code to organize a listbox or a list
But... The way to do it is not to add the wrongly formatted data in the listbox, then reformat it. The way to do it is to modify your collection before adding the items to your listbox
Re: [HELP] need a code to organize a listbox or a list
Quote:
Originally Posted by
.paul.
Try this...
Code:
Dim newItems() As String = ListBox1.Items.Cast(Of String).Select(Function(s) s.Split(" "c).Last & " " & s.Split(" "c).First).ToArray
ListBox1.Items.Clear()
ListBox1.Items.AddRange(newItems)
beautiful!!!!!! it works fine for me! big thank ! +1000 love have a nice day for all of you.
Re: [HELP] need a code to organize a listbox or a list
Quote:
Originally Posted by
danzey
beautiful!!!!!! it works fine for me! big thank ! +1000 love have a nice day for all of you.
It's a hack... Not even a clever one :D
Re: [HELP] need a code to organize a listbox or a list
HTML Code:
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' Set the caption bar text of the form.
Me.Text = "Hello Danzey"
ListBox1.Items.Add("200")
ListBox1.Items.Add("140")
ListBox1.Items.Add("120")
ListBox2.Items.Add("jack")
ListBox2.Items.Add("nina")
ListBox2.Items.Add("lisa")
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
MsgBox(ListBox2.SelectedItem.ToString() + ListBox1.SelectedItem.ToString())
End Sub
End Class