Select to view content in your preferred language

Showing Windows Forms with an Add-in

5273
8
11-16-2010 11:39 AM
JimCser
Deactivated User
Hello-
My existing ArcMap 10 DLL, created in Visual C# 2008, creates a button which shows a Windows Form containing a ListBox, a few buttons, and a DataGridView control.  Is it possible to implement this within the new Add-In environment?

Thanks,
Jim Cser
0 Kudos
8 Replies
JeffreyHamblin
Occasional Contributor
Hello-
My existing ArcMap 10 DLL, created in Visual C# 2008, creates a button which shows a Windows Form containing a ListBox, a few buttons, and a DataGridView control.  Is it possible to implement this within the new Add-In environment?


Yes, most definitely.

For a simple test, just run the ESRI Add-in Wizard for a new project and select the button type. Once the new empty project is initiated, add a form to the project. In the OnClick() method the wizard created in the button class, create and show your form.

For example:

public class YourButton : ESRI.ArcGIS.Desktop.AddIns.Button
    {
        public YourButton()
        {
        }

        protected override void OnClick()
        {
            frmYourForm yourForm= new frmYourForm();
            yourForm.Text = "Your App Name";
            yourForm.ShowDialog();   
        }
    }


-Jeff
0 Kudos
JimCser
Deactivated User
Jeff-
That worked great, thanks!  I could use my C# code virtually as-is, as long as I referenced the correct libraries. 

The only required code change was how I declared the IMXDocument.  Rather than defining an IApplication object, I instead do:
IMxDocument pMxDoc = ArcMap.Document;


Regards,
Jim
0 Kudos
SamC
by
Emerging Contributor
Hi all,

I'm trying to migrate some code I wrote for ArcMap 9.3.1 and have run into difficultes. I would like to use a Button add-in to load a windows form which displays some details of the open map document. This code worked previously, however now I'm getting an error I don't know how to rectify. It occurs when i'm trying to switch interfaces from IMxDocument to IDocument. This is my VB.NET code that produces the error:

Private Sub frmMapDetails_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Me.TopMost = True
        Dim pmxDocument As IMxDocument
        pmxDocument = My.ArcMap.Document
        Dim pIdoc As IDocument

The error I get is:

'IDocument' is not accessible in this context because it is 'Friend'.

This has stumped me, though as I'm new to coding/development that's not particularly difficult!! Am sure I must be missing something obvious.

Thanks

Sam
0 Kudos
JeffreyHamblin
Occasional Contributor
Hi Sam,

There is more than one way to get at the IDocument interface:

Directly, from an IApplication property:
Dim pDocument As IDocument = ArcMap.Application.Document

or by casting the IMxDocument
Dim pmxDocument As IMxDocument = ArcMap.Document
Dim pDocument As IDocument = TryCast(pmxDocument, IDocument)

If that doesn't help, post a more complete code sample.


I recommend that you take a look at the VB.NET coding samples found here:
http://help.arcgis.com/en/sdk/10.0/arcobjects_net/conceptualhelp/index.html
I have found them very helpful getting familiar with the new Add-In coding patterns.

(caveat: I'm working in C#, so I can't test my VB.NET code snips. But they should work.)

-Jeff
0 Kudos
SamC
by
Emerging Contributor
Hi Jeff,

Thanks for the reply, I'm still getting this error though. I've even tried starting afresh. Currently all i'm trying to achieve is the creation of a button add-in that displays a form containing some details about the currently open map document.

My code is:

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

Public Sub New()

End Sub

Protected Overrides Sub OnClick()

Dim testfrm = New frmMxdData
testfrm.Show()

My.ArcMap.Application.CurrentTool = Nothing
End Sub

Protected Overrides Sub OnUpdate()
Enabled = My.ArcMap.Application IsNot Nothing
End Sub
End Class
Code for the windows form:

Imports ESRI.ArcGIS.ArcMap
Imports ESRI.ArcGIS.esriSystem
Imports ESRI.ArcGIS.ArcMapUI
Imports ESRI.ArcGIS.Framework
Imports ESRI.ArcGIS.SystemUI
Imports ESRI.ArcGIS.Desktop

Public Class frmMxdData



Private Sub frmMxdData_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

Dim pmxDocument As IMxDocument
pmxDocument = My.ArcMap.Document
Dim pIdoc As IDocument
pIdoc = pmxDocument
tbMapTitle.Text = (pIdoc.Title) 'Populate text box with map title

End Sub

End Class
The error:

Error 3 'IDocument' is not accessible in this context because it is 'Friend'.

Intellisense gives me the IDocument option, but as soon as it is entered into the code it is flagged as an error irrespective of the method I use to get to it.

Thanks,

Sam
0 Kudos
EdgarBejarano
Deactivated User
SCC80, try two things. 

One is to fully-qualify the IDocument with the namespace:
ESRI.ArcGIS.Framework.IDocument

The other is to dimension the object variable as a module-level variable rather than a local variable.

Do you still see the compile error 'IDocument' is not accessible in this context because it is 'Friend'.?
0 Kudos
EdgarBejarano
Deactivated User
The following article may give some clues to help you:
http://support.microsoft.com/kb/814319
0 Kudos
SamC
by
Emerging Contributor
Hi Edgar,

Fully-qualifying IDocument with the namespace solved the error. My code now reads:

        Dim pmxDocument As IMxDocument
        pmxDocument = My.ArcMap.Document
        Dim pIdoc As ESRI.ArcGIS.Framework.IDocument
        pIdoc = pmxDocument
        tbMapTitle.Text = (pIdoc.Title)

Many thanks to you and Jeff for your help, it is much appreciated.

Regards,

Sam
0 Kudos