分享

VB 如何把一个listbox作为参数传递

 hdzgx 2017-10-22

方法一:

  1. ListBox2.Items.Clear()  
  2. For i = 0 To ListBox1.Items.Count - 1  
  3.     ListBox2.Items.Add(ListBox1.Items(i))  
  4. Next  

这样就将ListBox1的列表项移动到另一个ListBox2中了

这是利用For循环;提取第一个列表框ListBox1中的所有列表项,再全部加载到另一个listbox2


方法二:更好,但是还没完全看懂,网上搜了一下,都是有关VB.NET的编程


  1. Public Class Form1  
  2.     'Listbox之间项目拖动示例,左键移动,右键复制  
  3.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load  
  4.         ListBox2.AllowDrop = True  
  5.     End Sub  
  6.   
  7.     Private Sub ListBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListBox1.MouseDown  
  8.   
  9.         Dim DragIndex = ListBox1.IndexFromPoint(e.X, e.Y)  
  10.         If DragIndex <> ListBox.NoMatches Then  
  11.             ListBox1.SelectedIndex = DragIndex  
  12.             If e.Button = Windows.Forms.MouseButtons.Left Then  
  13.                 DoDragDrop(ListBox1.Items(DragIndex), DragDropEffects.Move)  
  14.             ElseIf e.Button = Windows.Forms.MouseButtons.Right Then  
  15.                 DoDragDrop(ListBox1.Items(DragIndex), DragDropEffects.Copy)  
  16.             End If  
  17.         End If  
  18.     End Sub  
  19.   
  20.     Private Sub ListBox2_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles ListBox2.DragEnter  
  21.         e.Effect = e.AllowedEffect  
  22.     End Sub  
  23.   
  24.     Private Sub ListBox2_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles ListBox2.DragDrop  
  25.         Dim item As Object = CType(e.Data.GetData(GetType(System.String)), System.Object)  
  26.         Dim item2 As Integer = ListBox2.IndexFromPoint(ListBox2.PointToClient(New Point(e.X, e.Y)))  
  27.         If item2 = -1 Then  
  28.             ListBox2.Items.Add(item)  
  29.         Else  
  30.             ListBox2.Items.Insert(item2, item)  
  31.         End If  
  32.         If e.AllowedEffect = DragDropEffects.Move Then  
  33.             ListBox1.Items.Remove(item)  
  34.         End If  
  35.     End Sub  
  36. End Class  


    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多