Dock Window (vb.net) crashes upon feature selection

2109
3
04-28-2011 08:31 AM
RobertMueller
New Contributor
I have a PLSS form (SLO_DockableWindow1) with several comboboxes (section, township, township direction, range, and range direction) and a "Find" button. The comboboxes are populated through their item collection. When I click "Find" to run the query from the comboboxes, ArcMap crashes - every time. This has to be a problem with my overall vb.net coding. Any one see the problem?

_______________________
Imports ESRI.ArcGIS.esriSystem
Imports ESRI.ArcGIS.Framework
Imports ESRI.ArcGIS.ArcMapUI
Imports ESRI.ArcGIS.Desktop
Imports ESRI.ArcGIS.Geodatabase
Imports ESRI.ArcGIS.SystemUI
Imports ESRI.ArcGIS.Geometry
Imports ESRI.ArcGIS.Display
Imports ESRI.ArcGIS.DataSourcesFile
Imports ESRI.ArcGIS.Carto


Public Class SLO_DockableWindow1

    Public sMer As String
    Public sSection As String
    Public sTwp As String
    Public sRng As String
    Public sTwpD As String
    Public sRngD As String
    Public sUnit As String
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim pMap As IMap
        Dim pMxDoc As IMxDocument
        Dim pActiveView As IActiveView

        pMxDoc = My.ArcMap.Document
        pMap = pMxDoc.FocusMap


        sSection = Sec_cbo.Text
        sTwp = Twp_cbo.Text
        sTwpD = TwpD_cbo.Text
        sRng = Rng_cbo.Text
        sRngD = RngD_cbo.Text

        Dim sLayerName As String
        Dim pFeatureLayer As IFeatureLayer
        Dim i As Integer

        sLayerName = "PLSS"
        For i = 0 To pMap.LayerCount - 1
            If pMap.Layer(i).Name = sLayerName Then
                pFeatureLayer = pMap.Layer(i)
                Exit For
            End If
        Next i

        ' Create the query filter
        Dim pQueryFilter As IQueryFilter
        Dim SrchStr As String

        Dim pFeatureSelection As IFeatureSelection

        pQueryFilter = New ESRI.ArcGIS.Geodatabase.QueryFilterClass
        SrchStr = "PARCEL_ID='" & sSection & "'AND TWP'" & sTwp & "'AND TWPD='" & sTwpD & "'AND RNG='" & sRng & "'AND RNGD='" & sRngD & "'"
        pQueryFilter.WhereClause = SrchStr
        MsgBox(SrchStr)
        'Invalidate only the selection cache
        'Flag the original selection
        pActiveView.PartialRefresh(ESRI.ArcGIS.Carto.esriViewDrawPhase.esriViewGeoSelection, Nothing, Nothing)
        'Perform the selection
        pFeatureSelection.SelectFeatures(pQueryFilter, ESRI.ArcGIS.Carto.esriSelectionResultEnum.esriSelectionResultNew, False)
        'Flag the new selection
        pActiveView.PartialRefresh(ESRI.ArcGIS.Carto.esriViewDrawPhase.esriViewGeoSelection, Nothing, Nothing)


    End Sub
_______________________
0 Kudos
3 Replies
SteveFang
New Contributor III
Not sure if you intent to do this but you are missing a "=" in the SQL string.

SrchStr = "PARCEL_ID='" & sSection & "'AND TWP = '" & sTwp & "'AND TWPD='" & sTwpD & "'AND RNG='" & sRng & "'AND RNGD='" & sRngD & "'"
0 Kudos
RobertMueller
New Contributor
Here is my vb.net sample for a PLSS finder in response to my question above. My "tool" starts with a button on the toolbar, which then opens up a dockable window with several comboboxes.

_______________________________________________________________
DockableWindow.vb:

Imports ESRI.ArcGIS.esriSystem
Imports ESRI.ArcGIS.Framework
Imports ESRI.ArcGIS.ArcMapUI
Imports ESRI.ArcGIS.Desktop
Imports ESRI.ArcGIS.Geodatabase
Imports ESRI.ArcGIS.Geometry
Imports ESRI.ArcGIS.Carto

