Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I need to create a dialog that the user can select a proper Workspace.

Is there a similar to IAddDataDialog, but for workspaces?

share|improve this question

1 Answer 1

up vote 8 down vote accepted

Assuming you have desktop, you can use IGxDialog.

public static IWorkspace Browse4Workspace(int hwnd)
{
    IGxDialog gxDlg = new GxDialogClass();
    gxDlg.ObjectFilter = new GxFilterWorkspacesClass();
    gxDlg.AllowMultiSelect = false;
    IEnumGxObject workspaces;
    IWorkspace ws = null;
    if (gxDlg.DoModalOpen(hwnd, out workspaces) && workspaces != null)
    {
        IGxObject gxObj = workspaces.Next();
        if (gxObj is IGxDatabase)
        {
            IGxDatabase gxDB = gxObj as IGxDatabase;
            if (gxDB != null)
                ws = gxDB.Workspace;
        }
        else if (gxObj is IGxFolder)
        {
            //Todo: figure out how to decide which workspace to get for
            // a particular folder (shapefile, raster etc.)                    
            //IEnumName enumName = ((IGxFolder)gxObj).FileSystemWorkspaceNames;

            // for now just assume we want a shapefile ...
            IWorkspaceFactory wsf = new ESRI.ArcGIS.DataSourcesFile.ShapefileWorkspaceFactoryClass();
            ws = wsf.OpenFromFile(((IGxFile)gxObj).Path, hwnd);
        }
        Marshal.FinalReleaseComObject(workspaces);
    }
    gxDlg.InternalCatalog.Close();
    Marshal.FinalReleaseComObject(gxDlg);
    return ws;
}

private void testToolStripMenuItem_Click(object sender, EventArgs e)
{
    try
    {
        IWorkspace ws = Browse4Workspace(this.Handle.ToInt32());
        if (ws != null)
            MessageBox.Show(this, ws.PathName);
        else
            MessageBox.Show(this, "no workspace selected");
    }
    catch (Exception ex)
    {
        MessageBox.Show(this, ex.Message);
    }
}
share|improve this answer
    
thanks master kirk! –  George Aug 27 '10 at 16:26

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.