Select to view content in your preferred language

How to select object by mouse in vba?

4579
6
06-16-2010 11:32 PM
chriss_
Emerging Contributor
Hi!

Anybody has an idea how to include a function to a vba form that allows me to select a feature by mouse?
At the end I want to read the value of a field of the feature i seleced by mouse.

I gto stucked on this for quite some time.
Please help me
Thanks
Chris
0 Kudos
6 Replies
chriss_
Emerging Contributor
Hi!

So really nobody can answer this question? I can hardly believe. It looks like something really basic. Please help me on this.

Best regards
Chris
0 Kudos
WilliamChristmas
Deactivated User
Private Sub UIToolControl2_MouseDown(ByVal button As Long, ByVal shift As Long, ByVal x As Long, ByVal y As Long)
    Dim pMxDoc As IMxDocument
    Set pMxDoc = Application.Document
    Dim pMxApp As IMxApplication
    Set pMxApp = Application
    Dim pPoint As IPoint
    Set pPoint = pMxApp.Display.DisplayTransformation.ToMapPoint(x, y)
    Dim env As ISelectionEnvironment
    pMap.SelectByShape pPoint, env, False
    pMxDoc.ActivatedView.PartialRefresh esriViewGeoSelection, Nothing, Nothing
End Sub

You might need to buffer the point if you're selecting line or point features.
To do that:

Dim pTopoOp as ITopologicalOperator
Dim bufferDistance as Long  (I suggest you use something like 0.005 * pMap.MapScale)
Set pTopoOp = pPoint
pMap.SelectByShape pTopoOp.Buffer(bufferDistance), env, False
0 Kudos
chriss_
Emerging Contributor
Thanks Will!

I really tried to get he Code run but it always get stuck on this line

pMap.SelectByShape pPoint, env, False

I get the Error Message "Object required". If I point in this line with the cursor on "env" it tells me "env=nothing".
I would think that I ve to set the ISelectionEnvironment before this line isnt it?( but i ve no clue how to do this)

You ve any idea what I might have done wrong?

Best regards and thanks for your help
0 Kudos
chriss_
Emerging Contributor
Thanks Will!

I really tried to get he Code run but it always get stuck on this line

pMap.SelectByShape pPoint, env, False

I get the Error Message "Object required". If I point in this line with the cursor on "env" it tells me "env=nothing".
I would think that I ve to set the ISelectionEnvironment before this line isnt it?( but i ve no clue how to do this)

You ve any idea what I might have done wrong?

Best regards and thanks for your help
0 Kudos
WilliamChristmas
Deactivated User
woops simple mistake, sorry

After you set pMxDoc do this:
Dim pMap as IMap
Set pMap = pMxDoc.FocusMap
0 Kudos
chriss_
Emerging Contributor
Hi Will!

I figured out (with your help: )) how to run this on a UIToolControl. In fact I finally found several possibilities.

BUT: How can I use it in a FORM?
I tried to copy the CODE to a CommandButton in my Form but there it doesn't work. It runs but I can not fire the selecting mouseclick so I always get "0 Areas selected". I tried many different ways all of them working fine on the UIToolControl but none is running in my Form.

For the UITollControl I get this wrapper line:
Private Sub UIToolQuery_MouseDown(ByVal button As Long, ByVal shift As Long, ByVal x As Long, ByVal y As Long)

For Mousedown in the form I get this wrapper line:
Private Sub From1_MouseDown(ByVal button As Integer, ByVal shift As Integer, ByVal x As Single, ByVal y As Single)

I guess somewhere in there must be the problem but no idea how to solve it.

Anyboy an idea?

P.s. The Code that is running fine on my UITollControl:


Private Sub UIToolQuery_MouseDown(ByVal button As Long, ByVal shift As Long, ByVal x As Long, ByVal y As Long)

   ' Part 1: Get the point clicked by the user.
    Dim pMxDoc As IMxDocument
    Dim pActiveView As IActiveView
    Dim m_blnMouseDown As Boolean
    Dim pPoint As IPoint
    Set pMxDoc = ThisDocument
    Set pActiveView = pMxDoc.FocusMap
    ' Convert teh entered point from display coordinates to map coordinates.
    Set pPoint = pActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(x, y)

    ' Part 2: Perform a spatial query of features within 20,000 meters of the entered point.
    Dim pLayer As IFeatureLayer
    Dim pSpatialFilter As ISpatialFilter
    Dim pTopoOperator As ITopologicalOperator
    Dim pSelection As IFeatureSelection
    Dim pElement As IElement
    Dim pSelectionSet As ISelectionSet
    Set pLayer = pMxDoc.FocusMap.Layer(1)
    ' Create a 20000-meter buffer polygon around the clicked point.
    Set pTopoOperator = pPoint
    Set pElement = New PolygonElement
    pElement.Geometry = pTopoOperator.Buffer(20000)
    ' Create a spatial filter for selecting features within the buffer polygon.
    Set pSpatialFilter = New SpatialFilter
    pSpatialFilter.SpatialRel = esriSpatialRelContains
    Set pSpatialFilter.Geometry = pElement.Geometry
    ' Refresh the active view.
    pActiveView.PartialRefresh esriViewGeoSelection, Nothing, Nothing
    ' Perform spatial query.
    Set pSelection = pLayer
    pSelection.SelectFeatures pSpatialFilter, esriSelectionResultNew, False
    ' Refresh the active view to highlight the selected features.
    pActiveView.PartialRefresh esriViewGeoSelection, Nothing, Nothing
    ' Create a selection set and report number of features in the set.
    Set pSelectionSet = pSelection.SelectionSet
    MsgBox pSelectionSet.count & " Areas selected."


End Sub
0 Kudos