ArcObjects Library Reference  

CustomCommand

About the Browse for Python files Sample

[C#]

CustomCommand.cs

using ESRI.ArcGIS.Framework;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.CatalogUI;
using ESRI.ArcGIS.Catalog;
using ESRI.ArcGIS.ArcCatalog;
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;


namespace CustomGxFilter_CS
{
    public class CustomCommand : ESRI.ArcGIS.Desktop.AddIns.Button
    {
        #region Member Variables
        private IGxApplication m_pApp;
        #endregion

        public CustomCommand()
        {
        }

        protected override void OnClick()
        {
            // set
            m_pApp = (IGxApplication)CustomGxFilter_CS.ArcCatalog.Application;
            IGxCatalog pCat = null;
            IGxFileFilter pFileFilter = null;
            IEnumGxObject pSelection = null;
            IGxDialog pDlg = null;
            IGxObjectFilter pFilter = null;
            try
            {
                pDlg = new GxDialog();
                pCat = pDlg.InternalCatalog ;
                pFileFilter = pCat.FileFilter;
                if (pFileFilter.FindFileType("py") < 0)
                {
                    //enter the third parameter with the location of the icon as needed
                    pFileFilter.AddFileType("PY", "Python file", "");
                }

                pFilter = new CustomGxFilter_CS.CustomFilter();
                pDlg.ObjectFilter = pFilter;
                pDlg.Title = "Please select a .Py file";
                pDlg.AllowMultiSelect = true;
                pDlg.DoModalOpen(0, out pSelection);
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.ToString());
            }
            finally
            {
                pCat = null;
                pFileFilter = null;
                pSelection = null;
                pDlg = null;
                pFilter = null;
            }
        }

        protected override void OnUpdate()
        {
            Enabled = ArcCatalog.Application != null;
        }
    }
}

[Visual Basic .NET]

CustomCommand.vb

Imports ESRI.ArcGIS.Framework
Imports ESRI.ArcGIS.esriSystem
Imports ESRI.ArcGIS.Catalog
Imports ESRI.ArcGIS.CatalogUI

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

#Region "Member Variables"
    Private m_pApp As IGxApplication
#End Region

    Public Sub New()

    End Sub

    Protected Overrides Sub OnClick()
        '
        '  TODO: Sample code showing how to access button host
        '
        My.ArcCatalog.Application.CurrentTool = Nothing
        ' set
        m_pApp = My.ArcCatalog.Application
        Dim pCat As IGxCatalog, pFileFilter As IGxFileFilter, pSelection As IEnumGxObject = Nothing
        Dim pDlg As IGxDialog, pFilter As IGxObjectFilter
        'Dim pDlg As IGxDialog, pHandle As ESRI.ArcGIS.esriSystem.OLE_HANDLE, pFilter As IGxObjectFilter
        Try

            pDlg = New GxDialog
            pCat = pDlg.InternalCatalog
            pFileFilter = pCat.FileFilter
            If pFileFilter.FindFileType("py") < 0 Then
                'enter the third parameter with the location of the icon as needed
                pFileFilter.AddFileType("py", "Python file", "")
            End If

            pFilter = New CustomFilter
            pDlg.ObjectFilter = pFilter
            pDlg.Title = "Please select a .Py file"
            pDlg.AllowMultiSelect = True
            pDlg.DoModalOpen(0, pSelection)
        Catch ex As Exception
            MsgBox(ex.ToString())
        Finally
            pCat = Nothing
            pFileFilter = Nothing
            pSelection = Nothing
            pDlg = Nothing
            pFilter = Nothing
        End Try
    End Sub

    Protected Overrides Sub OnUpdate()
        Enabled = My.ArcCatalog.Application IsNot Nothing
    End Sub
End Class