I'm trying to convert my ArcGIS 9.3 code to ArcGIS 10 via Visual Basic 2008 Express; however, I'm having a couple issues. The goal is for a user to select a feature in ArcMap, click on this custom button, and a user form appears displaying attribute information of the selected feature. The code worked great in 9.3, but not in 10.Currently, I can get the user form to open by itself...but if a user selects a feature and clicks the button it crashes ArcMap. :eek: Also, I have three function and variable warnings in Visual Basic...I'm not sure what they mean or how to fix them...I have attached the warning errors I received in Visual Basic and the code for the user form is below. Any help would be greatly appreciated!!
Open User Form
Public Class TransLineEdit
Inherits ESRI.ArcGIS.Desktop.AddIns.Button
Dim pForm As New frmEdit
Protected Overrides Sub OnClick()
Try
pForm.Show()
Catch ex As Exception
System.Windows.Forms.MessageBox.Show(ex.ToString, "OnClick")
End Try
End Sub
End Class
User Form Code:
Imports ESRI.ArcGIS.ArcMapUI
Imports ESRI.ArcGIS.Carto
Imports ESRI.ArcGIS.Geodatabase
Imports ESRI.ArcGIS.Geometry
Imports ESRI.ArcGIS.ArcMap
Imports ESRI.ArcGIS.DataManagementTools
Imports ESRI.ArcGIS.Desktop
Imports ESRI.ArcGIS.Editor
Imports ESRI.ArcGIS.Framework
Public Class frmEdit
'Declare variables that are used all thru the application
Public pMxDoc As IMxDocument
Public pMap As IMap
Public pEditor As IEditor
Public Application As IAppROT
Private Sub UserForm_Initialize()
'Get Improvement Projects FeatureLayer
Dim pFeatLyr As IFeatureLayer
pFeatLyr = GetLayerByTOC("TransportationImprovementProjects_Lines")
'Get selected feature (only one)
Dim pFeat As IFeature
pFeat = GetSelFeat(pFeatLyr)
'Populate form controls with feature's attribute values
txtPrjYear.Text = pFeat.Value(pFeat.Fields.FindField("YEAR"))
txtPrjType.Text = pFeat.Value(pFeat.Fields.FindField("PROJECTYPE"))
txtAgency.Text = pFeat.Value(pFeat.Fields.FindField("AGENCY"))
txtLocation.Text = pFeat.Value(pFeat.Fields.FindField("LOCATION"))
txtDescrip.Text = pFeat.Value(pFeat.Fields.FindField("DESCRIPTION"))
txtCost.Text = pFeat.Value(pFeat.Fields.FindField("COST"))
txtDate.Text = pFeat.Value(pFeat.Fields.FindField("DATEEDITED"))
txtUser.Text = pFeat.Value(pFeat.Fields.FindField("USEREDITED"))
Dim pDoc As IMxDocument
pDoc = My.ArcMap.Document
End Sub
Private Sub cmdCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCancel.Click
End Sub
Private Sub cmdUpdate_Click()
On Error GoTo EH
'Initialize global variables
Call Initialize()
'Get Transportation Improvement Projects FeatureLayer
Dim pFeatLyr As IFeatureLayer
pFeatLyr = GetLayerByTOC("TransportationImprovementProjects_Lines")
'Get Dataset from FeatureLayer
Dim pEditDataset As IDataset
pEditDataset = pFeatLyr
'If not editing, start editing dataset's Workspace
If pEditor.EditState <> esriEditState.esriStateEditing Then
pEditor.StartEditing(pEditDataset.Workspace)
End If
'Get SelectionSet
Dim pSelSet As ISelectionSet
pSelSet = GetSelection(pFeatLyr)
'Get FeatureCursor from SelectionSet
Dim pFCursor As IFeatureCursor
pSelSet.Search(Nothing, True, pFCursor)
'Initialize first (only) feature
Dim pFeat As IFeature
pFeat = pFCursor.NextFeature
'Update attributes with user input
pFeat.Value(pFeat.Fields.FindField("YEAR")) = txtPrjYear.Text
pFeat.Value(pFeat.Fields.FindField("PROJECTYPE")) = txtPrjType.Text
pFeat.Value(pFeat.Fields.FindField("LOCATION")) = txtLocation.Text
pFeat.Value(pFeat.Fields.FindField("AGENCY")) = txtAgency.Text
pFeat.Value(pFeat.Fields.FindField("DESCRIPTION")) = txtDescrip.Text
pFeat.Value(pFeat.Fields.FindField("COST")) = txtCost.Text
pFeat.Value(pFeat.Fields.FindField("DATEEDITED")) = txtDate.Text
pFeat.Value(pFeat.Fields.FindField("USEREDITED")) = txtUser.Text
'Store changes to feature
pFeat.Store()
'Stop editing and save edits
pEditor.StopEditing(True)
'Refresh map
pMxDoc.ActiveView.Refresh()
'Kill form
Me.Close()
'Avoid error handler
Exit Sub
EH:
'Stop editing, trash edits
If pEditor.EditState <> esriEditState.esriStateEditing Then
pEditor.StopEditing(False)
End If
'Display error
MsgBox("A Runtime Error Occured. Error Number: " & CStr(Err.Number) & _
vbCrLf & "Error Description: " & Err.Description, vbExclamation + vbOKOnly)
End Sub
Private Sub cmdUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdUpdate.Click
'Kill form
Me.Close()
'Clear selected features
Dim pActiveView As IActiveView
pActiveView = pMap
pMap.ClearSelection()
pActiveView.Refresh()
End Sub
Public Sub Initialize()
'Initalize global variables
pMxDoc = Application.Document
pMap = pMxDoc.FocusMap
'Instantiate the editor
pEditor = Application.FindExtensionByName("ESRI Object Editor")
End Sub
Public Function GetLayerByTOC(ByVal lyrName As String) As IFeatureLayer
'This function finds a feature layer based on its TOC Name
Call Initialize()
Dim lyrCntr As Integer
Dim pFeatLyr As IFeatureLayer
For lyrCntr = 0 To pMap.LayerCount - 1
'Ensure that the layer is valid
If pMap.Layer(lyrCntr).Valid = True Then
'Ensure that the layer is a feature layer
If TypeOf pMap.Layer(lyrCntr) Is IFeatureLayer Then
pFeatLyr = pMap.Layer(lyrCntr)
If UCase(pMap.Layer(lyrCntr).Name) = UCase(lyrName) Then
GetLayerByTOC = pFeatLyr
Exit Function
End If
End If
End If
Next lyrCntr
'Layer not found. Show a message
MsgBox("Layer " & lyrName & " not found !", vbCritical, "Error")
End Function
Public Function GetSelFeat(ByVal pFeatLyr As IFeatureLayer) As IFeature
'Gets selected feature from feature layer and returns feature
'Call this function only when you are sure that only one feature is selected
'Initialize the required variables
Call Initialize()
Dim pFeatSel As IFeatureSelection
pFeatSel = pFeatLyr
Dim pSelectionSet As ISelectionSet
pSelectionSet = pFeatSel.SelectionSet
Dim pFeatCur As IFeatureCursor
pSelectionSet.Search(Nothing, True, pFeatCur)
'Return the selected feature
GetSelFeat = pFeatCur.NextFeature
End Function
Public Function GetSelection(ByVal pFeatLyr As IFeatureLayer) As ISelectionSet
'Gets selection of feature layer and returns selection set
'Initialize the required variables
Call Initialize()
Dim pFeatSel As IFeatureSelection
pFeatSel = pFeatLyr
GetSelection = pFeatSel.SelectionSet
End Function
End Class