Select to view content in your preferred language

Zoom to selected features

1055
5
06-07-2010 12:21 AM
NehayaArabeiat
Emerging Contributor
Hi All,

I try this code to zoom to selected features but it zooms to nothing and the map becomes blank, what is the problem, please help me??

Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click

        Try

            pActiveview = AxPageLayoutControl1.ActiveView
            Dim pMap, pmap2 As IMap
            pmap2 = pActiveview.FocusMap
            pMap = AxTOCControl1.ActiveView.FocusMap
            pmap2.ClearSelection()
            pActiveview.Refresh()

            If Not TypeOf pMap.Layer(0) Is IFeatureLayer Then Exit Sub
            pfeatureLayer = pMap.Layer(0)
            pfeatureselection = pfeatureLayer 'QI

            Dim where As String
            ' where = "GeoCode2 = '" & Blocks_List.SelectedItem & "'"
            where = "GeoCode2 = '" & Blocks_List.SelectedItem & "'"

            If Not pfeatureselection Is Nothing Then
                Dim pQueryFilter As IQueryFilter = New QueryFilterClass()
                pQueryFilter.WhereClause = where
                pfeatureselection.SelectFeatures(pQueryFilter, esriSelectionResultEnum.esriSelectionResultNew, False)

                ' Dim pfeaturelayer2 As IFeatureLayer
                ' pfeaturelayer2 = pfeatureselection

                Dim pSelSet As ISelectionSet

                pSelSet = pfeatureselection.SelectionSet


                Dim pEnumGeom As IEnumGeometry

                Dim pEnumGeomBind As IEnumGeometryBind


                pEnumGeom = New EnumFeatureGeometry

                pEnumGeomBind = pEnumGeom

                pEnumGeomBind.BindGeometrySource(Nothing, pSelSet)


                Dim pGeomFactory As IGeometryFactory

                pGeomFactory = New GeometryEnvironment


                Dim pGeom As IGeometry

                pGeom = pGeomFactory.CreateGeometryFromEnumerator(pEnumGeom)

                ' AxPageLayoutControl1.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeoSelection, Nothing, Nothing)

                AxPageLayoutControl1.ActiveView.Extent = pGeom.Envelope


                'AxPageLayoutControl1.ActiveView.FocusMap.MapScale = 5593

                AxPageLayoutControl1.ActiveView.Refresh()


            End If

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

    End Sub
0 Kudos
5 Replies
RuchiraWelikala
Regular Contributor
I'm not sure exactly where the issue with the your code is, but in VB.NET (not necessarily with a standalone application), I tend to import the ESRI.ArcGIS.Controls assembly into the code and use this to zoom to selected features.

            ' Declare ICommand to use ZoomToSelected tool
            Dim pCommand As ICommand
            pCommand = New ControlsZoomToSelectedCommand
            ' Pass the m_application as an object for the oncreate method
            pCommand.OnCreate(AxMapControl)
            pCommand.OnClick()

            pMxDoc.FocusMap.MapScale = 100000


for the OnCreate method, I generally use the IApplication, but in your case, it might be the AxMapControl.
Give it a try,
Good luck.
0 Kudos
NehayaArabeiat
Emerging Contributor
Hi rwelikal,

Thanks for your reply.

I tried your code but it gives me this error:

"Exception from HRESULT: 0x800A0424"
0 Kudos
sundareshRamasubramanian
New Contributor
Hi

Can you please explain us on what exactly are you looking at? I dont understand why you are using both layout and map controls.
You please tell us what exactly the process is.
Zooming to a selected feature shouldnt be a problem.
0 Kudos
WilliamChristmas
Deactivated User
Dim pUID As New UID
    Dim pCmdItem As ICommandItem
    pUID.value = "{AB073B49-DE5E-11D1-AA80-00C04FA37860}"
    pUID.SubType = 3
    Set pCmdItem = Application.Document.CommandBars.Find(pUID)
    pCmdItem.Execute
0 Kudos
NehayaArabeiat
Emerging Contributor
Hi
Thanks for your reply.

I try your code but I'm working in vb.net and it give me the following error for Application.Document:
"reference to non-shared member requires an object reference".

Is their a way to call commandbars instead from Application.Document??
0 Kudos