Map Content Updates
frmFileGDB.vb
' Copyright 2011 ESRI
' 
' All rights reserved under the copyright laws of the United States
' and applicable international laws, treaties, and conventions.
' 
' You may freely redistribute and use this sample code, with or
' without modification, provided you include the original copyright
' notice and use restrictions.
' 
' See the use restrictions.
' 

Imports Microsoft.VisualBasic
Imports System
Imports System.Linq
Imports System.Windows.Forms

Imports ESRI.ArcGISExplorer.Data
Imports ESRI.ArcGISExplorer.Mapping

Partial Public Class frmFileGDB
    Inherits Form
    Public Sub New()
        InitializeComponent()
    End Sub

    Private Sub btnBrowse_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnBrowse.Click
        'Routine for adding shapefile data to the map.  Begin by browsing for the data.
        Dim fdlg As FolderBrowserDialog = New FolderBrowserDialog()
        fdlg.ShowNewFolderButton = False
        fdlg.Description = "Find File Geodatabase"

        'If a shapefile is found, then create a FeatureLayer based on it, add symbology,
        'add the data to the map, and then zoom in.
        If fdlg.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
            txtDatabase.Text = fdlg.SelectedPath
        End If
    End Sub

    Private Sub btnFind_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnFind.Click
        If txtDatabase.Text = "" Then
            MessageBox.Show("You need to specify a database!!", "No Database", MessageBoxButtons.OK)
            Return
        End If

        'If we have a database, then try to connect to it
        Dim fileGDB As Geodatabase = Nothing
        Try
            fileGDB = New Geodatabase(txtDatabase.Text)
        Catch e1 As Exception
            MessageBox.Show("Could not connect to database!!", "Could not connect", MessageBoxButtons.OK)
            Return
        End Try

        If fileGDB Is Nothing Then
            MessageBox.Show("Could not connect to database!!", "Could not connect", MessageBoxButtons.OK)
            Return
        End If

        'if we are this far, then we've connected and we need to create a tree view of the feature classes
        tvwFeatureClasses.Nodes.Clear()
        'Add the Geodatabase Folders (Feature Datasets) and their contents first
        For Each folder As GeodatabaseFolder In fileGDB.GetGeodatabaseFolders()
            Dim folderNode As TreeNode = tvwFeatureClasses.Nodes.Add(folder.Name, folder.Name)
            For Each table As Table In folder.GetTables()
                folderNode.Nodes.Add(table.Name, table.Name)
            Next table
        Next folder
        'Add standalone tables second
        For Each table2 As Table In fileGDB.GetTables(TableDiscoveryOptions.Spatial, False)
            tvwFeatureClasses.Nodes.Add(table2.Name, table2.Name)
        Next table2
    End Sub

    Private Sub btnCancel_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnCancel.Click
        Me.Close()
    End Sub

    Private Sub btnOK_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnOK.Click
        If tvwFeatureClasses.Nodes.Count = 0 Then
            MessageBox.Show("Nothing to add!!", "no data", MessageBoxButtons.OK)
            Return
        End If

        If tvwFeatureClasses.SelectedNode Is Nothing Then
            MessageBox.Show("Please select a table in the list!", "no data", MessageBoxButtons.OK)
            Return
        End If

        'If we have a selection from the tree view, then we are ready to add it to the map

        Dim dataSrcProps As DataSourceProperties = New DataSourceProperties(txtDatabase.Text, tvwFeatureClasses.SelectedNode.Text)
        Dim fLayer As FeatureLayer = New FeatureLayer(dataSrcProps)
        fLayer.Connect()

        If Not fLayer Is Nothing Then
            If fLayer.Name <> "" Then
                'if we've created a layer, then add it to the map contents collection and zoom
                Dim mapDisp As MapDisplay = ESRI.ArcGISExplorer.Application.Application.ActiveMapDisplay
                mapDisp.Map.ChildItems.Add(fLayer)
                mapDisp.ZoomTo(fLayer.Extent)
            End If
        End If

        'close the form
        Me.Close()
    End Sub

    Private Sub tvwFeatureClasses_AfterSelect(ByVal sender As Object, ByVal e As TreeViewEventArgs) Handles tvwFeatureClasses.AfterSelect
        If Not tvwFeatureClasses.SelectedNode Is Nothing Then
            btnOK.Enabled = True
        Else
            btnOK.Enabled = False
        End If
    End Sub
End Class