如何编辑记录

本例实现的是如何修改FeatureClass中某条记录(Feature)的值。

l   要点

通过IFeatureClass.Update方法获得可修改记录的IFeatureCursor接口对象,使用IFeatureCursor.NextFeature方法获得Ifeatur接口对象,修改其属性值,通过IFeatureCursor.UpdateFeature方法提交IFeature修改内容。

主要用到IFeatureCursor接口

l   程序说明

函数OpenFeatureClass获得当前激活的Map中第一层的IFeatureClass接口对象。

函数EditFeature修改pFeatureClass中第一条记录的第七个字段的值。

l   代码

Private Function EditFeature(pFeatureClass As IFeatureClass) As Boolean

    Dim pFeature                As IFeature

    Dim pFeatureCursor          As IFeatureCursor

On Error GoTo ErrorHandler:

    EditFeature = False

    If (pFeatureClass Is Nothing) Then

        Exit Function

    End If

    Set pFeatureCursor = pFeatureClass.Update(Nothing, False)

    Set pFeature = pFeatureCursor.NextFeature

    If (Not pFeature Is Nothing) Then

        pFeature.Value(6) = "New Place"

        pFeatureCursor.UpdateFeature pFeature

        MsgBox ("修改成功")

        EditFeature = True

    Else

        MsgBox ("修改失败")

    End If

    Exit Function

ErrorHandler:

    MsgBox Err.Description

End Function

Private Function OpenFeatureClass() As IFeatureClass

    Dim pMxDocument             As IMxDocument

    Dim pMap                    As IMap

    Dim pFeatureLayer           As IFeatureLayer

    Dim pFeatureClass           As IFeatureClass

On Error GoTo ErrorHandler:

    Set OpenFeatureClass = Nothing

    Set pMxDocument = ThisDocument

    Set pMap = pMxDocument.FocusMap

    If (pMap.LayerCount = 0) Then

        MsgBox ("缺少数据")

        Exit Function

    End If

    Set pFeatureLayer = pMap.Layer(0)

    Set pFeatureClass = pFeatureLayer.FeatureClass

    Set OpenFeatureClass = pFeatureClass

    Exit Function

ErrorHandler:

    MsgBox Err.Description

End Function

Private Sub UIButtonControl1_Click()

On Error GoTo ErrorHandler:

    Dim pFeatureClass       As IFeatureClass

    Set pFeatureClass = OpenFeatureClass()

    EditFeature pFeatureClass

    Exit Sub

ErrorHandler:

    MsgBox Err.Description

End Sub

posted on 2006-09-07 13:04  greatbird  阅读(483)  评论(0)    收藏  举报

导航