把以下代码用记事本保存为后缀名为.frm的文件中,用VB打开。
==================================
VERSION 5.00
Begin VB.Form Form1
Caption = "Form1"
ClientHeight = 3195
ClientLeft = 60
ClientTop = 345
ClientWidth = 4680
LinkTopic = "Form1"
ScaleHeight = 3195
ScaleWidth = 4680
StartUpPosition = 3 '窗口缺省
Begin VB.CommandButton Command3
Caption = "Command3"
Height = 375
Left = 1920
TabIndex = 4
Top = 1920
Width = 855
End
Begin VB.CommandButton Command2
Caption = "Command2"
Height = 375
Left = 1920
TabIndex = 3
Top = 1200
Width = 855
End
Begin VB.CommandButton Command1
Caption = "Command1"
Height = 375
Left = 1920
TabIndex = 2
Top = 480
Width = 855
End
Begin VB.ListBox List2
Height = 2400
Left = 2880
TabIndex = 1
Top = 360
Width = 1815
End
Begin VB.ListBox List1
Height = 2400
Left = 120
TabIndex = 0
Top = 360
Width = 1695
End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Function IsPrime(ByVal n As Long) As Boolean
Dim i As Long
For i = 2 To Int(n / 2)
If n Mod i = 0 Then IsPrime = False: Exit Function
Next
IsPrime = True
End Function
Private Sub Command2_Click()
Dim i As Integer
ReDim a(0) As Integer
For i = 0 To 29
If IsPrime(List1.List(i)) Then
a(UBound(a)) = List1.List(i)
List2.AddItem a(UBound(a))
ReDim Preserve a(UBound(a) + 1)
End If
Next
Command1.Enabled = True
Command2.Enabled = False
End Sub
Private Sub Command3_Click()
End
End Sub
Private Sub Form_Load()
Command1.Caption = "排序"
Command2.Caption = "筛选"
Command3.Caption = "退出"
Command1.Enabled = False
Randomize
Dim i As Integer
For i = 1 To 30
List1.AddItem Int(Rnd * 1900 + 100)
Next
End Sub