OsmSharp / ui

The UI components.
http://osmsharp.com/
GNU General Public License v2.0
139 stars 91 forks source link

System.NullReferenceException in MapControl at design time #277

Open OgreTransporter opened 8 years ago

OgreTransporter commented 8 years ago

I want to display an OSM map on a control, so I've downloaded OsmShap daily build. If I drag and drop a new MapControl to a WinForm, I'll get the following message:

System.NullReferenceException: Object reference not set to an instance of an object at OsmSharp.WinForms.UI.MapControl.OnPaint(PaintEventArgs e) in D:\OsmSharp\OsmSharp.UI\OsmSharp.WinForms.UI\MapControl.cs:205 at

The error appears because there is no map at design time, so a check for a valid map is needed. I've downloaded the source and created a workaround:

        protected override void OnPaint(PaintEventArgs e)
        {
            long ticksBefore = DateTime.Now.Ticks;

            var g = e.Graphics;

            if(this.Map == null)
            {
                g.FillRectangle(Brushes.White, 0, 0, this.Width, this.Height);
                Pen wred = (Pen)Pens.Red.Clone();
                wred.Width = 3.0f;
                g.DrawRectangle(wred, 1, 1, this.Width - 3, this.Height - 3);
                g.DrawLine(wred, 1, 1, this.Width - 3, this.Height - 3);
                g.DrawLine(wred, 1, this.Height - 3, this.Width - 3, 1);
                return;
            }