Public Class SLO_DockableWindow1

    Public sMer As String
    Public sSection As String
    Public sTwp As String
    Public sRng As String
    Public sTwpD As String
    Public sRngD As String
    Public sUnit As String
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim pMap As IMap
        Dim pMxDoc As IMxDocument
        Dim pActiveView As IActiveView

        pMxDoc = My.ArcMap.Document
        pMap = pMxDoc.FocusMap
        pActiveView = pMap

        Dim sLayerName As String
        Dim FeatureLayer As IFeatureLayer
        Dim SrchStr As String

        Dim i As Integer

        'Layer name to look for in the TOC
        sLayerName = "PLSS"

        'Finds the layer in the TOC
        For i = 0 To pMap.LayerCount - 1
            If pMap.Layer(i).Name = sLayerName Then
                FeatureLayer = pMap.Layer(i)

                Exit For
            End If
        Next i

        'Checks to make sure "PLSS" layer is available
        If pActiveView Is Nothing OrElse FeatureLayer Is Nothing Then
            MsgBox("Please add the PLSS layer and select a feature", MsgBoxStyle.Information, "PLSS")
            Return
        End If

        Dim featureSelection As ESRI.ArcGIS.Carto.IFeatureSelection = TryCast(FeatureLayer, ESRI.ArcGIS.Carto.IFeatureSelection) ' Dynamic Cast

        'Sets the search string from the combobox inputs
        SrchStr = "PARCEL_ID = '" & sSection & "'AND TWP =" & sTwp & " AND TWPD ='" & sTwpD & "'AND RNG = " & sRng & "AND RNGD = '" & sRngD & "'"

        'A series of checks to make sure all necessary inputs are in place - error checking
        If SrchStr Is Nothing Then
            MsgBox("Please make a selection in every box", MsgBoxStyle.Information, "PLSS")
            Return
        ElseIf sSection Is Nothing Then
            MsgBox("Please enter a section", MsgBoxStyle.Information, "PLSS")
            Return
        ElseIf sTwp Is Nothing Then
            MsgBox("Please enter a township range", MsgBoxStyle.Information, "PLSS")
            Return
        ElseIf sTwpD Is Nothing Then
            MsgBox("Please enter a township direction", MsgBoxStyle.Information, "PLSS")
            Return
        ElseIf sRng Is Nothing Then
            MsgBox("Please enter a range", MsgBoxStyle.Information, "PLSS")
            Return
        ElseIf sRngD Is Nothing Then
            MsgBox("Please enter a range direction", MsgBoxStyle.Information, "PLSS")
            Return
        Else 'If everything accounted for, move on.

        End If


        MsgBox("Finding your selection....Please wait", MsgBoxStyle.Information, "PLSS")

        Dim queryFilter As ESRI.ArcGIS.Geodatabase.IQueryFilter = New ESRI.ArcGIS.Geodatabase.QueryFilterClass
        queryFilter.WhereClause = SrchStr

        ' Invalidate only the selection cache. Flag the original selection
        pActiveView.PartialRefresh(ESRI.ArcGIS.Carto.esriViewDrawPhase.esriViewGeoSelection, Nothing, Nothing)

        ' Perform the selection
        featureSelection.SelectFeatures(queryFilter, ESRI.ArcGIS.Carto.esriSelectionResultEnum.esriSelectionResultNew, False)

        ' Flag the new selection
        pActiveView.PartialRefresh(ESRI.ArcGIS.Carto.esriViewDrawPhase.esriViewGeoSelection, Nothing, Nothing)


    End Sub

    Public Sub New(ByVal hook As Object)

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        Me.Hook = hook
    End Sub
    Private m_hook As Object
    ''' <summary>
    ''' Host object of the dockable window
    ''' </summary>
    Public Property Hook() As Object
        Get
            Return m_hook
        End Get
        Set(ByVal value As Object)
            m_hook = value
        End Set
    End Property
    ''' <summary>
    ''' Implementation class of the dockable window add-in. It is responsible for
    ''' creating and disposing the user interface class for the dockable window.
    ''' </summary>
    Public Class AddinImpl
        Inherits ESRI.ArcGIS.Desktop.AddIns.DockableWindow

        Private m_windowUI As SLO_DockableWindow1

        Protected Overrides Function OnCreateChild() As System.IntPtr
            m_windowUI = New SLO_DockableWindow1(Me.Hook)
            Return m_windowUI.Handle
        End Function

        Protected Overrides Sub Dispose(ByVal Param As Boolean)
            If m_windowUI IsNot Nothing Then
                m_windowUI.Dispose(Param)
            End If

            MyBase.Dispose(Param)
        End Sub

    End Class
    Private Sub Sec_cbo_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Sec_cbo.SelectedIndexChanged
        sSection = Sec_cbo.Text
    End Sub
    Private Sub Twp_cbo_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Twp_cbo.SelectedIndexChanged
        sTwp = Twp_cbo.Text
    End Sub
    Private Sub TwpD_cbo_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TwpD_cbo.SelectedIndexChanged
        sTwpD = TwpD_cbo.Text
    End Sub
    Private Sub Rng_cbo_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Rng_cbo.SelectedIndexChanged
        sRng = Rng_cbo.Text
    End Sub
    Private Sub RngD_cbo_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RngD_cbo.SelectedIndexChanged
        sRngD = RngD_cbo.Text
    End Sub


End Class
_______________________________________________________________________
Button.vb:

Public Class Find_PLSS_btn
    Inherits ESRI.ArcGIS.Desktop.AddIns.Button

    Public Sub New()

    End Sub

    Protected Overrides Sub OnClick()

        Dim dockWindow As ESRI.ArcGIS.Framework.IDockableWindow

        ' Only get/create the dockable window if it's not there
        If dockWindow Is Nothing Then
            Dim dockWinID As UID = New UIDClass()
            dockWinID.Value = My.ThisAddIn.IDs.SLO_DockableWindow1
            dockWindow = My.ArcMap.DockableWindowManager.GetDockableWindow(dockWinID)
        End If
        dockWindow.Show((Not dockWindow.IsVisible()))



        My.ArcMap.Application.CurrentTool = Nothing
    End Sub

    Protected Overrides Sub OnUpdate()
        Enabled = My.ArcMap.Application IsNot Nothing
    End Sub
End Class
0 Kudos
SteveFang
New Contributor III
Do you know which line is causing ArcMap to crash?  Most likely the culprit is in your button click routine.  If you don't know which line you can try the following to isolate the error.

(1) Use a try catch in your button click routine to capture the exception. 
(2) Set a break point on the catch of the try catch to debug it so you can identify the exception.
(3) If the exception is not clear then set a break point at the beginning of the routine and run it in debug mode to identify the line that is crashing ArcMap.
0 Kudos