3

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

Is there a similar to IAddDataDialog, but for workspaces?

11

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);
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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