一道简单的VB编程题目

来源:百度知道 编辑:UC知道 时间:2024/06/18 06:17:48
窗体中有一个组合框和一个命令按钮。程序的功能是在运行时,如果在组合框中输入一个项目并单击命令按钮,则搜索组合框中的项目,如果没有此项,则
把此项添加到列表中;如果有此项,则弹出提示:“已有此项”,然后清除输入的内容。

Private Sub command1_click()

If Combo1.Text <> "" Then
ats = 0
For i = 0 To Combo1.ListCount - 1
If Combo1.List(i) = Combo1.Text Then
ats = 1
Exit For
End If
Next i
If ats = 0 Then
Combo1.AddItem Combo1.Text
Combo1.SelStart = 0
Combo1.SelLength = Len(Combo1.Text)
MsgBox "项目已添加"
Else
MsgBox "已在列表中"
End If
End If
End Sub

Private Sub Command1_Click()
Dim i As Integer

If Trim(Combo1.Text) = "" Then
MsgBox "不能添加空项!"
Exit Sub
End If

For i = 0 To Combo1.ListCount - 1
If Trim(Combo1.Text) = Combo1.List(i) Then
MsgBox "该项已存在!"
Exit Sub
End If
Next i

Combo1.AddItem Trim(Combo1.Text)
MsgBox "添加项成功!"
End Sub