简体   繁体   中英

Having problem to close a second windows form without closing the main form

In ac# I have two forms: Windows Form1 & Windows Form2. When I click on a link label in Form1, the second form is shown. But when I close the second form, my first Windows form closes too.

my main form is:

namespace Windows_Forms_Basics
{
    public partial class ShowLocationOnMapForm : Form
    {
        private string latitude;
        private string longitute;
        private GeoCoordinateWatcher watcher = new GeoCoordinateWatcher();

    public ShowLocationOnMapForm()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        watcher = new GeoCoordinateWatcher();
        // Catch the StatusChanged event.  
        watcher.StatusChanged += Watcher_StatusChanged;
        // Start the watcher.  
        watcher.Start();
    }

    private void button_ShowOnMap_Click(object sender, EventArgs e)
    {
        textBox_Latitude.Text = latitude;
        textBox_Longtitude.Text = longitute;
    }

    [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1031:Do not catch general exception types", Justification = "<Pending>")]
    private void Watcher_StatusChanged(object sender, GeoPositionStatusChangedEventArgs e) // Find GeoLocation of Device  
    {
        try
        {
            if (e.Status == GeoPositionStatus.Ready)
            {
                // Display the latitude and longitude.  
                if (watcher.Position.Location.IsUnknown)
                {
                    latitude = "0";
                    longitute = "0";
                }
                else
                {
                    latitude = watcher.Position.Location.Latitude.ToString();
                    longitute = watcher.Position.Location.Longitude.ToString();
                }
            }
            else
            {
                latitude = "0";
                longitute = "0";
            }
        }
        catch (Exception)
        {
            latitude = "0";
            longitute = "0";
        }
    }

    private void label1_Click(object sender, EventArgs e)
    {

    }

    private HelpForm form2 = new HelpForm();
    private void linkLabel_help_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
    {
        this.Hide();
        form2.Show();
    }
}
}

and my second form is:

  namespace Windows_Forms_Basics
{
    public partial class HelpForm : Form
    {

    public HelpForm()
    {
        InitializeComponent();
    }


    private void HelpForm_Load(object sender, EventArgs e)
    {

    }


    private void button_BackToForm1_Click(object sender, EventArgs e)
    {
     ShowLocationOnMapForm form1 = new ShowLocationOnMapForm();
       this.Hide();
        form1.Show();
    }
}
}

Do you have any idea how to tackle this problem?

I am guessing you may be “confusing” forms and one forms “ability” to access another form. Let's start by taking a look at the bit of code in your initial question…

private HelpForm form2 = new HelpForm();
private void linkLabel_help_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)  {
    this.Hide();
    form2.Show();
}

This code is called from (what appears to be) a Form named ShowLocationOnMapForm. On this form is a LinkLabel named linkLabel_help and its LinkClicked event is wired up and is shown above. In addition, there appears to be a “global” variable form2 that is a HelpForm object (first line in the code above), which is another form. It is unnecessary to create this “global” form variable in the ShowLocationOnMapForm. …. However, we will continue as it is not pertinent at this point.

When the user “clicks” the `LinkLabel' the above method will fire. On the first Line in the method…

this.Hide();

Is going to “hide” the current form ShowLocationOnMapForm … and “show” the second form (HelpForm) with the line…

form2.Show();

On the surface, this may appear correct, however, there is one problem that I am guessing you are missing. The problem is…

How are you going to “unhide” the first form ShowLocationOnMapForm ?

The second form (HelpForm) is “shown”, however, it isn't going to KNOW anything about the first form. In this situation the first form is basically LOST and you have no way of accessing it. Therefore when you attempt the line… form1.Show(); in the second form, the compiler is going to complain because its not going to know what form1 is. In this code, there is NO way to get back to the first form. It is not only hidden from the user, but from the second form's perspective the “CODE” can't see it either!

Your faulty solution is not only “creating” another form1 but it is also doing the same with the second form.

Given this, it appears clear, that if you want to keep access to the first form… then you are going to have to use a ShowDialog instead of Show OR pass the first form to the second form.

Since it is unclear “how” you want these forms to interact, I can only proffer two (2) ways that you can use to at least keep access to the first form.

1) Use ShowDialog instead of Show when displaying the second form. It may look like …

private void linkLabel_help_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) {
  HelpForm form2 = new HelpForm();
  this.Hide();
  form2.ShowDialog();
  this.Show();
}

In the code above, the ShowDialog is going to “STOP” code execution in the first form and will wait for the second form (form2) to close. When executed, the first form is hidden, then the second form is shown, however, unlike the Show command, the line this.Show(); will not be executed until the second form closes. I am guessing this may be what you are looking for.

2) Pass the first form to the second form giving the second form “access” to the first form.

This will require that the second form has a “constructor” that takes a ShowLocationOnMapForm object as a parameter. With this, the second form can create a “global” variable of type ShowLocationOnMapForm that “points” to the first form. The constructor may look like…

private ShowLocationOnMapForm parentForm;

public HelpForm(ShowLocationOnMapForm parent) {
  InitializeComponent();
  parentForm = parent;
}

In the first form, you would instantiate the second form with...

HelpForm form2 = new HelpForm(this);

With this approach, the second form will have total access to the first form. You could add the “back” button as you describe and simply execute the line… ParentForm.Show(); However, I recommend you also wire up the second forms FormClose event and show the first form, otherwise, if the user clicks the close button (top right) and doesn't click the “back” button, then you will have LOST the first form again.

Without knowing “exactly” how you want these forms to interact it is difficult to proffer a complete solution.

There are also other ways to accomplish this, however these should work for most cases.

I hope this makes sense and helps.

I tried to solve this problem by placing a 'Back to Form1' button in Form2. Which works, and the solution is as follows:

on my Form1 I have:

        private Form2 form2 = new HelpForm();
    private void linkLabel_help_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
    {
        this.Hide();
        form2.Show();
    }

and on my second form I have:

        private void button_BackToForm1_Click(object sender, EventArgs e)
    {
     HelpForm form1 = new HelpForm();
       this.Hide();
        form1.Show();
    }

But the problem is if I click the close button (on top right of the window) instead of the GoBack button on the second form, Form1 & Form2 both close in the same time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM