[VB.net] Populate textboxes from listview selected items
Hi All.
I have a Form with listview of customer name. e.g. AAA, BBB, CCC, DDD, EEE. etc And 3 textboxes.
I want to be able to populate the textboxes when the user selects a customer. For example: If User selects AAA, textbox1.text = AAA, then if User selects CCC, textbox2.text = CCC etc.
I tired: (in a button on click event)
TextBox1.Text = Listview1.SelectedItems(0).Text
TextBox2.Text = Listview1.SelectedItems(1).Text
TextBox3.Text = Listview1.SelectedItems(2).Text
But this only works if the user selects 'all' 3 customers at once. I want to allow the User to populate each textboxes depending on how many they select eg. 'Upto' 3 customers .
Any ideas how i can achieve this?
Many Thanks in Advance.
Re: [VB.net] Populate textboxes from listview selected items
The way I would go about doing this would be to have a counter which starts at 1.
Then in your button click event you would check what the counter was if its 1 then you would put the current selecteditem text into textbox1, if the counter was 2 then into textbox2 and I think you can guess where if the counter is 3 :)
So your code might look similar to this
Code:
Public Class Form1
Private counter As Integer
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If ListView1.SelectedItems.Count = 1 Then
Select Case counter
Case 1
TextBox1.Text = ListView1.SelectedItems(0).Text
Case 2
TextBox2.Text = ListView1.SelectedItems(0).Text
Case 3
TextBox3.Text = ListView1.SelectedItems(0).Text
End Select
counter += 1
'This bit of code is so that you loop round and start at 1 again
If counter > 3 Then
counter = 1
End If
Else
MsgBox("You must select an item in order to continue")
End If
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
counter = 1
End Sub
End Class
I've gone with the idea that you probably only want one thing selected at once, so you would probably want the listviews MultiSelect property set to false for this.
Anyway I hope that helps you
Satal :D
Re: [VB.net] Populate textboxes from listview selected items
Hi Satal
Thanks for the help! Great concept and very clean, much better than the way i thought of doing it. :bigyello:
esc
Re: [VB.net] Populate textboxes from listview selected items
No worries always happy to help :D