Me.Close() ArcMap crash

578
2
06-22-2012 04:14 AM
DaveCouture
New Contributor III
How can I close a Form without ArcMap crashing?

Example:

       
Imports ESRI.ArcGIS.esriSystem
Imports ESRI.ArcGIS.Geometry
Imports ESRI.ArcGIS.Geodatabase
Imports ESRI.ArcGIS.Carto
Imports System.IO
Imports Microsoft.VisualBasic
Imports System
Imports System.Windows.Forms


Public Class FindAddressForm
    Inherits System.Windows.Forms.Form

    Public Sub New()
        MyBase.New()

        InitializeComponent()
        ' Populate the street name ComboBox from text file
        Dim streetList As String
        streetList = "\\csjs186\CPCommon\ArcMap Add-ins\StreetNameList2.txt"

        If File.Exists(streetList) Then
            Dim r As IO.StreamReader
            r = New IO.StreamReader(streetList)
            While (r.Peek() > -1)
                ComboBox1.Items.Add(r.ReadLine.Trim)
            End While
            r.Close()
        Else
            MsgBox("Can't find " + streetList)
            Me.Close()
        End If

End Sub
0 Kudos
2 Replies
ThavitinaiduGulivindala
Occasional Contributor
You cant use Me.Close in New() method as the form is not yet created but getting initialised. You can call Me.Close only after displaying form using either Form.Show or Form.ShowDialog.

How can I close a Form without ArcMap crashing?

Example:

       
Imports ESRI.ArcGIS.esriSystem
Imports ESRI.ArcGIS.Geometry
Imports ESRI.ArcGIS.Geodatabase
Imports ESRI.ArcGIS.Carto
Imports System.IO
Imports Microsoft.VisualBasic
Imports System
Imports System.Windows.Forms


Public Class FindAddressForm
    Inherits System.Windows.Forms.Form

    Public Sub New()
        MyBase.New()

        InitializeComponent()
        ' Populate the street name ComboBox from text file
        Dim streetList As String
        streetList = "\\csjs186\CPCommon\ArcMap Add-ins\StreetNameList2.txt"

        If File.Exists(streetList) Then
            Dim r As IO.StreamReader
            r = New IO.StreamReader(streetList)
            While (r.Peek() > -1)
                ComboBox1.Items.Add(r.ReadLine.Trim)
            End While
            r.Close()
        Else
            MsgBox("Can't find " + streetList)
            Me.Close()
        End If

End Sub
0 Kudos
RichardWatson
Frequent Contributor
If a class cannot be constructed then you are expected to throw an exception.  Obviously the code calling this would have to catch it.

Another alternative is to not have a public constructor but rather a static method which returns an instance of the class (a class factory if you will).  In this case it would return null if it cannot be created.
0 Kudos