0





82
1

So basically im trying to make polygons with a name entered from Form2, called Apgabala_nosaukums (it's in my language, sorry for that). I have been trying to debug this, first 2 times the name entered from Form2 did get read and i was able to see that the name was added to the Polygon. But now it is not getting in the fromVisibleChanged anymore, ending in that the polygon is not getting name. Meaning that I cannot get the bool to true, so I could add 4 points and make a square or rectangle area out of them. Any ideas? Basically the btnAdd_Click function is not working properly, rest is working fine. Any ideas?

Form1 (Main form):

namespace GMapTest
{
    public partial class Form1 : Form
    {
        GMapOverlay polygons = new GMapOverlay("polygons");
        List<PointLatLng> points = new List<PointLatLng>();
        double lat;
        double lng;
        int clicks = 0;
        bool add = false;
        string nosaukums;
        public Form1()
        {
            InitializeComponent();
        }

        private void gMapControl1_Load(object sender, EventArgs e)
        {
            gmap.MapProvider = GoogleMapProvider.Instance;
            GMaps.Instance.Mode = AccessMode.ServerOnly;
            gmap.SetPositionByKeywords("Riga, Latvia");
            gmap.ShowCenter = false;
            gmap.Overlays.Add(polygons);  
        }

        private void gmap_MouseDown(object sender, MouseEventArgs e)
        {
            if (add == true)
            {
                if (e.Button == MouseButtons.Left)
                {
                    lat = gmap.FromLocalToLatLng(e.X, e.Y).Lat;
                    lng = gmap.FromLocalToLatLng(e.X, e.Y).Lng;
                    clicks += 1;
                    points.Add(new PointLatLng(lat, lng));
                }
                if (clicks == 4)
                {
                    GMapPolygon polygon = new GMapPolygon(points, nosaukums);
                    polygons.Polygons.Add(polygon);

                    clicks = 0;
                    points.Clear();
                    add = false;
                }
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            Apgabala_nosaukums addName = new Apgabala_nosaukums();
            addName.ShowDialog();
            addName.VisibleChanged += formVisibleChanged;
            if (nosaukums != null)
            {
                this.add = true;
            }

        }

        private void formVisibleChanged(object sender, EventArgs e)
        {
            Apgabala_nosaukums frm = (Apgabala_nosaukums)sender;
            if (!frm.Visible)
            {
                this.nosaukums = (frm.ReturnText);
                frm.Dispose();
            }
        }
    }
}

Form2 (Apgabala_nosaukums):

namespace GMapTest
{
    public partial class Apgabala_nosaukums : Form
    {
        public string ReturnText { get; set; }

        public Apgabala_nosaukums()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.ReturnText = this.txtName.Text;
            this.Visible = false;
        }
    }
}
Question author Janis-lazda | Source

0


1

The problem is in your btnAdd_Click function. When you call ShowDialog your other form is shown and the next line, addName.VisibleChanged += formVisibleChanged; isn't called until you close the new form. ShowDialog shows the form modally, you can't interact with the parent until you close the new form.

There are a couple ways you could fix this.

1) Subscribe to the VisibleChanged event before you show the form,

addName.VisibleChanged += formVisibleChanged;
addName.ShowDialog();

2) Call addName.Show() instead of addName.ShowDialog(). This shows the form in a non-modal way. The event will get subscribed to because execution continues in btnAdd_Click before the new form is closed. But, the parent form will be interactable, not sure if this is desired or not.

3) You could also get rid of the VisibleChanged event stuff and instead do ShowDialog and read the property after. This is what I'd recommend from seeing the code.

private void btnAdd_Click(object sender, EventArgs e)
{
    Apgabala_nosaukums addName = new Apgabala_nosaukums();
    addName.ShowDialog();
    this.nosaukums = addName.ReturnText;
    addName.Dispose();
}
Answer author Gunnerone

Ask about this question here!