vb数组赋值问题

2024-11-09 10:53:17
推荐回答(2个)
回答1:

Private
Sub
Form_Load()
Dim
a(10)
As
Integer,
n
As
Integer,
i
As
Integer
n
=
InputBox("请输入n的值(n>=2且n<=10)")
For
i
=
1
To
n
a(i)
=
InputBox("请按顺序输入数组中各元素的值")
Picture1.Print
"a("
&
i
&
")=";
a(i)
Next
i
End
Sub
1)你代码稍作修改可以输入数组中各各元素的值.
2)当然也可在窗体初始化时给数组赋值:
Private
Sub
Form_Load()
a(0)
=
1
a(0)
=
11
a(0)
=
27
a(0)
=
15
a(0)
=
136
a(0)
=
44
End
Sub
3)在窗体初始化时读取文本文件(或数据库)给数组赋值:
Private
Sub
Form_Load()
Dim
As
Byte
Dim
gongyi(31)
as
Single
Open
"c:\gongyi.txt"
For
Input
As
#1
For
i
=
0
To
31
Input
#1,
gongyi_sj(i)
Next
i
Close
End
Sub

回答2:

因为在数组初始化中不能直接定义数组的维数,故你错在Dim
label1_top(0
To
9)
As
Long
Dim
label1_left(0
To
9)
As
Long
指明0-9,去掉(0-9),还有你一个数据中有个b,也不对(细心阿)这样就ok了
Option
Explicit
Dim
i
Private
Sub
Form_Click()
Dim
label1_top
Dim
label1_left
label1_left
=
Array(120,
3000,
4200,
960,
2520,
4080,
960,
2520,
4080,
5760)
label1_top
=
Array(720,
1200,
1200,
2160,
2160,
2160,
3000,
3000,
3000,
1680)
For
i
=
0
To
9
Print
label1_top(i)
Next
i
End
Sub
我运行了一个,可以
另一个留给你了