0

I am trying to build a USerControl that contains a GMapControl. When I place the GMapControl directly on the Form, then it works as expected. If I however place the GMapControl on a UserControl, and then add that UserControl to the Form, I get errors.

For example:

My UserControl, Map.cs:

public Map()
        {
            InitializeComponent();

            gMapControl1.MapProvider = GMapProviders.OpenStreetMap;
            gMapControl1.Position = new PointLatLng(54.6961334816182, 25.2985095977783);
            gMapControl1.MinZoom = 1;
            gMapControl1.MaxZoom = 24;
            gMapControl1.Zoom = 9;

            top = new GMapOverlay("1");
            objects = new GMapOverlay("objects");
            routes = new GMapOverlay("routes");
            polygons = new GMapOverlay("polygons");

            gMapControl1.Overlays.Add(routes);
            gMapControl1.Overlays.Add(polygons);
            gMapControl1.Overlays.Add(objects);
            gMapControl1.Overlays.Add(top);

            gMapControl1.OnMarkerClick += new MarkerClick(gMapControl1_OnMarkerClick);
            gMapControl1.OnPolygonClick += new PolygonClick(gMapControl1_OnPolygonClick);
        }

Then I add this UserControl to my Form by dragging it on there. Then I get an Exception:

Failed to create component 'Map'. The error message follows: 'System.MissingMethodException: Method not found: 'Void GMap.NET.WindowsForms.GMapControl.set_MapProvider(GMap.NET, MapProviders.GMapProvider)'. at OpenStreetMapTest.Map..ctor()'

If I have the same code that I have in the UserControl Map inside a Form, then no errors. Also, the set_MapProvider exists and works if I don't put the GMapControl inside a UserControl.

Any ideas?

Ted
  • 19,727
  • 35
  • 96
  • 154

2 Answers2

1

Decompile the code and see what the Map construtor is doing. Maybe it's locating some method by reflection. Can't think why else you'd get a MissingMethodException dependong on where the control is sitting.

On DesignMode suggestion, that property just flat out doesn't work for nested user controls which is really frustrating. However, you can use the following work around (this property would be in a UserControlBase class from which you would inherit)

Simply check IsDesignerHosted instead of IsDesignMode.

        /// <summary>
        /// Indicates if the code is being run in the context of the designer
        /// </summary>
        /// <remarks>
        /// <see cref="Component.DesignMode"/> always returns false for nested controls. This is one
        /// of the suggested work arounds here: http://stackoverflow.com/questions/34664/designmode-with-controls
        /// </remarks>
        public bool IsDesignerHosted
        {
            get
            {
                Control ctrl = this;

                while (ctrl != null)
                {
                    if ((ctrl.Site != null) && ctrl.Site.DesignMode)
                        return true;
                    ctrl = ctrl.Parent;
                }
                return false;
            }
        }
Steven P
  • 1,956
  • 1
  • 16
  • 17
  • Thx, that worked a lot better! I was playing around a bit with different ways to check "isDesignMode", but the only one that worked was your solution =) – Ted Mar 21 '12 at 08:40
0

you should wrap everything inside the if ( !DesignMode )

eg.

Map()
{
    InitializeComponent();

    if ( !DesignMode )
    {
        gMapControl1.MapProvider = GMapProviders.OpenStreetMap;
        gMapControl1.Position = new PointLatLng(54.6961334816182, 25.2985095977783);
        gMapControl1.MinZoom = 1;
        gMapControl1.MaxZoom = 24;
        gMapControl1.Zoom = 9;

        top = new GMapOverlay("1");
        objects = new GMapOverlay("objects");
        routes = new GMapOverlay("routes");
        polygons = new GMapOverlay("polygons");

        gMapControl1.Overlays.Add(routes);
        gMapControl1.Overlays.Add(polygons);
        gMapControl1.Overlays.Add(objects);
        gMapControl1.Overlays.Add(top);

        gMapControl1.OnMarkerClick += new MarkerClick(gMapControl1_OnMarkerClick);
        gMapControl1.OnPolygonClick += new PolygonClick(gMapControl1_OnPolygonClick);
    }
}
Mladen Macanović
  • 1,099
  • 7
  • 17
  • "Another issue of the DesignMode property is that it doesn't work inside the constructor of a UserControl or Form!" http://dotnetfacts.blogspot.se/2009/01/identifying-run-time-and-design-mode.html ... – Ted Mar 20 '12 at 20:09