识别未保存的更改并通知用户 [英] Recognise Unsaved Changes and Notify User

查看:133
本文介绍了识别未保存的更改并通知用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个链接到数据库的表单,并允许用户插入,更新和删除记录。我希望用户在点击返回主菜单时获得任何未保存的更改的通知,并且具有是否保存更改的选项。

I have a form that is linked to a database and allows a user to insert, update and delete records. I want the user to be notified of any unsaved changes when they click to go back to the main menu, and have the option whether or not to save them.

我到目前为止的代码如下,但它不能识别更改。

The code I have so far is below, but it doesn't recognize the changes. It just goes straight back to the main menu.

private void btnBack_Click(object sender, EventArgs e)
    {
        frmMenu frmMainMenu = new frmMenu();

        if (dsOrders.HasChanges())
        {


            if (DialogResult.Yes == MessageBox.Show("There are changes that have not been saved and will be lost. Would you like to save them before leaving this form?", "Unsaved Changes", MessageBoxButtons.YesNo))
            {
                dsOrders.AcceptChanges();
            }
            else
            {
                frmOrders.ActiveForm.Hide();

                frmMainMenu.Show();
            }

        }
        else
        {
            frmOrders.ActiveForm.Hide();

            frmMainMenu.Show();
        }

    }


推荐答案

一种方法是使用个别控制项的「已变更」事件并设定一个脏位

One way to do is use the individual controls' Changed event and set a dirty bit

例如

    public bool Dirty { get; set; }


    private void textBox1_TextChanged(object sender, EventArgs e)
    {
          Dirty = true;
    }

,然后

        if (Dirty)
        {


            if (DialogResult.Yes == MessageBox.Show("There are changes that have not been saved and will be lost. Would you like to save them before leaving this form?", "Unsaved Changes", MessageBoxButtons.YesNo))
            {
                dsOrders.AcceptChanges();
            }
            else
            {
                frmOrders.ActiveForm.Hide();

                frmMainMenu.Show();
            }

        }

这篇关于识别未保存的更改并通知用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