gpt4 book ai didi

c# - 检查表单数据是否已更改

转载 作者:太空宇宙 更新时间:2023-11-03 11:32:05 25 4
gpt4 key购买 nike

我一直在尝试使用以下 C# 代码来检查表单数据在关闭和保存之前是否已更改,但它似乎总是报告表单已更改,即使未进行任何更改也是如此。

//Declare a private variable
private bool requiresSaving =false;

//Declare an event
private void SomethingChanged(object sender, EventArgs e)
{
requiresSaving = true;
}

//Hook up this event to the various changed events, eg
this.txtNameDepart.TextChanged += new System.EventHandler(this.SomethingChanged);


//Check this variable when you are closing the form
private void DepartamentEdit_FormClosing(object sender, FormClosingEventArgs e)
{
if (requiresSaving)
{
....

还需要在saveDepart方法中设置requiresSaving为false。

最佳答案

我认为您甚至可以在加载初始数据之前连接这些事件。然后 SomethingChanged 触发并启用保存按钮,即使用户没有更改任何内容。

您可以在加载默认/现有数据时取消挂接这些事件,也可以在加载默认/现有数据后挂接这些事件。

//Un-Hook when loading your default/existing data.
private void SetDefaultData()
{
this.txtNameDepart.TextChanged -= new System.EventHandler(this.SomethingChanged);
this.txtNameDepart = "My default text";
this.txtNameDepart.TextChanged += new System.EventHandler(this.SomethingChanged);
}

关于c# - 检查表单数据是否已更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7439438/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com