[VB.NET] Chuyển ListBox này sang ListBox kia

Liên hệ QC

Thanh Bình PV

Thành viên hoạt động
Tham gia
30/10/19
Bài viết
151
Được thích
19
Em chào mọi người,
EM có 1 Form có 2 ListBox. Khi Form load sẽ lấy những đối tượng đang hiện vào ListBox1 và đang ẩn vào ListBox2.

Em muốn khi chọn nhiều đối tượng bên ListBox1 và nhấn Button1 thì sẽ ẩn những đối tượng trong ListBox1 và chuyển sang ListBox2. Nhưng hiện tại em chưa tìm được cách lấy những đối tượng được chọn trong ListBox1.

Em đã thử trong VB.Net sử dụng các code ở bên dưới nhưng vẫn không được.
Mong anh chị giúp em với ạ. Em cảm ơn.
Mã:
For i = 0 To ListBox1.Items.Count - 1
                If ListBox1.GetSelected(i) = True Then
                    Dim k As String = ""
                    k = ListBox1.Items(i).ToString
                    For j = 1 To oComponent.SurfaceBodies.Count
                        oBody = oComponent.SurfaceBodies.Item(j)
                        If oBody.name = k Then
                            oBody.visible = False
                            ListBox2.Items.Add(k)
                            ListBox1.Items.Remove(k)
                        End If
                    Next j
                End If
            Next
Or
Mã:
 For i = 0 To ListBox1.Items.Count - 1
                If ListBox1.SelectedItems(i) IsNot Nothing Then
                    Dim k As String = ""
                    k = ListBox1.Items(i).ToString
                    For j = 1 To oComponent.SurfaceBodies.Count
                        oBody = oComponent.SurfaceBodies.Item(j)
                        If oBody.name = k Then
                            oBody.visible = False
                            ListBox2.Items.Add(k)
                            ListBox1.Items.Remove(k)
                        End If
                    Next j
                End If
            Next
Và cuối cùng
Mã:
Try
                For i = 0 To ListBox1.Items.Count - 1
                    Dim idx As Integer = ListBox1.SelectedIndices(i)
                    Dim k As String = ""
                    k = ListBox1.Items(i).ToString
                    For j = 1 To oComponent.SurfaceBodies.Count
                        oBody = oComponent.SurfaceBodies.Item(j)
                        If oBody.name = k Then
                            oBody.visible = False
                            ListBox2.Items.Add(k)
                            ListBox1.Items.Remove(k)
                        End If
                    Next j
                Next
            Catch ex As Exception
            End Try
 
Bạn cho cái hình nhìn xíu đọc mệt quá hiiii
 
Bạn thử cái này xem
For x = 0 To ListBox1.SelectedItems.Count() - 1 'dem item dc chon
If ListBox1.SelectedItems.Count() > 0 Then
' MsgBox(ListBox1.SelectedItems(x)) lay ten item dc chon
'paste item duoc chon sang lb2
ListBox2.Items.Add(ListBox1.SelectedItems(x))
End If
Next x
For y = 0 To ListBox1.SelectedItems.Count() - 1 'dem item dc chon
If ListBox1.SelectedItems.Count() > 0 Then
ListBox1.Items.RemoveAt(ListBox1.SelectedIndex) 'xoa item duoc chon
End If
Next y
 
Bạn thử cái này xem
For x = 0 To ListBox1.SelectedItems.Count() - 1 'dem item dc chon
If ListBox1.SelectedItems.Count() > 0 Then
' MsgBox(ListBox1.SelectedItems(x)) lay ten item dc chon
'paste item duoc chon sang lb2
ListBox2.Items.Add(ListBox1.SelectedItems(x))
End If
Next x
For y = 0 To ListBox1.SelectedItems.Count() - 1 'dem item dc chon
If ListBox1.SelectedItems.Count() > 0 Then
ListBox1.Items.RemoveAt(ListBox1.SelectedIndex) 'xoa item duoc chon
End If
Next y
Em cảm ơn ạ. Em đã thử và chạy tốt ạ.
 
Web KT
Back
Top Bottom