vb中文本框可以输出的内容如何在列表框里输出?

2024-11-25 19:55:59
推荐回答(2个)
回答1:

以下程序实现:在文本框输入内容,按回车键(Enter键)后,文本框中的内容输出到列表框。

1)在窗体上布置一个TextBox控件和一个ListBox控件

2)代码

Option Explicit

Private Sub Form_Load()
    ' 窗体启动时,清空Text1和List1
    Text1.Text = ""
    List1.Clear
End Sub

Private Sub Text1_KeyPress(KeyAscii As Integer)
    ' 如果按下了回车键(回车键的Ascii码为13)
    If KeyAscii = 13 Then
        Dim s As String
        ' 取出Text1中输入的内容并滤除前后的空格
        s = Trim(Text1.Text)
        
        If s <> "" Then
            ' 如果Text1中输入的内容不为空,则添加到List1中
            List1.AddItem s
            ' 添加后,清空Text1
            Text1.Text = ""
        End If
    End If
End Sub

3)运行效果

窗体启动时

在Text1中输入,按回车后,输入的内容在List1中显示

回答2:

添加一个listbox,名称为list1

代码:

Private Sub Command1_Click()
Dim a0() As Integer, a1() As Integer, x%, y$
y = InputBox("请输入a0数组元素个数", "输入数据", "5")
If Val(y) = 0 Or Val(y) = 1 Then Exit Sub
ReDim a0(1 To Val(y))
For x = 1 To UBound(a0)
y = InputBox("请输入a0数组第" & x & "个元素", "输入数据", "1")
If Val(y) = 0 Then Exit Sub
a0(x) = Val(y)
Next
y = InputBox("请输入a1数组元素个数", "输入数据", "4")
If Val(y) = 0 Or Val(y) = 1 Then Exit Sub
ReDim a1(1 To Val(y))
For x = 1 To UBound(a1)
y = InputBox("请输入a1数组第" & x & "个元素", "输入数据", "2")
If Val(y) = 0 Then Exit Sub
a1(x) = Val(y)
Next
'Text1.Text = Px(a0(), a1())
Call Pxlist(a0(), a1(), List1)
End Sub

Private Sub Pxlist(aa%(), bb%(), l As ListBox)
Dim i&, a&, b&, c&, d&, x&, y&, m&, n&, z&, t&(4), s$(4)
For a = 1 To UBound(aa) - 2
For b = a + 1 To UBound(aa) - 1
For c = b + 1 To UBound(aa)
For x = 1 To UBound(bb) - 1
For y = x + 1 To UBound(bb)
i = i + 1 '行序列号
t(0) = aa(a): t(1) = aa(b): t(2) = aa(c): t(3) = bb(x): t(4) = bb(y)
For m = 0 To 4
For n = m + 1 To 4
If t(n) < t(m) Then z = t(m): t(m) = t(n): t(n) = z
Next
s(m) = Format(t(m), "00")
Next
l.AddItem Format(i, "@@@@ ") & Join(s)
Next y, x, c, b, a
Label1.Caption = "一共有" & Str(i) & "种组合"
End Sub