一道简单的VB编程题

来源:百度知道 编辑:UC知道 时间:2024/05/26 11:53:23
设计一个程序,用户界面如下图所示,要求:
(1)在组合框中输入内容后,单击“添加”按钮,如果组合框中设有该内容,则将输入内容加入到列表中,否则将不添加,另外要求组合框中内容能自动按字母排序。
(2)在列表中选择某一选项后,单击“删除”按钮,则删除该项。
(3)单击“清除”按钮,将清除列表中的所有内容。

要求写出程序代码

添加一个TextBox(Text1),一个ListBox(List1),三个按钮(Command1、Command2、Command3)
设置List1的Sorted属性为True

Private Sub Command1_Click()
For i = 0 To List1.ListCount - 1
If Text1.Text = List1.List(i) Then
Exit For
ElseIf i = List1.ListCount - 1 And List1.List(i) <> Text1.Text Then
List1.AddItem Text1.Text
End If
Next
Text1.Text = ""
End Sub

Private Sub Command2_Click()
List1.RemoveItem (List1.ListIndex)
End Sub

Private Sub Command3_Click()
List1.Clear
Text1.Text = ""
End Sub

Private Sub Form_Load()
With List1
.AddItem "北京"
.AddItem "上海"
.AddItem "济南"
.AddItem "广州"
.AddItem "石家庄"
End With
Text1.Text = ""
Command1.Caption = "添加"
Command2.Caption = "删除"
Command3.Caption = "清除"
End Sub

楼上的比我快啊
'这是你