Select to view content in your preferred language

VB.Net - Interacting with Form in ArcMap

825
4
08-03-2010 07:59 AM
NeedHelp
Emerging Contributor
I've read countless posts and other info...however I just can't wrap my head around how to have a form interact with ArcMap.
My Form has 2 list boxes.
Box #1 - lists themes in View (works fine, cuz box gets populated before form is shown)
Box #2 - lists the fields from the theme selected in box #1 (Don't know how to do this!!!)

On form load, I seem to run into issues when I run GetActiveView,GetMxDocument, etc...

Is anyone able to attach this type of small (...and I hope simple)project, to help me out??

Thanks,
0 Kudos
4 Replies
KenBuja
MVP Esteemed Contributor
In this example, I have a form with two comboboxes. The first (cboPointLayer) contains the point layers in the view and the second (cboField) contains the fields of the selected layer. The Form_Initialize function populates cboPointLayer and when the user selects a layer, the subroutine cboPointLayer_SelectedIndexChanged populates cboField.

Public Class SelectionForm

  Private m_App As ESRI.ArcGIS.Framework.IApplication
  Private pMxDoc As ESRI.ArcGIS.ArcMapUI.IMxDocument
  Private pEnumLayers As ESRI.ArcGIS.Carto.IEnumLayer
  Private pFLayer As ESRI.ArcGIS.Carto.IFeatureLayer

  Friend Function Form_Initialize(ByVal m_application As ESRI.ArcGIS.Framework.IApplication) As Boolean

    Dim pLayer As ESRI.ArcGIS.Carto.ILayer
    Dim pFLayer As ESRI.ArcGIS.Carto.IFeatureLayer2

    m_App = m_application
    pMxDoc = CType(m_App.Document, ESRI.ArcGIS.ArcMapUI.IMxDocument)

    cboPointLayer.Items.Clear()
    cboField.Items.Clear()

    If pMxDoc.FocusMap.LayerCount > 0 Then
      pEnumLayers = pMxDoc.FocusMap.Layers
      pLayer = pEnumLayers.Next
      Do Until pLayer Is Nothing
        If pLayer.Valid Then
          If TypeOf pLayer Is ESRI.ArcGIS.Carto.IFeatureLayer Then
            pFLayer = pLayer
            If pFLayer.ShapeType = ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPoint Then
              cboPointLayer.Items.Add(pLayer.Name)
            End If
          End If
        End If
        pLayer = pEnumLayers.Next
      Loop
    End If

    Return True

  End Function

  Private Sub cboPointLayer_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboPointLayer.SelectedIndexChanged

    Dim pLayer As ESRI.ArcGIS.Carto.ILayer
    Dim pFields As ESRI.ArcGIS.Geodatabase.IFields

    Try
      pEnumLayers.Reset()
      cboField.Items.Clear()

      pLayer = pEnumLayers.Next
      Do Until pLayer Is Nothing
        If pLayer.Name = cboPointLayer.Text Then
          pPointFLayer = New ESRI.ArcGIS.Carto.FeatureLayer
          pPointFLayer = pLayer
          Exit Do
        End If
        pLayer = pEnumLayers.Next
      Loop

      pFields = pPointFLayer.FeatureClass.Fields

      For i As Integer = 0 To pFields.FieldCount - 1
        If pFields.Field(i).Type < 6 Then 'this adds only numeric, date, and string fields, not geometry, Blobs, raster, GUID, Global ID, or XML fields.
          cboField.Items.Add(pFields.Field(i).Name)
          ListBox1.Items.Add(pFields.Field(i).Name)
        End If
      Next
      EnableRun()
    Catch ex As Exception
     Messagebox.Show(ex.message, "Point Layer")
    End Try

  End Sub
End Class
0 Kudos
NeedHelp
Emerging Contributor
Thanks for the quick reply Ken!!

I created a new Toolbar,command and Form.
The Form I created was called Form1, and has the same controls and names as your 2 combo boxes, and list boxes from your example.

I c&p your code. However the combo boxes don't populate (point layers are in the TOC).
(modified your code from "Friend Fucntion Form_Initialize" to "Friend Function Form1_Initialize")

What is the code you used to call the form?
Mine is:
Public Overrides Sub OnClick()
        Dim theform As New Form1
        theform.ShowDialog()
    End Sub

Again thanks for any help/assistance you can provide.
0 Kudos
KenBuja
MVP Esteemed Contributor
If you notice in the Form_Initialize code, it's expecting IApplication to be passed to it. Here's the OnCreate and OnClick subroutines. I'm also using a a class that sets ArcMap as the form's parent, as mentioned in this thread.


  Private m_application As IApplication

  Public Overrides Sub OnCreate(ByVal hook As Object)
    If Not hook Is Nothing Then
      m_application = CType(hook, IApplication)

      'Disable if it is not ArcMap
      If TypeOf hook Is IMxApplication Then
        MyBase.m_enabled = True
      Else
        MyBase.m_enabled = False
      End If
    End If

    ' TODO:  Add other initialization code
  End Sub

  Public Overrides Sub OnClick()

    Dim SelectionForm As New myApp.SelectionForm

    Try
      If Not SelectionForm.Form_Initialize(m_application) Then Exit Sub
      SelectionForm.ShowDialog(New Win32HWNDWrapper(m_application.hWnd))
    Catch ex As Exception
      MessageBox.Show(ex.Message, "OnClick")
    End Try
  End Sub
0 Kudos
NeedHelp
Emerging Contributor
That was exactly what I needed!!!

Thank you very very much Ken.

ps. I think I owe you at least one 'beverage' 🙂
0 Kudos