I'm trying to zoom to a feature just like in Arcmap but in VB.net. I'm using Axmapcontrol to display the map.
I've tried the following:

Dim pCmdItem As ICommandItem
Dim pUID As New UID
pUID.Value = "{AB073B49-DE5E-11D1-AA80-00C04FA37860}"
pCmdItem = CommandBars.Find(pUID)

but it's showing the following error:

Reference to a non-shared member requires an object reference.

I've found a solution that suggets to change mytype from empty to WindowsForms in the vbproj file, but it already contains the correct value. Is there any way to zoom to a feature in an Axmapcontrol in VB.net?


This is one approach, you build the extent you want to zoom to by cycling through the selection. The following VBA function takes as input an ILayer object.

Public Sub ZoomToSelected(ByRef pLayer As ILayer)

    If TypeOf pLayer Is IFeatureLayer Then
        ' Get selection on layer
        Dim pFeatureLayer As IFeatureLayer
        Set pFeatureLayer = pLayer
        Dim pFeatureSelection As IFeatureSelection
        Set pFeatureSelection = pFeatureLayer

        If pFeatureSelection.SelectionSet.Count > 0 Then
            ' Layer has a selection so process it
            Dim pEnvelope As IEnvelope
            Set pEnvelope = New Envelope
            Dim pFeature As IFeature

            ' Create a cursor over selection
            Dim pFeatureCursor As IFeatureCursor
            pFeatureSelection.SelectionSet.Search Nothing, True, pFeatureCursor
            Set pFeature = pFeatureCursor.NextFeature
            Dim pEnv As IEnvelope

            ' Loop unioning extents
            Do While Not pFeature Is Nothing
                Set pEnv = pFeature.Extent
                pEnvelope.Union pEnv
                Set pFeature = pFeatureCursor.NextFeature

            ' Get active view an reset extent
            Dim pMXD As IMxDocument
            Set pMXD = ThisDocument
            Dim pMap As IMap
            Set pMap = pMXD.FocusMap
            Dim pAV As IActiveView
            Set pAV = pMap
            pAV.Extent = pEnvelope
        End If
    End If
End Sub

You may want to tweak the code to expand the extent before you set the Activeview to it. That can be done via the IEnvelope interface

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.