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.
**User Form Code:**
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 frmLineSelectAttribute
'Declare variables that are used all thru the application
Public pMxDoc As IMxDocument
Public pMap As ESRI.ArcGIS.Carto.IMap
Public pEditor As IEditor
Public Application As IAppROT
Private Sub cmdCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCancel.Click
'Kill form
Me.Close()
'Clear selected features
'Dim pActiveView As ESRI.ArcGIS.Carto.IActiveView
'pActiveView = pMap
'pMap.ClearSelection()
'pActiveView.Refresh()
End Sub
Shared Function AccessLayersData(ByVal pLayer As ESRI.ArcGIS.Carto.ILayer) As IFeatureClass
Dim pFeatureLayer As ESRI.ArcGIS.Carto.IFeatureLayer = pLayer
If Not pFeatureLayer Is Nothing Then
Return pFeatureLayer.FeatureClass
Else
Return Nothing
End If
End Function
Private Sub UserForm_Initialize()
'Get Improvement Projects FeatureLayer
Dim pFeatLyr As ESRI.ArcGIS.Carto.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
'Dim pItem As ICommandItem
'pItem = Project.ThisDocument.CommandBars.Find(arcid.Query_ZoomToSelected)
'pItem.Execute()
'pDoc.ActiveView.Refresh()
End Sub
Public Function GetSelFeat(ByVal pFeatLyr As ESRI.ArcGIS.Carto.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 ESRI.ArcGIS.Carto.IFeatureSelection
pFeatSel = pFeatLyr
Dim pSelectionSet As ISelectionSet
pSelectionSet = pFeatSel.SelectionSet
pSelectionSet.Search(Nothing, True, pFeatSel)
'Return the selected feature
GetSelFeat = pFeatSel.NextFeature
End Function
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 ESRI.ArcGIS.Carto.IFeatureLayer
'This function finds a feature layer based on its TOC Name
Call Initialize()
Dim lyrCntr As Integer
Dim pFeatLyr As ESRI.ArcGIS.Carto.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 ESRI.ArcGIS.Carto.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
End Class