如给定一个文件data.dat,内容如下:
a 1
a 2
a 5
b 70
b 1
b 6
b 2
点击下拉列表甲,有选项a和b,
选中b,
则乙下拉列表中的项变为70,1,6,2四项
缺省:甲列表已选中a,乙列表已选中1
combobox1,combobox2
private sub combobox1_click()
if combobox1.text="a" then
combobox2.clear
combobox2.additem "1"
combobox2.additem "2"
combobox2.additem "5"
end if
if combobox1.text="b" then
combobox2.clear
combobox2.additem "70"
combobox2.additem "1"
combobox2.additem "6"
combobox2.additem "2"
end if
end sub
首先引用microsoft scripting runtime
Option Explicit
Dim ArrayX() As String, i As Integer 模块级变量
Private Sub Combo1_Click()
Dim j As Integer
With Combo2
Select Case Combo1.Text
Case "a"
.Clear
For j = 1 To i
If ArrayX(1, j) = "a" Then
.AddItem ArrayX(2, j)
End If
Next j
Case "b"
.Clear
For j = 1 To i
If ArrayX(1, j) = "b" Then
.AddItem ArrayX(2, j)
End If
Next j
End Select
End With
End Sub
Private Sub Form_Load()
Dim fs As New FileSystemObject, a, retstring, txtname
txtname = "c:\data.dat"
Set fs = CreateObject("Scripting.FileSystemObject")
Set a = fs.OpenTextFile(txtname, ForReading, False)
input data into AarrayX
i = 0
Do While a.AtEndOfLine <> True
retstring = a.ReadLine
i = i + 1
ReDim Preserve ArrayX(1 To 2, 1 To i) As String
ArrayX(1, i) = Left(retstring, 1)
ArrayX(2, i) = Trim(Right(retstring, 2))
Loop
a.Close
Combo1.AddItem "a"
Combo1.AddItem "b"
Combo1.Text = "a"
Combo2.Text = "1"
End Sub
up