ArcObjects Library Reference  

OpenSimplePointDlg

About the Simple point plug-in data source Sample

[C#]

OpenSimplePointDlg.cs

using System;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geodatabase;

namespace ESRI.ArcGIS.Samples.SimplePointPlugin
{
  public partial class OpenSimplePointDlg : Form
  {
    #region class memebers
    private IHookHelper m_hookHelper = null;
    IWorkspace m_workspace = null;
    #endregion

    #region class constructor
    public OpenSimplePointDlg(IHookHelper hookHelper)
    {
      if (null == hookHelper)
        throw new Exception("Hook helper is not initialize");

      InitializeComponent();
      
      m_hookHelper = hookHelper;
    }
    #endregion

    #region UI event handlers
    private void btnOpenDataSource_Click(object sender, EventArgs e)
    {
      m_workspace = OpenPlugInWorkspace();

      ListFeatureClasses();
    }

    private void lstDeatureClasses_DoubleClick(object sender, EventArgs e)
    {
      this.Hide();
      OpenDataset();
      this.Close();
    }

    private void btnOK_Click(object sender, EventArgs e)
    {
      this.Hide();
      OpenDataset();
      this.Close();
    }

    private void btnCancel_Click(object sender, EventArgs e)
    {
      this.Close();
    }
    #endregion

    #region private methods
    private string GetFileName()
    {
      OpenFileDialog dlg = new OpenFileDialog();
      dlg.Filter = "Simple Point (*.csp)|*.csp";
      dlg.Title = "Open Simple Point file";
      dlg.RestoreDirectory = true;
      dlg.CheckPathExists = true;
      dlg.CheckFileExists = true;
      dlg.Multiselect = false;

      DialogResult dr = dlg.ShowDialog();
      if (DialogResult.OK == dr)
        return dlg.FileName;

      return string.Empty;
    }

    private IWorkspace OpenPlugInWorkspace()
    {
      try
      {
        string path = GetFileName();
        if (string.Empty == path)
          return null;

        //update the path textbox
        txtPath.Text = path;
        
        //get the type using the ProgID
        Type t = Type.GetTypeFromProgID("esriGeoDatabase.SimplePointPluginWorkspaceFactory");
        //Use activator in order to create an instance of the workspace factory
        IWorkspaceFactory workspaceFactory = Activator.CreateInstance(t) as IWorkspaceFactory;

        //open the workspace
        return workspaceFactory.OpenFromFile(System.IO.Path.GetDirectoryName(path), 0);
      }
      catch (Exception ex)
      {
        System.Diagnostics.Trace.WriteLine(ex.Message);
        return null;
      }
    }

    private void ListFeatureClasses()
    {
      lstDeatureClasses.Items.Clear();

      if (null == m_workspace)
        return;

      IEnumDatasetName datasetNames = m_workspace.get_DatasetNames(esriDatasetType.esriDTAny);
      datasetNames.Reset();
      IDatasetName dsName;
      while ((dsName = datasetNames.Next()) != null)
      {
        lstDeatureClasses.Items.Add(dsName.Name);
      }

      //select the first dataset on the list
      if (lstDeatureClasses.Items.Count > 0)
      {
        lstDeatureClasses.SelectedIndex = 0;
        lstDeatureClasses.Refresh();
      }
    }

    private void OpenDataset()
    {
      try
      {
        if (null == m_hookHelper || null == m_workspace)
          return;

        if (string.Empty == (string)lstDeatureClasses.SelectedItem)
          return;

        //get the selected item from the listbox
        string dataset = (string)lstDeatureClasses.SelectedItem;

        //cast the workspace into a feature workspace
        IFeatureWorkspace featureWorkspace = m_workspace as IFeatureWorkspace;
        if (null == featureWorkspace)
          return;

        //get a featureclass from the workspace
        IFeatureClass featureClass = featureWorkspace.OpenFeatureClass(dataset);

        //create a new feature layer and add it to the map
        IFeatureLayer featureLayer = new FeatureLayerClass();
        featureLayer.Name = featureClass.AliasName;
        featureLayer.FeatureClass = featureClass;
        m_hookHelper.FocusMap.AddLayer((ILayer)featureLayer);
      }
      catch (Exception ex)
      {
        System.Diagnostics.Trace.WriteLine(ex.Message);
      }
    }
    #endregion
  }
}
[Visual Basic .NET]

OpenSimplePointDlg.vb

Imports Microsoft.VisualBasic
Imports System
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Text
Imports System.Windows.Forms
Imports ESRI.ArcGIS.Controls
Imports ESRI.ArcGIS.Carto
Imports ESRI.ArcGIS.Geodatabase

Partial Public Class OpenSimplePointDlg
  Inherits Form
#Region "class members"
    Private m_hookHelper As IHookHelper = Nothing
    Private m_workspace As IWorkspace = Nothing
#End Region

#Region "class constructor"
  Public Sub New(ByVal hookHelper As IHookHelper)
    If Nothing Is hookHelper Then
      Throw New Exception("Hook helper is not initialize")
    End If

    InitializeComponent()

    m_hookHelper = hookHelper
  End Sub
#End Region

#Region "UI event handlers"
  Private Sub btnOpenDataSource_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnOpenDataSource.Click
    m_workspace = OpenPlugInWorkspace()

    ListFeatureClasses()
  End Sub

  Private Sub lstDeatureClasses_DoubleClick(ByVal sender As Object, ByVal e As EventArgs) Handles lstDeatureClasses.DoubleClick
    Me.Hide()
    OpenDataset()
    Me.Close()
  End Sub

  Private Sub btnOK_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnOK.Click
    Me.Hide()
    OpenDataset()
    Me.Close()
  End Sub

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

#Region "private methods"
  Private Function GetFileName() As String
    Dim dlg As OpenFileDialog = New OpenFileDialog()
    dlg.Filter = "Simple Point (*.csp)|*.csp"
    dlg.Title = "Open Simple Point file"
    dlg.RestoreDirectory = True
    dlg.CheckPathExists = True
    dlg.CheckFileExists = True
    dlg.Multiselect = False

    Dim dr As DialogResult = dlg.ShowDialog()
    If System.Windows.Forms.DialogResult.OK = dr Then
      Return dlg.FileName
    End If

    Return String.Empty
  End Function

  Private Function OpenPlugInWorkspace() As IWorkspace
    Try
      Dim path As String = GetFileName()
      If String.Empty = path Then
        Return Nothing
      End If

      'update the path textbox
      txtPath.Text = path

      'get the type using the ProgID
      Dim t As Type = Type.GetTypeFromProgID("esriGeoDatabase.SimplePointPluginWorkspaceFactory")
      'Use activator in order to create an instance of the workspace factory
      Dim workspaceFactory As IWorkspaceFactory = TryCast(Activator.CreateInstance(t), IWorkspaceFactory)

      'open the workspace
      Return workspaceFactory.OpenFromFile(System.IO.Path.GetDirectoryName(path), 0)
    Catch ex As Exception
      System.Diagnostics.Trace.WriteLine(ex.Message)
      Return Nothing
    End Try
  End Function

  Private Sub ListFeatureClasses()
    lstDeatureClasses.Items.Clear()

    If Nothing Is m_workspace Then
      Return
    End If

    Dim datasetNames As IEnumDatasetName = m_workspace.DatasetNames(esriDatasetType.esriDTAny)
    datasetNames.Reset()
    Dim dsName As IDatasetName

    dsName = datasetNames.Next()
    Do While Not dsName Is Nothing
      lstDeatureClasses.Items.Add(dsName.Name)
      dsName = datasetNames.Next()
    Loop

        'select the first dataset on the list
    If lstDeatureClasses.Items.Count > 0 Then
      lstDeatureClasses.SelectedIndex = 0
      lstDeatureClasses.Refresh()
    End If
  End Sub

  Private Sub OpenDataset()
    Try
      If Nothing Is m_hookHelper OrElse Nothing Is m_workspace Then
        Return
      End If

      If String.Empty Is CStr(lstDeatureClasses.SelectedItem) Then
        Return
      End If

      'get the selected item from the listbox
      Dim dataset As String = CStr(lstDeatureClasses.SelectedItem)

      'cast the workspace into a feature workspace
      Dim featureWorkspace As IFeatureWorkspace = TryCast(m_workspace, IFeatureWorkspace)
      If Nothing Is featureWorkspace Then
        Return
      End If

      'get a featureclass from the workspace
      Dim featureClass As IFeatureClass = featureWorkspace.OpenFeatureClass(dataset)

      'create a new feature layer and add it to the map
      Dim featureLayer As IFeatureLayer = New FeatureLayerClass()
      featureLayer.Name = featureClass.AliasName
      featureLayer.FeatureClass = featureClass
      m_hookHelper.FocusMap.AddLayer(CType(featureLayer, ILayer))
    Catch ex As Exception
      System.Diagnostics.Trace.WriteLine(ex.Message)
    End Try
  End Sub
#End Region
End Class