Results 1 to 2 of 2

Thread: Adding text between listboxes items

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2015
    Posts
    3

    Adding text between listboxes items

    I'm using vb.net form.

    I have 8 listboxes (listbox1 , listbox2,listbox3..) each one contains many items and another empty listbox (listbox10). I'm trying to add first listboxes items into the listbox10

    ex: listbox1 first items is "A" , listbox2 first items is "B" , listbox3 first items is "C"...etc . Now listbox10 first item must be "Abc...";

    The problem is i want to add text ( string or number) between each listbox

    ex: listbox1 first items is "A" , listbox2 first items is "B" , listbox3 first items is "C"...etc . Now I want listbox10 first item to be "The letters A is before B and now C"

    The main idea is to combine all listboxes items and adding text between them to create a meaningful sentence

    here is my code

    Dim controls = New List(Of ListBox)() From _
    {ListBox1, ListBox2, ListBox3, ListBox4, ListBox5, ListBox6, ListBox7, ListBox8}
    Dim minCount = Controls.Min(Function(x) x.Items.Count)
    For x = 1 To minCount - 1
    ListBox10.Items.Add(String.Join(" ", controls.Select
    (Function(lb) lb.Items(x).ToString)))
    Next

    End Sub

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

    Re: Adding text between listboxes items

    This question really has nothing at all to do with ListBoxes. It boils down to the fact that you have a number of Strings and you want to build another String from them and some literal text. That is an example of how you identify what the actual problem is and not make it harder to solve by including irrelevant factors. It's for that sort of thing that the String.Format method exists. As a simple example:
    vb.net Code:
    1. Dim text = String.Format("The name is {0}.  {1} {0}.",
    2.                          familyNameTextBox.Text,
    3.                          givenNameTextBox.Text)
    If the user were to type "James" into the given name field and "Bond" into the family name field then `text` would contain "The name is Bond. James Bond.". As you can see, it's simple to insert whatever values you want into literal text wherever you want.
    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

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