# Selection by polygon

**URL:** <https://mapwindow.discourse.group/t/selection-by-polygon/21>\
**Category:** MapWinGIS\
**Created:** [January 17, 2019, 8:43am UTC](https://mapwindow.discourse.group/t/selection-by-polygon/21 "2019-01-17T08:43:51Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Anthony](https://avatars.discourse-cdn.com/v4/letter/a/49beb7/32.png) [@Anthony](https://mapwindow.discourse.group/u/Anthony)\
**Post date:** [January 17, 2019, 8:43am UTC](https://mapwindow.discourse.group/t/selection-by-polygon/21/1 "2019-01-17T08:43:51Z")

</div>

Hello,

I tried to use the polygon selection but I could not trigger the end event with a ctrl + left mouse click  
The event never goes in the selectboxfinal… but I don’t understand because I have defined SendSelectBoxFinal = true

Should I do something special?

Thanks for your help,

---

<div class="post-metadata">

**Author:** ![pmeems](https://yyz2.discourse-cdn.com/free1/user_avatar/mapwindow.discourse.group/pmeems/32/9_2.png) [@pmeems](https://mapwindow.discourse.group/u/pmeems)\
**Post date:** [January 17, 2019, 4:07pm UTC](https://mapwindow.discourse.group/t/selection-by-polygon/21/2 "2019-01-17T16:07:36Z")

</div>

Can you show some code to reproduce it?

---

<div class="post-metadata">

**Author:** ![Anthony](https://avatars.discourse-cdn.com/v4/letter/a/49beb7/32.png) [@Anthony](https://mapwindow.discourse.group/u/Anthony)\
**Post date:** [January 21, 2019, 1:52pm UTC](https://mapwindow.discourse.group/t/selection-by-polygon/21/3 "2019-01-21T13:52:41Z")

</div>

Hello,

Thank you for your reply  
The code :

```
public partial class Form : System.Windows.Forms.Form
{
    public Form()
    {
        InitializeComponent();

        axMap.SendSelectBoxFinal = true;
    }

    private void buttonSelectByPolygon_Click(object sender, EventArgs e)
    {
        axMap.CursorMode = tkCursorMode.cmSelectByPolygon;
    }

    private void buttonIdentify_Click(object sender, EventArgs e)
    {
        axMap.CursorMode = tkCursorMode.cmIdentify;

        axMap.Identifier.IdentifierMode = tkIdentifierMode.imAllLayers;
    }

    private void buttonLoad_Click(object sender, EventArgs e)
    {
        LoadLayer(@"C:\SHP\CARRY_ORANGE_VACANCIEL-FAC.shp");
        LoadLayer(@"C:\SHP\CARRY_ORANGE_VACANCIEL-POT.shp");

        // ShapeIdentified : Does not work with a polyline shp ?
        LoadLayer(@"C:\SHP\CARRY_ORANGE_VACANCIEL-CABLE.shp");
    }

    private void axMap_SelectBoxFinal(object sender, AxMapWinGIS._DMapEvents_SelectBoxFinalEvent e)
    {
        // Never go here
    }

    private void axMap_ShapeIdentified(object sender, AxMapWinGIS._DMapEvents_ShapeIdentifiedEvent e)
    {
        // Do not work with polylines
        // e.layerHandle = -1 ...

        Shapefile shp = (Shapefile)axMap.get_GetObject(e.layerHandle);
    }

    private void axMap_ShapesIdentified(object sender, AxMapWinGIS._DMapEvents_ShapesIdentifiedEvent e)
    {
        // Never go here
    }

    public void LoadLayer(string layerLocation)
    {
        try
        {
            int layerHandle = axMap.AddLayerFromFilename(layerLocation, tkFileOpenStrategy.fosAutoDetect, true);

            Shapefile sf = axMap.get_Shapefile(layerHandle);

            // Ajout des étiquettes
            if (sf != null)
            {
                sf.Volatile = true;
                sf.CollisionMode = tkCollisionMode.AllowCollisions;
                sf.Labels.Generate("[ETIQUETTE]", tkLabelPositioning.lpCentroid, false);
                sf.Labels.AvoidCollisions = false;
                sf.Identifiable = true;

                // Pour la sélection dans l'interface graphique
                Utils utils = new Utils();

                LabelCategory ct = sf.Labels.AddCategory("Selected");
                ct.FrameBackColor = utils.ColorByName(tkMapColor.Yellow);
            }
        }
        catch
        {
            
        }
    }
}
```

---

<div class="post-metadata">

**Author:** ![jerryfaust](https://yyz2.discourse-cdn.com/free1/user_avatar/mapwindow.discourse.group/jerryfaust/32/378_2.png) [@jerryfaust](https://mapwindow.discourse.group/u/jerryfaust)\
**Post date:** [January 26, 2019, 8:33am UTC](https://mapwindow.discourse.group/t/selection-by-polygon/21/4 "2019-01-26T08:33:04Z")

</div>

Hello @Anthony

Using the cmSelection tool will invoke the SelectBoxFinal. The cmSelectByPolgon, on the other hand, will invoke the ChooseLayer event to allow you specify which layer (or layers) you wish to select. See the documentation here:

[tkCursorMode](https://www.mapwindow.org/documentation/mapwingis4.9/_enumerations_8cs.html#a988ad29d268eb5a035739d90080581d3)

Let us know if you need more clarification.

Regards.  
Jerry.

---

<div class="post-metadata">

**Author:** ![GKing](https://avatars.discourse-cdn.com/v4/letter/g/2acd7d/32.png) [@GKing](https://mapwindow.discourse.group/u/GKing)\
**Post date:** [February 1, 2019, 7:30pm UTC](https://mapwindow.discourse.group/t/selection-by-polygon/21/5 "2019-02-01T19:30:22Z")

</div>

Hi,  
I’ve noticed a similar problem after updating the references to MapWinGIS 5.0.  
My CursorMode is set to cmSelection and SendSelectBoxFinal is set to True but SelectBoxFinal is never raised. The same code works fine with MapWinGIS 4.9.

Thanks

---

<div class="post-metadata">

**Author:** ![Anthony](https://avatars.discourse-cdn.com/v4/letter/a/49beb7/32.png) [@Anthony](https://mapwindow.discourse.group/u/Anthony)\
**Post date:** [February 1, 2019, 9:05pm UTC](https://mapwindow.discourse.group/t/selection-by-polygon/21/6 "2019-02-01T21:05:16Z")

</div>

Hello @jerryfaust

Sorry for the late answer I was on another project.  
I tried to follow your advice (see below) however I have two questions:

```
    private void axMap_ChooseLayer(object sender, AxMapWinGIS._DMapEvents_ChooseLayerEvent e)
    {
        e.layerHandle = 0;
    }

    private void axMap_SelectionChanged(object sender, AxMapWinGIS._DMapEvents_SelectionChangedEvent e)
    {

        Shapefile shp = (Shapefile)axMap.get_GetObject(e.layerHandle);
    }

```

First:  
How should we do if we want to use multiple layers for the same selection?

Second:  
Is it normal that the cursor “cmIdentify” does not work if a point is on a line? I still get:  
:  
layerHandle = -1  
shapeIndex = -1

Thanks

---

<div class="post-metadata">

**Author:** ![jerryfaust](https://yyz2.discourse-cdn.com/free1/user_avatar/mapwindow.discourse.group/jerryfaust/32/378_2.png) [@jerryfaust](https://mapwindow.discourse.group/u/jerryfaust)\
**Post date:** [February 1, 2019, 9:49pm UTC](https://mapwindow.discourse.group/t/selection-by-polygon/21/7 "2019-02-01T21:49:40Z")

</div>

Thank you for the feedback, @GKing.

You’re right. There is an issue in v5.0 that is related to the new Shapefile.Selectable attribute (being able to select multiple layers concurrently by setting their **Selectable** property = True).

However, the behavior already existed in the case of **single-layer selection**. If you specifed a layer handle in the **ChooseLayer** event, even in **v4.9** , AND any shapes were selected, the **SelectionChanged** event was raised, but the **SelectBoxFinal** event was not raised. It was only raised if you did not specify a layer handle in the **ChooseLayer** event OR no shapes were selected.

Even so, a bug now exists in **v5.0** , such that the **SelectBoxFinal** event is not raised in either case.

**Please consider the following** , and feel free to advise (I am including @pmeems in the discussion).

1. Note that historically, the **SelectBoxFinal** event was not raised if a layer was marked for selection AND shapes were actually selected.

2. Based on the documentation, if you do not specify a layer handle in the [ChooseLayer](https://www.mapwindow.org/documentation/mapwingis4.9/group __map__ events.html#gaf932a4b2f0b87a71b714634c49921275) event, _the operation will not be performed_. It does not indicate whether or not the **SelectBoxFinal** event should still be raised.

3. The documentation for the [SelectBoxFinal](https://www.mapwindow.org/documentation/mapwingis4.9/group __map__ events.html#ga55c8081f23dd587626451a84f2c39c55) event puts no conditions on whether or not it should be raised.

4. QUESTION: Should the **SelectBoxFinal** event _always be raised?_, or as in the past, only when no shapes were actually selected?

Regards.  
Jerry.

---

<div class="post-metadata">

**Author:** ![jerryfaust](https://yyz2.discourse-cdn.com/free1/user_avatar/mapwindow.discourse.group/jerryfaust/32/378_2.png) [@jerryfaust](https://mapwindow.discourse.group/u/jerryfaust)\
**Post date:** [February 1, 2019, 11:08pm UTC](https://mapwindow.discourse.group/t/selection-by-polygon/21/8 "2019-02-01T23:08:57Z")

</div>

Hello @Anthony

In order to select from multiple layers in the same selection, do not specify a layer handle in ChooseLayer, and instead, set the Selectable property = True for each layer that you would like to be selectable. Perhaps I should make it more clear in the documentation.

I’m not sure I understand your second question. The **cmIdentify** tool is capable of selecting both point and line shapes just by clicking on them. Are you wanting to select both a point and a line in a single selection? You can do that based on the answer to your first question. Let me know if your question is still unanswered.

Regards.  
Jerry.

---

<div class="post-metadata">

**Author:** ![Anthony](https://avatars.discourse-cdn.com/v4/letter/a/49beb7/32.png) [@Anthony](https://mapwindow.discourse.group/u/Anthony)\
**Post date:** [February 3, 2019, 4:57pm UTC](https://mapwindow.discourse.group/t/selection-by-polygon/21/9 "2019-02-03T16:57:05Z")

</div>

Hello,

Thank you for you precious help @jerryfaust !  
I finally understood how polygon selection works.  
The “cmIdentify” tool does not work in the specific case, if I select the point that is on the line (see attached image)

![Identify](https://global.discourse-cdn.com/free1/uploads/mapwindow1/original/1X/1d8147650a72c9910c7cd34e7c49125c23be62c9.png)

```
    private void axMap_ShapeIdentified(object sender, AxMapWinGIS._DMapEvents_ShapeIdentifiedEvent e)
    {
        // if the point is present on a line
        // e.layerHandle = -1
        // e.shapeIndex = -1
    }

```

Is it a bug?

---

<div class="post-metadata">

**Author:** ![jerryfaust](https://yyz2.discourse-cdn.com/free1/user_avatar/mapwindow.discourse.group/jerryfaust/32/378_2.png) [@jerryfaust](https://mapwindow.discourse.group/u/jerryfaust)\
**Post date:** [February 4, 2019, 4:47am UTC](https://mapwindow.discourse.group/t/selection-by-polygon/21/10 "2019-02-04T04:47:33Z")

</div>

Hello @Anthony

I apologize for any confusion in this discussion - since there are two discussions going on in this thread. One is about shape ‘Selection’ and the other is about shape ‘Identification’. I’ve realized that in my previous response to you, I was mixing up the two concepts. You were asking about the ‘Identify’ tool, and I was giving an answer about the ‘Selection’ tool.

Regarding your sample with the cmIdentify tool, can you please give more context (more of your code)? Here’s what we’re looking for:

1. Are you trying to Identify a single layer (IdentifierMode = imSingleLayer) or all layers (IdentifierMode = imAllLayers)?
2. If you set the mode to imSingleLayer, then you have to set the ActiveLayer property, and if you set the mode to imAllLayers, then you have to set individual layer properties to ‘Identifiable’ in order for them to be Identified, and you would then fetch them out of the [SelectionList](https://www.mapwindow.org/documentation/mapwingis4.9/class_selection_list.html).

Thank you.  
Jerry.

---

<div class="post-metadata">

**Author:** ![Anthony](https://avatars.discourse-cdn.com/v4/letter/a/49beb7/32.png) [@Anthony](https://mapwindow.discourse.group/u/Anthony)\
**Post date:** [February 6, 2019, 7:36pm UTC](https://mapwindow.discourse.group/t/selection-by-polygon/21/11 "2019-02-06T19:36:46Z")

</div>

Hello @jerryfaust ,

Thank your precious help, the object “SelectionList” solves my problem.  
On the other hand I do not find the selection super convenient for polylines. I have to zoom out in order to identify a polyline (see screenshot)

 ![test](https://global.discourse-cdn.com/free1/uploads/mapwindow1/original/1X/1297dff10ed58c47b9640b45302bebaa13bff61c.png)

Finally for polylines I used the “selection” tool.

Thank you,  
Anthony,

---

<div class="post-metadata">

**Author:** ![Walery](https://avatars.discourse-cdn.com/v4/letter/w/e9bcb4/32.png) [@Walery](https://mapwindow.discourse.group/u/Walery)\
**Post date:** [February 21, 2019, 2:55am UTC](https://mapwindow.discourse.group/t/selection-by-polygon/21/12 "2019-02-21T02:55:11Z")

</div>

Hello!  
Regarding the selection of objects on the map polygon. I did as follows.  
If possible, please insert this code into the MapWinGis documentation in the “examples” section. Thanks you!

> #region ----------Selected layer----------  
> //Select By Polygon  
> private void btnSelectByPolygon\_Click(object sender, EventArgs e)  
> {  
> axMap1.CursorMode = tkCursorMode.cmSelectByPolygon;  
> }  
> //Duble Click to Finish Select By Polygon  
> private void axMap1\_DblClick(object sender, EventArgs e)  
> {  
> axMap1.ShapeEditor.SaveChanges();  
> }  
> //Select by Rectangle  
> private void btnSelection\_Click(object sender, EventArgs e)  
> {  
> axMap1.CursorMode = tkCursorMode.cmSelection;  
> }  
> //layer selection  
> private void axMap1\_ChooseLayer(object sender, _DMapEvents\_ChooseLayerEvent e)  
> {  
> e.layerHandle = 0;  
> }  
> //  
> private void axMap1\_SelectionChanged(object sender, DMapEvents\_SelectionChangedEvent e)  
> {  
> get\_selected\_attribute(e.layerHandle);  
> }  
> //Get attribute from selected layer  
> private void get\_selected\_attribute(int layerHandle)  
> {  
> try  
> {  
> var sf = new Shapefile();  
> sf = axMap1.get\_Shapefile(layerHandle);  
> //string expression = “”;  
> string data = “”;  
> /for (int i = 1; i \< sf.NumFields; i++)  
> {  
> expression += “[” + sf.Field[i].Name + “]”;  
> }/  
> int fieldIndex = sf.Table.FieldIndexByName[“num”];  
> if (fieldIndex != -1)  
> {  
> for (int i = 0; i \< sf.NumSelected; i++)  
> {  
> data_ += “[” + (string)sf.CellValue[fieldIndex, i].ToString() + “]”;  
> }  
> }  
> MessageBox.Show("Objects: " + sf.NumSelected.ToString() + "\n Value: " + data\_);  
> [//MessageBox.Show](https://MessageBox.Show)("Objects: “+sf.NumSelected.ToString()+”,\n Fields: “+expression+”,\n Value: "+ data\_);  
> }  
> catch (Exception ex)  
> {  
> MessageBox.Show("Ошибка: " + ex, “Ошибка”, MessageBoxButtons.OK);  
> return;  
> }  
> }  
> #endregion

if you need to select the desired layer, then I did it

```
>private void axMap1_ChooseLayer(object sender, _DMapEvents_ChooseLayerEvent e)
    {
        int handle = 0;
        int thishandle = 0;
        int result = 0;
        while (handle < axMap1.Layers.Count)
        {
            if (axMap1.Layers[handle].Name == "Example Point")
            {
                thishandle = handle;
                result = 1;
            }
            handle++;
        }
        if (result > 0)
        {
            e.layerHandle = thishandle;
        }
    }

```

---

<div class="post-metadata">

**Author:** ![Walery](https://avatars.discourse-cdn.com/v4/letter/w/e9bcb4/32.png) [@Walery](https://mapwindow.discourse.group/u/Walery)\
**Post date:** [February 21, 2019, 3:19am UTC](https://mapwindow.discourse.group/t/selection-by-polygon/21/13 "2019-02-21T03:19:05Z")

</div>

Give an example of an identification code cmIdentify, thanks!

---

<div class="post-metadata">

**Author:** ![pmeems](https://yyz2.discourse-cdn.com/free1/user_avatar/mapwindow.discourse.group/pmeems/32/9_2.png) [@pmeems](https://mapwindow.discourse.group/u/pmeems)\
**Post date:** [July 3, 2019, 7:23am UTC](https://mapwindow.discourse.group/t/selection-by-polygon/21/14 "2019-07-03T07:23:13Z")

</div>


