# 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:** 1\
**Showing post:** 12

<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;
        }
    }

```

---

_[View the full topic](https://mapwindow.discourse.group/t/selection-by-polygon/21)._
