Results 1 to 10 of 10

Thread: [HELP] need a code to organize a listbox or a list

  1. #1

    Thread Starter
    Lively Member
    Join Date
    May 2017
    Posts
    96

    Question [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.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,370

    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
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  4. #4

    Thread Starter
    Lively Member
    Join Date
    May 2017
    Posts
    96

    Resolved Re: [HELP] need a code to organize a listbox or a list

    Quote Originally Posted by dday9 View Post
    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?
    Last edited by danzey; Jun 8th, 2020 at 11:45 AM.

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,102

    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.
    My usual boring signature: Nothing

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    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)

  7. #7
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    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

  8. #8

    Thread Starter
    Lively Member
    Join Date
    May 2017
    Posts
    96

    Re: [HELP] need a code to organize a listbox or a list

    Quote Originally Posted by .paul. View Post
    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.
    Last edited by danzey; Jun 8th, 2020 at 12:53 PM.

  9. #9
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: [HELP] need a code to organize a listbox or a list

    Quote Originally Posted by danzey View Post
    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

  10. #10
    Lively Member
    Join Date
    Jan 2020
    Posts
    119

    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

Tags for this Thread

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