文本框列表框

来源:百度知道 编辑:UC知道 时间:2024/05/22 17:38:54
)请在窗体上画两个按钮(名称分别为Option1和Option2,标题分别为“添加项目”和“删除项目”),一个列表框(名称为List1)和一个文本框(名称为Text1),如图1所示。编写窗体的Click事件过程。程序运行后,如果选择“添加项目”单选按钮,然后单击窗体,则从键盘上输入要添加的项目(内容任意,不少于三个),并添加到列表框中;如果选择“删除项目”单选按钮,则从键盘上输入要删除的项目,将其从列表框中删除。程序的运行情况如图2所示。

按照要求放好控件,然后写代码如下:

Private Sub Form_Activate()
Option1.Caption = "添加项目"
Option2.Caption = "删除项目"
Text1.Text = "请在此处输入内容,然后单击窗体,添加到列表框中"
Option1.Value = True
Option2.Value = False
List1.Clear
End Sub

Private Sub Form_Click()
If Option1.Value Then
If Trim(Text1.Text) <> "" Then
List1.AddItem Text1.Text
End If
End If

If Option2.Value Then
If List1.ListCount < 3 Then
MsgBox "列表框添加的内容不足三个,请继续添加!"
Option1.Value = True
Exit Sub
End If
For i = 0 To List1.ListCount - 1
List1.Refresh
If List1.List(i) = Text1.Text Then List1.RemoveItem i
Next
End If
Text1.SetFocus
End Sub

Private Sub List1_Click()
For i = 0 To List1.ListCount - 1
Print List1.List(i)
Next
End Sub

Private Sub Option1_Click()
Text1.Text = "请在此处输入内容,然后单击窗体,添加到列表框中"