A1VBCode Forums

Listbox to textbox


http://www.a1vbcode.com/vbforums/Topic31327.aspx

By luke jackson - 3/26/2012

I'm new to VB and new to this forum. Anyway I am wondering how you would be able to change the text of a textbox with each click of a button. I understand that using an array would probably be best, but for the sake of practice etc. I am trying to do it so that each line of text would be pulled from a list stored in a listbox. I'm assuming listbox.items.count and listbox.items(n) would be used and then you would set n+=1 or something along these lines but I can't get anything to work beyond displaying the first line of the listbox.
By CDRIVE - 4/2/2012

Sure there is but I wouldn't use a CommandButton. Here's a simple example using the ListBox Click() event. Any list item clicked will be put on a new line in the textbox.

' To put each list item on its own textbox line you must first set its Multiline property to
' True at designe time in the Properties window.

Option Explicit
Dim strTxt As String
Dim i As Integer

Private Sub Form_Load()
   strTxt = "Item"
   Text1.Text = ""    ' clear the textbox
   For i = 1 To 10
      List1.AddItem strTxt & " " & i
   Next i
End Sub

Private Sub List1_Click()
   Text1.Text = Text1.Text & List1.Text & vbNewLine
End Sub