How to close form

后端 未结 8 2162
南旧
南旧 2021-02-18 23:54

Ok, so a Windows Forms class, WindowSettings, and the form has a \"Cancel\"-button. When the user clicks the button, the dialog DialogSettingsCancel will pop-up up and ask the u

相关标签:
8条回答
  • 2021-02-18 23:57

    Why not use the DialogResult method to close the form?

    if(DialogSettingsCancel.ShowDialog() == DialogResult.Yes)
    {
         //this will close the form but will keep application open if your 
         //application type is "console" in the properties of the project
         this.Close();
    }
    

    For this to work however you will need to do it inside your "WindowSettings" form while you call the DialogSettingsCancel form. Much the same way you would call the OpenFileDialog, or any other Dialog form.

    0 讨论(0)
  • 2021-02-19 00:00

    There are different methods to open or close winform. Form.Close() is one method in closing a winform.

    When 'Form.Close()' execute , all resources created in that form are destroyed. Resources means control and all its child controls (labels , buttons) , forms etc.

    Some other methods to close winform

    1. Form.Hide()
    2. Application.Exit()

    Some methods to Open/Start a form

    1. Form.Show()
    2. Form.ShowDialog()
    3. Form.TopMost()

    All of them act differently , Explore them !

    0 讨论(0)
  • 2021-02-19 00:01

    Your closing your instance of the settings window right after you create it. You need to display the settings window first then wait for a dialog result. If it comes back as canceled then close the window. For Example:

    private void button1_Click(object sender, EventArgs e)
    {
        Settings newSettingsWindow = new Settings();
    
        if (newSettingsWindow.ShowDialog() == DialogResult.Cancel)
        {
            newSettingsWindow.Close();
        }
    }
    
    0 讨论(0)
  • 2021-02-19 00:05

    send the WindowSettings as the parameter of the constructor of the DialogSettingsCancel and then on the button1_Click when yes is pressed call the close method of both of them.

    public class DialogSettingsCancel
    {
        WindowSettings parent;
    
        public DialogSettingsCancel(WindowSettings settings)
        {
            this.parent = settings;        
        }
    
        private void button1_Click(object sender, EventArgs e)
        {
            //Code to trigger when the "Yes"-button is pressed.
            this.parent.Close();
            this.Close();
        }
    }
    
    0 讨论(0)
  • 2021-02-19 00:07
    new WindowSettings();
    

    You just closed a brand new instance of the form that wasn't visible in the first place.

    You need to close the original instance of the form by accepting it as a constructor parameter and storing it in a field.

    0 讨论(0)
  • 2021-02-19 00:12

    for example, if you want to close a windows form when an action is performed there are two methods to do it

    1.To close it directly

    Form1 f=new Form1();
    f.close(); //u can use below comment also
    //this.close();
    

    2.We can also hide form without closing it

     private void button1_Click(object sender, EventArgs e)
        {
            Form1 f1 = new Form1();
            Form2 f2 = new Form2();
            int flag = 0;
            string u, p;
            u = textBox1.Text;
            p = textBox2.Text;
            if(u=="username" && p=="pasword")
            {
                flag = 1;
            }
            else
            {
              MessageBox.Show("enter correct details");
            }
            if(flag==1)
            {
                f2.Show();
                this.Hide();
            }
    
        }
    
    0 讨论(0)
提交回复
热议问题