how to open a project

Aug 13, 2011 at 1:09 PM

I tried to write a code to open a project(*.dspx) file, but it's not working. I tried to use sample from SpatialToolStrip.cs file and convert it to VB, not sure this should work though. I got following error message:

System.NullReferenceException was unhandled
Message=Object reference not set to an instance of an object.

This is the code I used:

 Private _applicationManager As AppManager

 Public Overridable Property ApplicationManager() As AppManager
        Get
            Return _applicationManager
        End Get
        Set(ByVal value As AppManager)
            _applicationManager = value
        End Set
    End Property

    Private Sub ToolStripMenuItem5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripMenuItem5.Click
        Contract.Requires(ApplicationManager IsNot Nothing, "ApplicationManager is null.")
        Dim dlg = New OpenFileDialog()
        If dlg.ShowDialog(Me) <> DialogResult.OK Then
            Return
        End If
        Dim fileName As String = dlg.FileName
        Try
            ApplicationManager.SerializationManager.OpenProject(fileName)       ' <<< debugger points to this line
        Catch generatedExceptionName As IOException
            MessageBox.Show(Me, "Could not open the specified map file " & fileName, "Error", MessageBoxButtons.OK, MessageBoxIcon.[Error])
        Catch generatedExceptionName As XmlException
            MessageBox.Show(Me, "Failed to read the specified map file " & fileName, "Error", MessageBoxButtons.OK, MessageBoxIcon.[Error])
        Catch generatedExceptionName As ArgumentException
            MessageBox.Show(Me, "Failed to read a portion of the specified map file " & fileName, "Error", MessageBoxButtons.OK, MessageBoxIcon.[Error])
        End Try
    End Sub

Any idea what might be the problem and how to rectify it?

May 19, 2012 at 2:36 PM

Hello everybody,

I'm just facing the same problem and wonder if anybody came up with a solution meanwhile.

Thanks

May 19, 2012 at 4:29 PM

Hello again,

the best answers are the ones given by oneself ...

Ok, the problem is that if you simply translate the code there is no connection between the file to open an the map-control of your application. So you have to provide the connection. This is the code I used to open a project-file, where mapMain is the name of the map-control:

Private Sub cmdProjektOeffnen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdProjektOeffnen.Click
        Dim objApp As DotSpatial.Controls.AppManager
        Dim objDlg As OpenFileDialog

        ' neues AppManager-Objekt erzeugen und an Map-Steuerelement binden
        objApp = New DotSpatial.Controls.AppManager
        objApp.Map = Me.mapMain

        ' Neues Datei-Dialog-Objekt anlegen und initialisieren
        objDlg = New OpenFileDialog()
        objDlg.Filter = objApp.SerializationManager.OpenDialogFilterText
        If objDlg.ShowDialog() <> DialogResult.OK Then
            Return
        End If
        Try
            'Den AppManager.SerializationManager zum Öffnen des Projektes verwenden
            objApp.SerializationManager.OpenProject(objDlg.FileName)
            objApp.Map.Invalidate()
        Catch
            MsgBox("Beim Laden des Projektes trat ein Fehler auf!", CType(MsgBoxStyle.OkOnly + MsgBoxStyle.Information, MsgBoxStyle), "idomeoMAP 2013")
        End Try
    End Sub