Conversion from Lat Long to MGRS is working but not the other way around C#

99 Views Asked by At

I'm using CoordinateSharp package to convert my coordinates. When I want to convert from Lat Long to MGRS it works, even editing my lat long numericupdown box updates the MGRS to the correct value, the value changes when I click the next waypoint button. But when I change my MGRS textbox it doesnt update the numericupdown box with the respective Lat long value for that new MGRS value

My MGRS textbox code that displays the MGRS coordinates:-

private void textBox3_TextChanged(object sender, EventArgs e)
{
    textBox1.Text = textBox3.Text;
            
    if (cycle)
    {
        cycle = true;
        string mgrsValue = textBox3.Text;
        try
        {
            if (Coordinate.TryParse(mgrsValue, out Coordinate c))
            {
                LatDeg.Value = (decimal)c.Latitude.DecimalDegree;
                LongDeg.Value = (decimal)c.Longitude.DecimalDegree;
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show("An error occurred: " + ex.Message);
        }
        cycle = false;
    }
}

My Latitude numericupdownbox code:-

private void LatDeg_ValueChanged(object sender, EventArgs e)
{
    try
    {
        if (!cycle)
        {
            double latitude = (double)LatDeg.Value;
            double longitude = (double)LongDeg.Value;
            Coordinate c = new Coordinate(latitude, longitude);
            textBox3.Text = c.MGRS.ToString();
            Hide_BTNs();
        }
        cycle = false;
    }
    catch (Exception ex)
    {
        MessageBox.Show("An error occurred: " + ex.Message, "Error");
    }
}

My Longtitude numericupdown box code

private void LongDeg_ValueChanged(object sender, EventArgs e)
{
    try
    {
        if (!cycle)
        {
            double latitude = (double)LatDeg.Value;
            double longitude = (double)LongDeg.Value;
            Coordinate c = new Coordinate(latitude, longitude);
            textBox3.Text = c.MGRS.ToString();
            Hide_BTNs();
        }
        cycle = false;
    }
    catch (Exception ex)
    {
        MessageBox.Show("An error occurred: " + ex.Message, "Error");
    }
}

Next waypoint button just for reference

private void NextWP_Click(object sender, EventArgs e)
{
    cycle = true;

    WPNumber.Value = WPNumber.Value + 1;
    int no = (int)WPNumber.Value;

     cycle = false;
}

Note: the cycle boolean here is to distinguish between clicking nextwp button and when changing the the textbox3 value (basically you click the nextwp button --> the new latlong coordinates appear for that specific waypoint in the lat and long numericupdown boxes --> that specific waypoint latlong coords are converted to MGRS on the MGRS textbox. You can only change the numericupdown box value and it would change the MGRS value on textbox3 but not the other way around)

What I tried

I tried making a panel with a textbox that when you click convert, it converts the written MGRS to its respective latlong coordinates and it worked. Idk how with the textbox3 its not working, I was even using the same code I used on textbox3.

0

There are 0 best solutions below