两个ListBox,分别是左边的ListBox1和右边的ListBox2。
点击全部添加按纽后(第二个),左边的项与右边的项进行一个对比。如果右边ListBox里面已经含有这项了,则不添加,提示项已经存在了;如果右边的ListBox里面没有这项,则在它原有的项后面插入。j = List2.ListCount
For i = 0 To List1.ListCount - 1 Step 1
List2.AddItem List1.List(i), j
j = j + 1
Next i

解决方案 »

  1.   


    for i = 0 to list1.listcount - 1
    for j = 0 to list2.listcount - 1
    if list2(j) = list(1) then
    msgbox "重复!"
    exit for
    else
    list2.additme list1.list(i)
    endif
    next
    next
      

  2.   

    SendMessage发送 lb_findstring消息
      

  3.   

    1、下面这个是你在List1中所选择的就添加:dim i as Long,j as long       
    For i=0 to List1.ListCount-1               
        if List1.Selected(i)=True then '凡是选中的就添加(不能重复)               
            for j=0 to List2.ListCount-1   
               if trim(List2.List(j))<>trim(List1.List(i)) then                                       
                  List2.AddItem List1.List(i)               
                  List2.refresh   
               endif       
            next       
        endif                               
    next               2、下面这个是你选择全部添加List1中的项(也就是你点击全部添加按纽):dim i as Long,j as long       
    For i=0 to List1.ListCount-1               
         for j=0 to List2.ListCount-1   
            if trim(List2.List(j))<>trim(List1.List(i)) then                                       
               List2.AddItem List1.List(i)               
               List2.refresh
            else
               debug.print "第" & trim(str(i))& "项已经存在!"  
            endif       
         next                          
    next        
      

  4.   

    那还不如直接把右边的ListBox里面的内容全部删除,再重新添加~
      

  5.   

    首先针对一楼楼主的回复提出看法。此方法相当于冒泡,会重复添加数据。
    假如数据是以下:
    listbox1      listbox2
      1             3
      2             4
      3             5
    用二楼的算法操作以后,会添加3个1,6个2,所以肯定是不正确的。^_^另外,你的list2(j) = list(1)应该写成 List2.List(j) = List1.List(i)吧?虽然知道你的本意是对的,但咱们写代码的,就应该严谨一点,不是吗?
      

  6.   

    4楼的方法也是不可取的!如果数据较多,而ListBox2与ListBox1的不同的Item又比较多,你删除后如果将ListBox2原来的项恢复呢?总不能找张纸抄下来吧?^_^
      

  7.   

    3楼楼主你很热心,写得也很详细,但是你的方法跟2楼的方法实际上是同出一辙的。
    不信,你用
    listbox1                     listbox2 
        1                           3 
        2                           4 
        3                           5试一下,出来3个1,6个2,11个3。看来这个问题真的值得我们深究!
      

  8.   

    双for太慢了,好多年没有来CSDN了,现在的人变少咯~
    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Integer, ByVal lParam As Any) As Long
    Private Const LB_FINDSTRING = &H18FPrivate Sub Command1_Click()
        Dim I As Long
        For I = 0 To List1.ListCount - 1
            If SendMessage(List2.hwnd, LB_FINDSTRING, -1, ByVal CStr(List1.List(I))) <= 0 Then
                List2.AddItem List1.List(I)
            End If
        Next I
    End Sub