简体   繁体   English

关闭 C# Windows 窗体应用程序时重置模态窗体

[英]Resetting Modal Form When Closing C# Windows Forms App

I am writing a C# Windows Forms App Online Store GUI that interfaces with a database.我正在编写一个与数据库接口的 C# Windows 窗体应用程序在线商店 GUI。 One of the things I have to do is handle customers that are already in the database.我必须做的一件事是处理已经在数据库中的客户。 From the main menu form they click the 'Returning Customer' button, and enter their email and password.从主菜单表单中,他们单击“回头客”按钮,然后输入他们的电子邮件和密码。 The data entered is then checked against the customers stored in the database, and if it's verified, their user information is filled into text boxes (Name, Credit Card info, CVS, etc...) and the order form becomes visible.然后根据存储在数据库中的客户检查输入的数据,如果验证通过,则将他们的用户信息填入文本框(姓名、信用卡信息、CVS 等),并且订单表格变得可见。 I have no issues there.我在那里没有问题。 The issue I'm having is that if a returning customer successfully logs in, then cancels out back to the main menu, the next person to click the 'Returning Customer' button pulls up the form with the first user's information already filled in and visible, since both the this.Close() and the this.DialogResult = DialogResult.Cancel methods both only hide the form rather than actually close and free it.我遇到的问题是,如果回头客成功登录,然后取消回到主菜单,下一个点击“回头客”按钮的人会拉出表单,其中第一个用户的信息已经填写并可见,因为this.Close()this.DialogResult = DialogResult.Cancel方法都只隐藏表单而不是实际关闭和释放它。 But then if I use this.Dispose() on the Returning Customer Form to free it, it can't be reopened.但是如果我在返回客户表单上使用this.Dispose()来释放它,它就无法重新打开。
My question is: is there an easy way to handle this?我的问题是:有没有简单的方法来处理这个问题? I'm self taught in C# so forgive my inexperience.我是用 C# 自学的,所以请原谅我的经验不足。 Thank you for any help you can give.感谢您提供的任何帮助。

Per request see Form1 (Main Menu Form) code below:每个请求请参阅下面的 Form1(主菜单表单)代码:

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Database_Interface_Project
{
    public partial class Form1 : Form
    {
        // Removed SqlConnectionString for security purposes.
        public SqlConnection cnn = new SqlConnection(connectionString);


        // Main Menu Form
        public Form1()
        {
            InitializeComponent();
        }

        // New Customer Form
        Form2 newCustomer = new Form2();

        private void newCusButt_Click(object sender, EventArgs e)
        {
            newCustomer.ShowDialog();
        }

        // Returning Customer Form
        ReturningCustomer returningCustomer = new ReturningCustomer();
        private void RetCusButt_Click(object sender, EventArgs e)
        {
            returningCustomer.ShowDialog();

        }

        private void exitButt_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        // Manager Menu Form
        Manager managerMenu = new Manager();
        private void managerButt_Click(object sender, EventArgs e)
        {
            managerMenu.ShowDialog();
        }
    }
}```

You just need to re-instantiate returningCustomer form in order to reset its fields.您只需要重新实例化 ReturningCustomer 表单即可重置其字段。

private void RetCusButt_Click(object sender, EventArgs e)
{
    using (var returningCustomer = new ReturningCustomer())
    {
        returningCustomer.ShowDialog();
    }
}

Using statement calls Dispose() automatically after the using-block is left.在 using 块离开后, using 语句会自动调用Dispose()

Note : In your case you need to dispose ShowDialog appropriately to avoid GDI leak, since it has side effect of keeping the GDI objects alive.注意:在您的情况下,您需要适当地处理ShowDialog以避免 GDI 泄漏,因为它具有使 GDI 对象保持活动状态的副作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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