Display Selected Features Attributes in User Form

1892
2
11-29-2011 01:50 PM
PhyllisDavis
New Contributor II
Greetings!

I'm trying to convert my 9.3 code to ArcGIS 10..and am having some problems. My goal is to allow users to select a feature, click on a button, and the feature's attributes will appear in a user form that they can edit.

I have coverted everything using Visual Basic Express 2008 and am able to get the window to pop-open when a feature isn't selected (obviously the user form does not contain attribute information at this time)....but my ArcMap crashes everytime I have a feature selected and I press my button.

Any suggestions?
0 Kudos
2 Replies
PhyllisDavis
New Contributor II
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



0 Kudos
JamesCrandall
MVP Frequent Contributor
Add Try Catch blocks to each of your methods/functions and ditch the "On Error GoTo EH" (VB6.0 stuff I think).  This will kill the crashes you get and display an actual error message when something does go wrong.

I didn't fully check into the depth of your code, but if I were to guess I'd say the Editing stuff if probably cause of your issues.  Just not real sure though.

Edit: also,

I don't think this actually removes the reference/kills the form like you may think:


 'Kill form
 Me.Close()


Shouldn't your form have a dispose method?
0 Kudos