VB6列表框复制

时间:2014-12-17 19:34:15

标签: winforms vb6 listbox-control

使用Visual Basic 6.0,我试图用另一个列表框的项目填充一个列表框。

任何人都可以解释为什么以下子项无法正常工作, 在将两个列表框作为参数传递时获取类型不匹配。

Public Sub copyListboxes(Byref lstFrom as Listbox, Byref lstTo as Listbox)
    Dim i as integer
    For i = 0 to lstFrom.Listcount -1
        lstTo.Additem lstFrom.List(i)
    Next i
End Sub

'Call copyListboxes(Listbox1,Listbox2) 'Type Mismatch ... ?

提前致谢。

1 个答案:

答案 0 :(得分:2)

我能够通过使用Combox而不是Listbox来重现您的错误:

'1 form with:
'  1 command button: name=Command1
'  2 combobox controls: name=Listbox1 name=Listbox2
Option Explicit

Private Sub Command1_Click()
  Call copyListboxes(Listbox1, Listbox2) 'Type Mismatch!
End Sub

Public Sub copyListboxes(ByRef lstFrom As ListBox, ByRef lstTo As ListBox)
  Dim i As Integer
  For i = 0 To lstFrom.ListCount - 1
      lstTo.AddItem lstFrom.List(i)
  Next i
End Sub

Private Sub Form_Load()
  Dim intIndex As Integer
  For intIndex = 1 To 10
    Listbox1.AddItem CStr(intIndex)
  Next intIndex
End Sub

要解决此问题,您可以使用列表框控件或更改子组件以使用Combobox控件:

Public Sub copyListboxes(ByRef lstFrom As ComboBox, ByRef lstTo As ComboBox)
  Dim i As Integer
  For i = 0 To lstFrom.ListCount - 1
      lstTo.AddItem lstFrom.List(i)
  Next i
End Sub