skip to the main content area of this page
Developer Resources - Code Example 1

Easy GIS .NET Desktop Edition Code Examples

Easy GIS .NET Web Edition Code Examples

Click here to view or download the API reference.

Click here to download sample Shapefiles.

Opening and Viewing a ShapeFile in a .NET Windows Application

This example shows how to open and display a shapefile in a .NET Windows Application using the EGIS.Controls.SFMap .NET Shapefile control.

Download Code [736K]

Instructions to Build and Run Example

  • This example requires .NET 2.0 or greater. You will need Visual Studio 2005/2008 or Visual C# 2005/2008 Express Edition installed to open the included solution file.
  • To build and run the project, extract the downloaded files and open the Example1 solution file. Build and run the project. Open the included world_adm0.shp shapefile and you should see a window as shown in Figure 1
screenshot of web example 1
Figure 1

Explanation of Example

The example uses the EGIS.Controls.SFMap to load and view a shapefile.

The example was created in less than 5 minutes and contains only a few lines of code as shown below

 private void miOpen_Click(object sender, EventArgs e)
 {
    if(ofdShapefile.ShowDialog(this) == DialogResult.OK)
    {
        try
        {
            OpenShapefile(ofdShapefile.FileName);
        }
        catch (Exception ex)
        {
            MessageBox.Show(this, "Error : " + ex.Message);
        }
    }
 }

 private void OpenShapefile(string path)
 {
    // clear any shapefiles the map is currently displaying
    this.sfMap1.ClearShapeFiles();
    
    // open the shapefile passing in the path, display name of the shapefile and
    // the field name to be used when rendering the shapes (we use an empty string
    // as the field name (3rd parameter) can not be null)
    this.sfMap1.AddShapeFile(path, "ShapeFile", "");
    
    // read the shapefile dbf field names and set the shapefiles's RenderSettings
    // to use the first field to label the shapes.
    EGIS.ShapeFileLib.ShapeFile sf = this.sfMap1[0];
    sf.RenderSettings.FieldName = sf.RenderSettings.DbfReader.GetFieldNames()[0];
 }
            

To Pan around the loaded map click and drag the mouse. To zoom in or zoom out of the map use the mouse wheel or double-click the left and right mouse buttons.

Download Code [736K]