上周老师布置了一个作业,内容大致如下: 给出中国各省份的地图(为.shp格式),在ArcGIS中用VBA随机生成一个省份,猜此省名并确认。
用ArcGIS 9.2 加载图层,打开VBA,使用一个textbox 和三个CommandButton ,把三个commandbutton分别命名为“开始”、“确认”、“信息”,分别用于“随机生成省”、“确认省名”、“猜中的信息”。
以下是代码:
Public pav As IActiveView
Dim p1 As Integer
Dim p2 As Integer
Private Sub CommandButton1_Click()
'用于随机生成一个省
Dim pDoc As IMxDocument
Set pDoc = ThisDocument
Dim pMap As IMap
Set pMap = pDoc.FocusMap
Dim i As Integer
i = Int((33 - 0 + 1) * Rnd + 0)
Dim pFeatLyr As IFeatureLayer
Set pFeatLyr = pMap.Layer(0)
Dim pFeatLyrDef As IFeatureLayerDefinition
Set pFeatLyrDef = pFeatLyr
pFeatLyrDef.DefinitionExpression = "FID = " & i
pDoc.ActiveView.Refresh
End Sub
Private Sub CommandButton2_Click()
"在textbox中输入省名,进行确认工作
Dim sText, sAn As String
sText = TextBox1.Text
Dim pDoc As IMxDocument
Set pDoc = ThisDocument
Dim pMap As IMap
Set pMap = pDoc.FocusMap
Dim pFeatureLyr As IFeatureLayer
Set pFeatureLyr = pMap.Layer(0)
Dim pFeatCursor As IFeatureCursor
Dim pFeat As IFeature
Set pFeat = pFeatCursor
Dim Index As Integer
Index = pFeatureLyr.FeatureClass.Fields.FindField("NAME")
Dim pTable As ITable
Set pTable = pFeatureLyr
Dim pCursor As ICursor
Set pCursor = pTable.Search(Nothing, False)
Dim pRow As IRow
Set pRow = pCursor.NextRow
If sText <> pRow.Value(Index) Then
sAn = pRow.Value(Index)
MsgBox "wrong! 应该是" & sAn
p2 = p2 + 1
Call CommandButton1_Click
Else: MsgBox "right!"
p1 = p1 + 1
p2 = p2 + 1
Call CommandButton1_Click
End If
End Sub
Private Sub CommandButton3_Click()
"进行统计工作
Dim pDoc As IMxDocument
Set pDoc = ThisDocument
Dim pMap As IMap
Set pMap = pDoc.FocusMap
Dim pFeatureLyr As IFeatureLayer
Set pFeatureLyr = pMap.Layer(0)
Dim pFeatCursor As IFeatureCursor
Dim pFeat As IFeature
Set pFeat = pFeatCursor
Dim Index As Integer
Index = pFeatureLyr.FeatureClass.Fields.FindField("NAME")
Dim pTable As ITable
Set pTable = pFeatureLyr
Dim pCursor As ICursor
Set pCursor = pTable.Search(Nothing, False)
Dim pRow As IRow
Set pRow = pCursor.NextRow
If sText = pRow.Value(Index) Then
MsgBox "答对了" & p1 & "题.总共作答" & p2 & "题"
Else
MsgBox "答对了" & p1 & "题.总共作答" & p2 & "题"
End If
End Sub
结尾:这个程序很草率,用人工输入省名很麻烦,但技术有限不清楚怎样用一个控件把所有的省份列出进行选择.