user3387807
user3387807

Reputation: 7

VB: Transfering all text from one listbox to another

Thanks for reading guys first of all. Secondly I'd like button1 to transfer all text from listbox1 to listbox2. So when button1 is clicked, all items in listbox1 transfers to listbox2. Here is what I have, thanks again:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button4.Click

   ListBox2.Items.Add(ListBox1.SelectedItems)

End Sub

Using VB2012

Upvotes: 1

Views: 13317

Answers (3)

slek
slek

Reputation: 309

try this:

    ListBox2.Items.Clear()  //This is to avoid duplication if button was clicked more than once

     For i = 0 To ListBox1.Items.Count - 1
        ListBox2.Items.Add(ListBox1.Items(i).ToString)
    Next

Upvotes: 1

Victor Zakharov
Victor Zakharov

Reputation: 26424

To copy all items from one listbox to another (ListBox1 -> ListBox2):

ListBox2.Items.AddRange(ListBox1.Items)

Upvotes: 2

Edper
Edper

Reputation: 9322

You traverse on the 1st Listbox selected items and add each item in your 2nd ListBox, like

For Each Item As String In ListBox1.SelectedItems
  ListBox2.Items.Add(Item)
Next

Upvotes: 0

Related Questions