Opening forms from a button Addin for ArcGIS 10 on a 64-bit Win7 computer

3605
8
04-25-2012 07:45 AM
TimTeaford
New Contributor
I recently upgraded my laptop from a 32-bit Win7 operating system to a 64-bit Win7 operating system.  I have ArcGIS 10 and I am developing Add-ins for ArcMap using Visual Studio 2010.

On my 32-bit computer, I had no problems opening forms from any type of add-in. Now when I try to open a form, ArcMap immediately closes and I get the error screen that asks if I want to send the error reports to ESRI.

Here is what I am doing:

Dim myFrmMain As New frmMain
myFrmMain.Show()

Does anyone have any ideas as to why this does not work on a 64-bit computer?
0 Kudos
8 Replies
KenBuja
MVP Esteemed Contributor
I'm developing add-ins on a Win 7 64 bit computer and don't have any problems with opening forms. Do you have any code in the form initialization routine?
0 Kudos
TimTeaford
New Contributor
I'm developing add-ins on a Win 7 64 bit computer and don't have any problems with opening forms. Do you have any code in the form initialization routine?


Hi Ken, thanks for responding.  Here is the code in my class module for my button addin which shows how I am trying to open my form:

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

    Public Sub New()
        MsgBox("It made it into the ''New'' subroutine!", MsgBoxStyle.OkOnly, "Woo hoo!")
    End Sub

    Protected Overrides Sub OnClick()
        MsgBox("It made it into the ''OnClick'' subroutine!", MsgBoxStyle.OkOnly, "Woo hoo!")
        Call OpenFrmMain(True)
    End Sub

    Protected Overrides Sub OnUpdate()
        'Enabled = My.ArcMap.Application IsNot Nothing
        Enabled = True
    End Sub

    Public Sub OpenFrmMain(ByVal ShowYN As Boolean)
        MsgBox("It made it into the ''OpenFrmMain'' subroutine!", MsgBoxStyle.OkOnly, "Woo hoo!")
        Dim myFrmMain As frmMain = Nothing
        'myFrmMain = New frmMain            'when I uncomment this line it errors here
        If ShowYN = True Then
            myFrmMain.Show()                   'open as modeless window --> otherwise it errors on this line
            'myFrmMain_Fields.ShowDialog()  'open as modal window
        End If
    End Sub 'OpenFrmMain(x)

End Class
0 Kudos
KenBuja
MVP Esteemed Contributor
You'll need to instantiate a new form the line

myFrmMain = New frmMain

to get a new form to show...but your logic is correct. The next thing to look at is the code in your form. Do you have any code in the New sub? In my forms, I have a function that does all the initialization work to get the form ready to show (InitializeForm), such as adding all the feature layers in a map to a combobox. Here's an example:

Public Class CompatibilityForm

    Private pEnumLayers As ESRI.ArcGIS.Carto.IEnumLayer
    Private pLayer As ESRI.ArcGIS.Carto.ILayer2

    Public Function InitializeForm() As Boolean

        Try
            pEnumLayers = My.ArcMap.Document.FocusMap.Layers
            pLayer = pEnumLayers.Next

            Do Until pLayer Is Nothing
                If pLayer.Valid Then
                    If TypeOf pLayer Is ESRI.ArcGIS.Carto.IFeatureLayer Then cboLayer.Items.Add(pLayer.Name)
                End If
                pLayer = pEnumLayers.Next
            Loop

            Return True

        Catch ex As Exception
            System.Windows.Forms.MessageBox.Show(ex.ToString, "Compatibility Form: InitializeForm")
            Return False
        End Try

    End Function

' more form code

End Class


And I call it with the lines
        Dim NewCompForm As New CompatibilityForm

        If NewCompForm.InitializeForm Then NewCompForm.ShowDialog()
0 Kudos
NeilClemmons
Regular Contributor III
The first thing I would check is your project's Target Platform setting.  It needs to be set to x86.  By default, it's set to AnyCPU which will cause problems when you try to run your app on a 64-bit system.
0 Kudos
TimTeaford
New Contributor
Hi Ken, I added a InitializeForm function in my form and I am calling the function and opening the form exactly as you had suggested.

Here is my InitializeForm function in my form:

Public Class frmMain
    Public Function InitializeForm() As Boolean
        InitializeForm = False
        If My.ArcMap.Document.ActiveView IsNot My.ArcMap.Document.PageLayout Then
            InitializeForm = True
        End If
        If Me.Visible = False Then
            InitializeForm = True
        End If
    End Function
    'more code...
End Class


And here is how I am calling the function and opening the form:

Dim myFrmMain As New frmMain
If myFrmMain.InitializeForm = True Then myFrmMain.ShowDialog()


When stepping through the code in debug mode, the program gets to "Dim myFrmMain As New frmMain" and then for some reason it jumps to my form to one of my class-level variables. After stepping through about 3 of my class-level variables in my form, it shuts down ArcMap and then displays the "send error reports to ESRI" window.

I've tested some of my other add-ins that also open forms and they work just fine (created in VS2008).  They open the form with the code that I showed you -- the same code that I have been using for several years.  It just seems to be this one project.

About the "New" sub -- I did have code in that subroutine, but it also immediately caused ArcMap to shut down.  In the New sub, I was trying to set global variables for the IMxDocument and IApplication.

I appreciate the suggestions and any more that you might have will be greatly appreciated too.
0 Kudos
KenBuja
MVP Esteemed Contributor
Have you put a Try..Catch block in your  New sub code to see what's going wrong?
0 Kudos
TimTeaford
New Contributor
Have you put a Try..Catch block in your  New sub code to see what's going wrong?


Ken, I found out what was wrong with my program and finally got my form to open up.

First, let me say that my program doesn't just open one main form.  Once the main form is opened up, the user can press a button to open up a second form. I did what you suggested by adding a Try...Catch block and the error that was generated said something about missing resources for my second form.

So I compared my first form with the second form and found only one difference.  On the first form I had added an icon for the Icon property.  This created a *.resx file for the first form.  The second form had not been assigned an icon and so did not have a *.resx file.

To resolve my problem, I first just commented out any references to the second form.  That enabled me to open my first form.  So then to be able to open my second form, I uncommented the code and added an icon for the Icon property.  At this point, I was able to open the second form by pressing a button on the first form.

Here is my code I used to open the first form from the button add-in class module:

    Protected Overrides Sub OnClick()
        Try
            If Not myFrmMain Is Nothing Then
                If myFrmMain.IsHandleCreated = False Then
                    myFrmMain = New frmMain
                    myFrmMain.Show()
                End If
            End If
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub


And here is the code that used to open the second form from a button click event on the first form:

    'Class-level variable:
    Dim myFrmDesc As frmDesc

    Private Sub btnLayerDesc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLayerDesc.Click
        mListBox = Me.lstLayers
        Call OpenFrmDesc(True)
    End Sub

    Public Sub OpenFrmDesc(ByVal ShowYN As Boolean)
        Try
            myFrmDesc = New frmDesc
            If myFrmDesc.IsHandleCreated = False Then
                myFrmDesc.Show()
            End If
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub
0 Kudos
TimTeaford
New Contributor
To give credit where credit is due, both Neil and Ken gave me the pieces to answer my question.  Neil by suggesting that I change the target platform to x86 and Ken by giving me some sample code and reminding me about using Try...Catch blocks.
0 Kudos