ShowDialog() Error Parameter is not valid

Topics: Windows Forms
Feb 16, 2012 at 8:06 PM

I am sorry to disturb you all, but I didn't manage to open two times the windows form containing the GmapControl.

In my MainForm I have one button that leads to this event:

private void tSBGoogle_Click(object sender, EventArgs e)

GoogleForm google = new GoogleForm();

if (google.DialogResult == DialogResult.OK)

        label1Site.Visible = true;



the second time that I click on this button at the google.ShowDialog(); command appears an error "Parameter is not valid".

I do not know where to look to find the problem... I have tried to invoke google.Dispose() but it doesn't solve the problem... Can you give me some leads?

Thank you very much for your help!

Feb 16, 2012 at 8:40 PM

fix GoogleForm ;}

Apr 27, 2012 at 2:54 PM
Edited Apr 27, 2012 at 3:12 PM

Hi radioman! I have the same issue and problem is not in the win form. If I am not init Gmap control -I can open form many times without any error, but if I ones init those code at load form, close the form and open again the form - I got the same error.

                    System.Net.IPHostEntry e = System.Net.Dns.GetHostEntry("");
                    MainMap.Manager.Mode = AccessMode.CacheOnly;
                    MessageBox.Show("No internet connection available, going to CacheOnly mode.""GMap.NET - Test"MessageBoxButtons.OK, MessageBoxIcon.Warning);
                System.Net.WebProxy prx = new System.Net.WebProxy("", 8080);
                prx.UseDefaultCredentials = true;
                GMapProvider.WebProxy = prx;
                MainMap.MapProvider = GMapProviders.OpenStreetMap;
                MainMap.Position = new PointLatLng(40.744207000000000000, -73.845978000000000000);
                MainMap.MinZoom = 1;
                MainMap.MaxZoom = 24;
                MainMap.Zoom = 11;

please check the following error messages:
Parameter is not valid.

Stack:    at System.Drawing.Graphics.CheckErrorStatus(Int32 status)
   at System.Drawing.Graphics.DrawString(String s, Font font, Brush brush, RectangleF layoutRectangle, StringFormat format)
   at System.Drawing.Graphics.DrawString(String s, Font font, Brush brush, Single x, Single y)
   at GMap.NET.WindowsForms.GMapControl.OnPaintOverlays(Graphics g)
   at GMap.NET.WindowsForms.GMapControl.OnPaint(PaintEventArgs e)
   at System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer, Boolean disposeEventArgs)
   at System.Windows.Forms.Control.WmPaint(Message& m)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
   at System.Windows.Forms.ContainerControl.WndProc(Message& m)
   at System.Windows.Forms.UserControl.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
   at System.Windows.Forms.SafeNativeMethods.UpdateWindow(HandleRef hWnd)
   at System.Windows.Forms.Control.Update()
   at System.Windows.Forms.Control.Refresh()
   at GMap.NET.WindowsForms.GMapControl.Refresh()
   at GMap.NET.WindowsForms.GMapControl.ForceUpdateOverlays()
   at GMap.NET.WindowsForms.GMapControl.set_Position(PointLatLng value)
   at BEGDC.NYCETrack.Client.NYCMap.MyMap.InitMap() in C:\DEV\NTM\BEGDC.NYCETrack.Client\NYCMap\MyMap.cs:line 58

Apr 27, 2012 at 3:15 PM
Edited Apr 27, 2012 at 3:18 PM

it's not gmap related, child forms works perfect in modal and modalless forms, if you dispose form, you cant use it again

p.s. share demo project, and i'll fix it

Oct 19, 2012 at 6:58 AM

Hi radioman.

When I open the form for the first time, everything is ok, but when I open the next time I see the error "Parameter is not valid". Can I ask you to provide an example?. I know that the problem lies in the type of window, but I don't know how to solve it.

This is the code window with the map:

public partial class frmLatLng : Form
        public frmLatLng()
        private void frmLatLng_Load(object sender, EventArgs e)
            Cursor = Cursors.WaitCursor;
            Cursor = Cursors.Default;
        void ShowMap()
            Mapa.MapProvider = GMapProviders.GoogleSatelliteMap;
            Mapa.Position = new PointLatLng(50.9351787221145, 21.3810038566589);
            Mapa.MinZoom = 1;
            Mapa.MaxZoom = 24;
            Mapa.Zoom = 17;
            Mapa.CanDragMap = true;
            Mapa.Height = this.Height - 100;
            Mapa.Dock = DockStyle.Fill;
            Mapa.Manager.Mode = AccessMode.ServerAndCache;

This is the code that opens a window with the map.

