VB列表框的问题

来源:百度知道 编辑:UC知道 时间:2024/09/24 07:25:39
Private Sub Form_Load()
Text1.Text = "": List1.Clear
End Sub
Private Sub Text1_KeyPress(K As Integer)
Dim n As Integer
' If K >= Asc("0") And K <= Asc("9") Then K = 0
If K = 13 Then
List1.AddItem Text1.Text, n
n = n + 1
Text1.Text = ""
End If
End Sub

我如果分别输入1,2,3,为什么显示的是3
2
1
里面的n=n+1用处,请说得详细点

这里的n=n+1什么用都没有
多余的

为什么显示是321呢?是因为List1.AddItem Text1.Text, n
就这句就行了
你每点一次回车
n都定义一次
又没赋值,所以都是默认值0
而List1.AddItem Text1.Text, n
n是指你把text1.text加到了list1的第几行(从0开始算的)

没有问题呀

n = n + 1 没有用!N的作用是用来指定新项目或行在该对象中的位置,因为每次开始都对n进行重新内存分配,n的初始值为0 则为list1的第一项(项目号为0)进行赋值,其他项下移。