更新时间:2022-06-26 09:34:37
诀窍是捕获ApplicationExit事件。这是通过编写一个在窗体关闭时调用的事件处理程序来完成的。我个人使用应用程序出口作为我的触发器。您还可以使用FormClosing事件。例如:
The trick is to catch the ApplicationExit event. This is done by writing an event handler that will be invoked when the form is closed. I personally use the application exit as my trigger. You could also use the FormClosing event. An example:
// ****************************************** Application_Exit
void Application_Exit ( object sender,
EventArgs e )
{
// Place your data saving code here
}
然后在表单构造函数中声明事件处理程序,例如:
Then declare the event handler in your form constructor, like:
Application.ApplicationExit +=
new EventHandler (
Application_Exit );
您的表单构造函数中还包含用于确定是否存在任何早期保存数据的代码。仅仅存在一个文件就足够了。如果是这样,请检索它并填充DataGrid。
Also in your form constructor include code to determine if there is any earlier saved data. The mere existence of a file should be sufficient. If so, retrieve it and populate your DataGrid.
确保将用户数据保存在用户可访问的用户唯一文件中。我建议不要使用cookies,因为它们可能会在机器上被禁止。
Make sure that you save the user's data in a user-accessible user-unique file. I recommend against using cookies as they may be prohibited on the machine.
希望有所帮助。
Hope that helps.