populate data from listbox to txtbox

newbeee

Member
Joined
Sep 27, 2007
Messages
19
Programming Experience
Beginner
I have a bunch of txtboxes that need to be fill with data from a listbox (read from a txt file).

Here's a scenario:
1. user click on a txtbox
2. user click on an item in a listbox
3. txtbox populate data from user selected item
4. the selected item is gray out or disappear after the populate to the txtbox

Any suggestion on how I could do this?
Thanks.
 
DragDrop is nice for doing this, for example you have two textboxes and set them to AllowDrop, then add this code:
VB.NET:
Private Sub ListBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListBox1.MouseDown
    If ListBox1.SelectedItem IsNot Nothing Then
        If ListBox1.DoDragDrop(ListBox1.SelectedItem, DragDropEffects.Move) = DragDropEffects.Move Then
            ListBox1.Items.Remove(ListBox1.SelectedItem)
        End If
    End If
End Sub

Private Sub TextBox1_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) _
Handles TextBox1.DragEnter, TextBox2.DragEnter
    If e.Data.GetDataPresent(DataFormats.StringFormat) Then
        e.Effect = DragDropEffects.Move
    End If
End Sub

Private Sub TextBox1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) _
Handles TextBox1.DragDrop, TextBox2.DragDrop
    Dim tb As TextBox = sender
    tb.Text = e.Data.GetData(DataFormats.StringFormat)
End Sub
Left-Mousedown items and drag them to their target textboxes.
 
JohnH:

Thank you for your quick insight. However, it looks like you're using a drag & drop control here. Is there any click control toward that?

All of my txtboxes are display in a tab container (3 tabs, a total of approx. 60+ txtboxes). It's going to be a pain if I have to write all that for every single txtbox?

I'm very new to .NET so bare with me.

Thanks for all your help.
 
However, it looks like you're using a drag & drop control here.
No, I'm using one Listbox and two Textboxes. All controls have D&D support.
VB.NET:
Is there any click control toward that?
If you mean, click a button and D&D is performed, no.
VB.NET:
All of my txtboxes are display in a tab container (3 tabs, a total of approx. 
60+ txtboxes). It's going to be a pain if I have to write all that for every single txtbox?
You don't have to write anything. Just copy-paste the code I posted. Then Select all Textboxes, then in properties windows switch to Events view, find the appropriate event and just select the existing handler method to handle it.
 
Back
Top