Update Note Geometry
DockWindow.vb
' Copyright 2011 ESRI
' 
' All rights reserved under the copyright laws of the United States
' and applicable international laws, treaties, and conventions.
' 
' You may freely redistribute and use this sample code, with or
' without modification, provided you include the original copyright
' notice and use restrictions.
' 
' See the use restrictions.
' 

Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
'Imports System.Drawing
Imports System.Data
Imports System.Linq
Imports System.Text
Imports System.Windows.Forms

Imports ESRI.ArcGISExplorer
Imports ESRI.ArcGISExplorer.Application
Imports ESRI.ArcGISExplorer.Mapping
Imports ESRI.ArcGISExplorer.Geometry
Imports ESRI.ArcGISExplorer.Data
Imports ESRI.ArcGISExplorer.Threading

Enum UpdateProperty
  X
  Y
End Enum

Public Partial Class DockWindow
  Inherits ESRI.ArcGISExplorer.Application.DockWindow

  Private _disp As MapDisplay = Nothing
  Private _currentNote As Note = Nothing
  Private _currentGeom As Geometry = Nothing
  Private _selectingItem As Boolean = False
  Private _selectedIndex As Integer = -1

  #Region "Displaying and updating coordinates"

  Private Sub UpdateGeometryOfNote(updateProperty__1 As UpdateProperty)
    ' This function deals with updating the coordinates of existing 
    ' Points or the coordinates of the vertices of Polylines and Polygons.

    ' First check the entered value is valid.
    Dim newCoordinate As Double = [Double].NaN
    Dim edited As Boolean = False
    If updateProperty__1 = UpdateProperty.X Then
      edited = Double.TryParse(Me.txtX.Text, newCoordinate)
    Else
      edited = Double.TryParse(Me.txtY.Text, newCoordinate)
    End If

    If edited Then
      ' For each type of Geometry, get a Point representing the
      ' existing Point, and then change the edited coordinate value;
      ' this will ensure all other properties of the Point are
      ' preserved (instead of creating a new Point).
      If _currentGeom.GeometryType = GeometryType.Point Then
        Dim pt As Point = TryCast(_currentGeom, Point)
        If updateProperty__1 = UpdateProperty.X Then
          pt.X = newCoordinate
        End If
        If updateProperty__1 = UpdateProperty.Y Then
          pt.Y = newCoordinate
        End If
        _currentNote.Graphic.Geometry = pt

      ElseIf (_currentGeom.GeometryType = GeometryType.Polyline) OrElse (_currentGeom.GeometryType = GeometryType.Polygon) Then
        ' Only the first part of multipart Polygons or Polylines is edited in this sample.
        Dim vgeom As New VertexGeometry(_currentGeom)
        Dim vertex As Point = vgeom.GetPoint(_selectedIndex)
        If updateProperty__1 = UpdateProperty.X Then
          vertex.X = newCoordinate
        End If
        If updateProperty__1 = UpdateProperty.Y Then
          vertex.Y = newCoordinate
        End If
        vgeom.SetPoint(_selectedIndex, vertex)
        _currentNote.Graphic.Geometry = _currentGeom

        RefreshListItem(_selectedIndex, vertex)
      End If
    Else
      ' User entered an invalid value - reset the UI.
      ShowGeometryCoords()
    End If
  End Sub

  Private Sub ShowGeometryCoords()
    _selectingItem = True

    SetupControlsForSelectedGeom()

    If _currentGeom.GeometryType = ESRI.ArcGISExplorer.Geometry.GeometryType.Point Then
      Dim pt As Point = TryCast(_currentGeom, Point)
      ShowSelectedCoords(pt)
    ElseIf (_currentGeom.GeometryType = GeometryType.Polyline) OrElse (_currentGeom.GeometryType = GeometryType.Polygon) Then
      Dim vgeom As New VertexGeometry(_currentGeom)
      For i As Integer = 0 To vgeom.PointCount() - 1
        AddVertexToListView(i, vgeom.GetPoint(i))
      Next
    End If
    _selectingItem = False
  End Sub

  #End Region

  #Region "UI Controls Updating"

  Private Sub RefreshListItem(_selectedIndex As Integer, lineVertex As Point)
    Dim item As ListViewItem = Me.lvwVertices.Items(_selectedIndex)
    item.SubItems(1).Text = lineVertex.X.ToString()
    item.SubItems(2).Text = lineVertex.Y.ToString()
  End Sub

  Private Sub ShowSelectedCoords(pt As Point)
    Me.txtX.Text = pt.X.ToString()
    Me.txtY.Text = pt.Y.ToString()
  End Sub

  Private Sub AddVertexToListView(vertexIndex As Integer, point As Point)
    Me.lvwVertices.Items.Add(New ListViewItem(New String() {vertexIndex.ToString(), point.X.ToString(), point.Y.ToString()}))
  End Sub

  Private Sub SetupControlsForSelectedGeom()
    Me.txtX.Text = ""
    Me.txtY.Text = ""
    Me.lvwVertices.Items.Clear()

    If _currentGeom Is Nothing Then
      Me.splitCoordinates.Enabled = False
      Me.lvwVertices.Enabled = False
      Me.txtMultiVertex.Enabled = False
    ElseIf _currentGeom.GeometryType = GeometryType.Point Then
      Me.splitCoordinates.Enabled = True
      Me.lvwVertices.Enabled = False
      Me.txtMultiVertex.Enabled = False
    ElseIf (_currentGeom.GeometryType = GeometryType.Polyline) OrElse (_currentGeom.GeometryType = GeometryType.Polygon) Then
      Me.splitCoordinates.Enabled = True
      Me.lvwVertices.Enabled = True
      Me.txtMultiVertex.Enabled = True
    End If
  End Sub

  #End Region

  #Region "UI event handlers functions"

    Public Sub New()
        InitializeComponent()

    ' Set up an event sink for clicking or selecting a Note.
    _disp = ESRI.ArcGISExplorer.Application.Application.ActiveMapDisplay
    AddHandler _disp.MapItemClicked, New EventHandler(Of MapItemMouseEventArgs)(AddressOf disp_MapItemClicked)
    AddHandler ESRI.ArcGISExplorer.Application.Application.MapItemSelectionChanged, New EventHandler(AddressOf Application_MapItemSelectionChanged)

    SetupControlsForSelectedGeom()
  End Sub

  Private Sub Application_MapItemSelectionChanged(ByVal sender As Object, ByVal e As EventArgs)
    ' Event handler for when a MapItem is clicked.
    Dim selItems As SelectedItemsCollection = ESRI.ArcGISExplorer.Application.Application.SelectedItems
    If selItems.Count = 1 Then
      If TypeOf selItems(0) Is Note Then
        _currentNote = selItems(0)
        _currentGeom = _currentNote.Graphic.Geometry

        ShowGeometryCoords()
      End If
    End If
  End Sub


  Private Sub disp_MapItemClicked(ByVal sender As Object, ByVal e As MapItemMouseEventArgs)
    ' Event handler for when a MapItem is clicked.
    If TypeOf e.MapItems(0) Is Note Then
      _currentNote = TryCast(e.MapItems(0), Note)
      _currentGeom = _currentNote.Graphic.Geometry

      ShowGeometryCoords()
    End If
  End Sub

  Private Sub txtX_TextChanged(ByVal sender As Object, ByVal e As EventArgs) Handles txtX.TextChanged
    If _selectingItem Then
      Return
    End If
    UpdateGeometryOfNote(UpdateProperty.X)
  End Sub

  Private Sub txtY_TextChanged(ByVal sender As Object, ByVal e As EventArgs) Handles txtY.TextChanged
    If _selectingItem Then
      Return
    End If
    UpdateGeometryOfNote(UpdateProperty.Y)
  End Sub

  Private Sub lvwVertices_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles lvwVertices.SelectedIndexChanged
    If Me.lvwVertices.SelectedItems.Count = 1 Then
      _selectingItem = True

      Dim selected As ListViewItem = Me.lvwVertices.SelectedItems(0)
      _selectedIndex = selected.Index
      Me.txtX.Text = selected.SubItems(1).Text
      Me.txtY.Text = selected.SubItems(2).Text

      _selectingItem = False
    End If
  End Sub

#End Region
End Class