Vb.net – Taking A Selected Item In A Listbox and Populate Into A Textbox

listboxlistboxitemsvb.net

Alright, I currently have a listbox being populated with a variety of files.

What I'd like to do is select a file, click the add button and populate the item name into that textbox.

Then, select another item, click the add button and populate that items named into an empty textbox.

I can get the first textbox to populate, but once I select the second item, I can't get empty textbox to display.

Here's my current code on how I'm populating the first textbox. The commented out section was for adding those items into another listbox, which worked but I need to specify a custom order, which I was I thought adding each item to a textbox.

    Private Sub ButtonAdd_Click(sender As System.Object, e As System.EventArgs) Handles ButtonAdd.Click
    'Dim selectedItems = (From i In ListBox1.SelectedItems).ToArray()
    'For Each selectedItem In selectedItems
    'ListBox3.Items.Add(selectedItem)
    'ListBox1.Items.Remove(selectedItem)
    'Next

    TextBox1.Text = ListBox1.SelectedItem

    End Sub

Any suggestions?

Best Answer

Try something like this:

Private Sub ButtonAdd_Click(sender As System.Object, e As System.EventArgs) Handles ButtonAdd.Click
    If string.IsNullOrEmpty(TextBox1.Text) Then 
        TextBox1.Text = ListBox1.SelectedItem
    ElseIf string.IsNullOrEmpty(TextBox2.Text) Then 
        TextBox2.Text = ListBox1.SelectedItem
    ElseIf string.IsNullOrEmpty(TextBox3.Text) Then 
        TextBox3.Text = ListBox1.SelectedItem
    End If
End Sub
Related Topic