[GIS] How to zoom to selected Feature in ArcMap using ArcObjects

arcmaparcobjects

By using the QueryFilter I am getting some features based on the query and I am placing that feature names in a listbox.
When the user selects a particular name in the listbox that feature has to be zoomed to in the map.

Below is my code:

IQueryFilter pQueryFilter = new QueryFilterClass();
            pQueryFilter.WhereClause = textBox1.Text + textBox2.Text;
            ICursor pCursor = pFeatureClass.Search(pQueryFilter, true) as ICursor;
            IRow pRow = pCursor.NextRow();
while (pRow != null)
            {
                    listBox2.Items.Add(pRow.get_Value(9).ToString());
                pRow = pCursor.NextRow();

            }

Here is what I tried:

IActiveView pActiveView = pMxDocument.ActiveView;
            IGeometry pGeometry = pFeature.Shape;
            IEnvelope pEnvelope = pGeometry.Envelope;

            pActiveView.Extent = pEnvelope.Expand(1.2, 1.2, true);

But I am unable to zoom.

Best Answer

This is not tested.

public void ZoomTo(IGeometry geometry)
{
    IMxDocument doc = (IMxDocument)ArcMap.Application.Document;
    IMap map = (IMap)doc.FocusMap;
    IActiveView pActiveView = (IActiveView)map;     
    IEnvelope pEnvelope;

    if (geometry is IPoint)
    {
          IEnvelope currentEnv = pActiveView.Extent;
          IPoint point = (IPoint)geometry;
          currentEnv.CenterAt(point);
          pActiveView.Extent = currentEnv;
          map.MapScale = 100; //to set the scale to 1:100         
    }
    else
    {
        pEnvelope = geometry.Envelope;
        pEnvelope.Expand(1.2, 1.2, true);

        pActiveView.Extent = pEnvelope;
    }

    pActiveView.Refresh();
}