private void bttShowMap_Click(object sender, EventArgs e)

    Cursor = Cursors.WaitCursor;
    frmLatLng ll = new frmLatLng();
    Cursor = Cursors.Default;


Oct 19, 2012 at 9:41 AM

just don't use frmLatLng_Load event, put everything in constructor

Oct 19, 2012 at 4:08 PM

I had the same problem some time ago. The solution:

I used in a Marker or Route (can't remember) a static Brush or Pen (Pens.Black, Brushes.Green,...). That caused the error, when the form was opened again, because the Dispose() method could not dispose of static Brushes/Pens (which seems logical :-).

So just use for example:

route.Stroke = new Pen(Color.Black) instead of route.Stroke = Pens.Black.


Oct 19, 2012 at 5:15 PM

route.Stroke = Colors.Red; // that is correct way, no need to make new instances...

Nov 22, 2012 at 6:06 PM
Edited Nov 22, 2012 at 6:08 PM

I have the same Problem on (VS2010). I have a Basic form (F_Basic) with a button. If I press the button I call the form SimpleMap. At first time pressing the button it works perfect, SimpleMap is opended. Then I close SimpleMap and press the button in F_Basic again. The program crashes with "invalid Parameter"

Code of F_Basic:

Public Class F_Basic

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    End Sub
End Class

Code of SimpleMap:

Imports GMap.NET
Imports GMap.NET.WindowsForms

Public Class SimpleMap

    Private Sub SimpleMap_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        Me.GMapControl1.MinZoom = 1
        Me.GMapControl1.MaxZoom = 17
        Me.GMapControl1.Position = New PointLatLng(48, 11)
        Me.GMapControl1.Zoom = 7
        'Me.GMapControl1.Manager.Mode = AccessMode.ServerAndCache
        Me.GMapControl1.Manager.Mode = AccessMode.ServerOnly
        Me.GMapControl1.MapProvider = GMap.NET.MapProviders.BingHybridMapProvider.Instance
    End Sub
End Class

Also I have tried this code for F_Basic (but it is the same result)

Public Class F_Basic

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim f As SimpleMap
        If f Is Nothing Then
            f = New SimpleMap
        End If
        f.Owner = Me
    End Sub
End Class

On Debugging I saw that it is crashing in SimpleMap at second call at line "Me.GMapControl1.MapProvider =..."

All my tries with dispose and so on does not help. Can anybody help me?

Nov 22, 2012 at 6:15 PM

if you want reuse the form, don't close it, hide it, or else use new form instance

Nov 22, 2012 at 9:48 PM

Thank you for your quick response. I do not want to hide form because of Memory. Also I want use GMapControls in more different forms of application.

I have tried this one with a new form instance. But it is still not working.

Public Class F_Basic

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click

        Dim f As New SimpleMap
     End Sub
End Class

Do you have any idea? It would be great because GMap.Net is a great tool, but only this problem is really a big problem for me.

Nov 22, 2012 at 10:31 PM

try don't use load event

p.s. what version do you use?

Nov 23, 2012 at 7:08 AM

Thank you for your patience. I have tried in SimpleMap-Form to move the code from Load-Event to a button event. But this also doesn't help. Now I can open and close the SimpleMap-Form very often, but if I click in SimplMap the Button with "Me.GMapControl1.MinZoom..." the next time when I open SimpleMap it crashes at pressing this button.

I program in VS 2010, Framework 4.0 (I have tried it also with Framework 2.0, but it is the same).

I use:

- GMap.Net.Core.dll Version from 31.01.2012/17:51 (2,19MB)

- GMap.Net.WindowsForms.dll Version from 31.01.2012/17:51 (138KB)

Nov 23, 2012 at 12:47 PM

hm, it's quite old build, try latest

Nov 23, 2012 at 2:16 PM

Where can I find it. Here on Codeplex is only an older one.

Nov 23, 2012 at 2:26 PM


latest build at the bottom

Nov 23, 2012 at 2:37 PM

Now it works!!!! Thank you very, very much!!!

Nov 23, 2012 at 3:03 PM

..i guess it's some old bug..

Apr 4, 2013 at 3:37 PM
Xandolph: I had the same problem.
restrictedArea is a polygon.

Wrong way:
restrictedArea.Stroke.Brush = Brushes.Red;
restrictedArea.Stroke.Width = 1.2f;
restrictedArea.Fill = Brushes.Transparent;

Correct way:
restrictedArea.Stroke = new Pen(Color.Red);
restrictedArea.Stroke.Width = 1.2f;
restrictedArea.Fill = new SolidBrush(Color.Transparent);