vb编程题
2编写程序,要求用户输入下列信息,姓名年龄通信地址邮政编码,电话,然后将输入的数据用适当的格式在窗体上显示出来 展开
1、
Dim s As Integer
Private Sub Command1_Click()
On Error Resume Next
Dim x(1 To 4) As Integer
For n = 1 To 4
x(n) = InputBox("请输入第" & n & "个数:")
Print x(n);
s = s + x(n)
Next n
End Sub
Private Sub Command2_Click()
Print s / 4
End Sub
2、
Option Explicit
Dim fileName As String
Private Sub Command1_Click()
'将文本框中的信息添加到列表框中
Dim i As Integer
With ListView1.ListItems.Add()
.Text = Text1(0)
For i = 1 To 4
.SubItems(i) = Text1(i)
Next i
End With
End Sub
Private Sub Command2_Click()
'删除列表框中信息
If ListView1.ListItems.Count > 0 Then
If MsgBox("真的要删除吗?", vbQuestion + vbYesNo + vbDefaultButton2) = vbYes Then
ListView1.ListItems.Remove ListView1.SelectedItem.Index
End If
End If
End Sub
Private Sub Form_Load()
Dim tpStr As String, i As Integer, j As Integer
Me.Caption = "通讯录"
For j = 0 To 4
Text1(j).Text = ""
Next j
fileName = App.Path & "\data.txt"
If Dir(fileName) <> "" Then
'加载数据
Open fileName For Input As #1
Do While Not EOF(1)
With ListView1.ListItems.Add()
For i = 0 To 4
Line Input #1, tpStr
If i = 0 Then
.Text = tpStr
Else
.SubItems(i) = tpStr
End If
Next i
End With
Loop
Close #1
End If
End Sub
Private Sub Form_Unload(Cancel As Integer)
Dim i As Integer
Dim tpList As ListItem
'保存数据
Open fileName For Output As #1
For Each tpList In ListView1.ListItems
Print #1, tpList.Text
For i = 1 To 4
Print #1, tpList.SubItems(i)
Next i
Next tpList
Close #1
End Sub
参考资料: 哈工大VB课件