windowsforms-16857-controls-and-libraries-map-control-end-user-features-select-and-highlight-map-items.md
The Map Control allows end users to select one or multiple vector items on a map surface, and provides API to interact with these items in code. Users can also highlight map items.
This article explains how to:
Use the MapItemsLayerBase.EnableHighlighting property to specify whether users can highlight map items on the layer:
informationLayer.EnableHighlighting = true;
informationLayer.EnableHighlighting = True
The following code customizes the appearance of vector items highlighted by the user:
informationLayer.HighlightedItemStyle.Fill = Color.Gray;
informationLayer.HighlightedItemStyle.Stroke = Color.Orange;
informationLayer.HighlightedItemStyle.StrokeWidth = 2;
informationLayer.HighlightedItemStyle.TextColor = Color.White;
informationLayer.HighlightedItemStyle.Fill = Color.Gray
informationLayer.HighlightedItemStyle.Stroke = Color.Orange
informationLayer.HighlightedItemStyle.StrokeWidth = 2
informationLayer.HighlightedItemStyle.TextColor = Color.White
The table below lists API members used by code listed above:
| Member | Description |
|---|---|
| MapItemsLayerBase.HighlightedItemStyle | Specifies a style to apply to highlighted items on the layer. |
| MapItemStyle.Fill | Defines a highlighted item’s fill color. |
| MapItemStyle.Stroke | Defines a highlighted item’s stroke color. |
| MapItemStyle.StrokeWidth | Specifies a highlighted item’s stroke width in pixels. |
| MapItemTextStyle.TextColor | Defines a highlighted item’s text color. |
Use the MapItemsLayerBase.EnableSelection property to define whether users can select items on the layer:
informationLayer.EnableSelection = false;
informationLayer.EnableSelection = False
The MapControl.SelectionMode property allows you to enable/disable selection for all map layers. The ElementSelectionMode enumeration lists available modes:
|
Mode
|
Example
|
Description
| | --- | --- | --- | |
Single
|
|
A single map item can be selected on the map at the same time.
| |
Multiple
|
|
Multiple map items can be selected at the same time.
You can use Rectangular Selection to select several items at once.
| |
Extended
|
|
Extended mode combines Single and Multiple selection mode behaviors.
Extended mode also allows you to use Rectangular Selection
| |
None
| |
Users cannot select any map items.
|
The code below enables Multiple mode:
mapControl.SelectionMode = ElementSelectionMode.Multiple;
mapControl.SelectionMode = ElementSelectionMode.Multiple
The following code customizes the appearance of items a user selects:
informationLayer.SelectedItemStyle.Fill = Color.Gray;
informationLayer.SelectedItemStyle.Stroke = Color.Orange;
informationLayer.SelectedItemStyle.StrokeWidth = 4;
informationLayer.SelectedItemStyle.TextColor = Color.White;
informationLayer.SelectedItemStyle.Fill = Color.Gray
informationLayer.SelectedItemStyle.Stroke = Color.Orange
informationLayer.SelectedItemStyle.StrokeWidth = 4
informationLayer.SelectedItemStyle.TextColor = Color.White
The table below lists the API members that code listed above uses:
| Member | Description |
|---|---|
| MapItemsLayerBase.SelectedItemStyle | Specifies a style to apply to selected items in the layer. |
| MapItemStyle.Fill | Defines a selected item’s fill color. |
| MapItemStyle.Stroke | Defines a selected item’s stroke color. |
| MapItemStyle.StrokeWidth | Specifies a selected item stroke’s width in pixels. |
| MapItemTextStyle.TextColor | Defines selected item text’s color. |
You can change the selection rectangle’s fill color and outline appearance:
The code below configures the selection rectangle as in the image above:
mapControl.SelectedRegionStyle.Fill = Color.FromArgb(80, 255, 165, 0);
mapControl.SelectedRegionStyle.Stroke = Color.FromArgb(144, 255, 165, 0);
mapControl.SelectedRegionStyle.Fill = Color.FromArgb(80, 255, 165, 0)
mapControl.SelectedRegionStyle.Stroke = Color.FromArgb(144, 255, 165, 0)
The table below lists the API members that code listed above uses:
| Member | Description |
|---|---|
| MapControl.SelectedRegionStyle | Specifies a style to apply to the Selection Rectangle’s area. |
| BackgroundStyle.Fill | Gets or sets the Selection Rectangle’s fill color. |
| BorderedElementStyle.Stroke | Gets or sets the Selection Rectangle’s stroke color. |
The MapItemsLayerBase.SelectedItems collection stores the items a user selects. The MapItemsLayerBase.SelectedItem property keeps the SelectedItems collection’s first item.
The Map control raises the MapControl.SelectionChanged event after the SelectedItems collection is changed.
The MapControl.SelectionChanging event occurs before any map item is selected. Set the MapSelectionChangingEventArgs.Cancel property to true to cancel the item’s selection.
mapControl1.SelectionChanging += mapControl1_SelectionChanging;
mapControl1.SelectionChanged += mapControl1_SelectionChanged;
//...
private void mapControl1_SelectionChanged(object sender, MapSelectionChangedEventArgs e) {
dataGenerator.SelectedPlane = e.Selection.Count > 0 ? (PlaneInfo)e.Selection[0] : null;
OnActivePlaneChanged();
}
private void mapControl1_SelectionChanging(object sender, MapSelectionChangingEventArgs e) {
PlaneInfo plainInfo = e.Selection.Count > 0 ? e.Selection[0] as PlaneInfo : null;
e.Cancel = plainInfo == null;
}
mapControl1.SelectionChanging = (mapControl1.SelectionChanging + mapControl1_SelectionChanging)
mapControl1.SelectionChanged = (mapControl1.SelectionChanged + mapControl1_SelectionChanged)
'...
Private Sub mapControl1_SelectionChanged(ByVal sender As Object, ByVal e As MapSelectionChangedEventArgs)
dataGenerator.SelectedPlane = If(e.Selection.Count > 0, CType(e.Selection(0), PlaneInfo), Nothing)
If e.Selection.Count > 0 Then
dataGenerator.SelectedPlane = CType(e.Selection(0), PlaneInfo)
Else
dataGenerator.SelectedPlane = Nothing
End If
OnActivePlaneChanged()
End Sub
Private Sub mapControl1_SelectionChanging(ByVal sender As Object, ByVal e As MapSelectionChangingEventArgs)
Dim plainInfo As PlaneInfo
If (e.Selection.Count > 0) Then
plainInfo = CType(e.Selection(0),PlaneInfo)
Else
plainInfo = Nothing
End If
e.Cancel = (plainInfo Is Nothing)
End Sub
Note
Use MapItem.IsHitTestVisible to specify whether a user can highlight or select the item.