// Called when current map is closed to ensure any active listeners are removed.
        public void ClearListeners()
        {
            TileMapEditor parentForm = this.MdiParent as TileMapEditor;

            // assign to event listeners
            parentForm.OnAddSpriteSheet -= AddNewSheet;
            parentForm.OnFillPallette   -= PopulatePalette;
            parentForm.OnEditMapTile    -= EditMapTile;
        }
        // Imports the current spritesheet and its settings into the active map.
        private void ImportButton_Click(object sender, EventArgs e)
        {
            if (Spritesheet == null)
            {
                return;
            }
            TileMapEditor parent = this.MdiParent as TileMapEditor;

            parent.AddSheet(Spritesheet);
            this.Close();
        }
        } = false;                                                          // check to see if the user is clicked ad dragging there mouse arround the map area to paint.

        public MapEditor(TileMapEditor parentForm)
        {
            this.MdiParent = parentForm;
            InitializeComponent();
            MapPanel.HorizontalScroll.Enabled = true;
            MapPanel.VerticalScroll.Enabled   = true;

            // assign to event listeners
            parentForm.OnAddSpriteSheet += AddNewSheet;
            parentForm.OnFillPallette   += PopulatePalette;
            parentForm.OnEditMapTile    += EditMapTile;
        }
 // On clicking a tile on the map if there is a current tile then the tle i changed to the current tile.
 void OnTileClick(object sender, MouseEventArgs e)
 {
     try
     {
         if (CurrentTile != null)
         {
             Point scrolledPoint = new Point(e.X - MapPanel.AutoScrollPosition.X,
                                             e.Y - MapPanel.AutoScrollPosition.Y);
             int           tile   = ((int)(scrolledPoint.X / size + scrolledPoint.Y / size * mapWidth));
             TileMapEditor parent = this.MdiParent as TileMapEditor;
             parent.tool.PaintTiles(ref map, mapWidth, tile, CurrentTile);
         }
     }
     catch (Exception ex)
     {
         MessageBox.Show("Error: " + ex.GetType());
     }
 }