简体   繁体   中英

Forms Closing/Showing - C#

I am developing my first UI C# program. I am hoping for some help. My first form contains a few textboxes, and two radio buttons and a Go button. If one radio button is checked it opens a new small form, if the other is checked it opens a new large form.

when the user clicks go - this is my code -

        this.WindowState = FormWindowState.Minimized;
        int.TryParse(tbHrs.Text, out hours);
        int.TryParse(tbMins.Text, out minutes);
        int.TryParse(tbSecs.Text, out seconds);
        int.TryParse(tbWarn1.Text, out warn1);
        int.TryParse(tbWarn2.Text, out warn2);


        bool Max = rbMax.Checked;
        if (Max == true)
        {
            if (_Max == null || _Max.IsDisposed)
            {
                _Max = new Max(hours, minutes, seconds, warn1, warn2);
            }
            _Max.Show();


        }
        else
        {
            if (_Min == null || _Min.IsDisposed)
            {
                _Min = new Min(hours, minutes, seconds, warn1, warn2);
            }
            _Min.Show();
        }

so it minimizes the form where the values were entered and passes across the values to start counting down when constructing the new form. On the new form I want to have buttons to pause, which work fine. However I also want a stop/reset button. So on stop/reset click i want to close the current form but then I want to bring the first form open from minimize state - i tried the commented out line below but it did not work. Does anyone know of way I can show the first user input form from minimize state when stop close on the second form is clicked and even better if it could reset the fields to blank on my first form. Many Thanks.

    private void MinStop_Reset_Click(object sender, EventArgs e)
    {
        this.Close();
        //ParentForm.Show();
    }

Add an event on the second form (you could subscribe to the existing Form.Closed event but you may want a more specific event for your use case).

public event EventHandler UserStoppedEvent;

Subscribe to it from your first form and do whatever you need to

_min.UserStoppedEvent += (s, e) => {this.WindowState = FormWindowState.Maximized;} 

In the parent form do:

_min.FormClosed += (s1,e1) =>  { this.WindowState = FormWindowState.Maximized; }

You need to pass ParentForm handle to the constructor of those small forms. Also look here: http://msdn.microsoft.com/en-us/library/system.windows.forms.control.parent.aspx and also here: http://www.daniweb.com/software-development/csharp/threads/120120

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