简体   繁体   English

如何在事件处理程序中使用消息框

[英]How to use a messagebox in a event handler

Context : I have a TreeView in one of my form. 上下文:我的表单中有一个TreeView。 Depending on the selection of that TreeView I load a UserControl in a pannel. 根据TreeView的选择,我在面板中加载UserControl。 One pannel is readonly but in the other pannel the user can modifie/add data that I save in a database. 一个面板是只读的,但在另一个面板中,用户可以修改/添加我保存在数据库中的数据。 If the user try to make a new selection in the TreeView (event BeforeSelect) and he is in the second control (mod/add) and he have unsaved data, I want to ask him if he want to save before loading the new control. 如果用户尝试在TreeView(事件BeforeSelect)中进行新选择并且他在第二个控件(mod / add)中并且他有未保存的数据,我想在加载新控件之前询问他是否要保存。 The problem is that when the messagebox is shown the event kinda lost the focus and is called in loop (around 20 times). 问题是,当显示消息框时,事件有点失去了焦点,并在循环中调用(大约20次)。 In addition, no matter what the user click on the messagebox (yes save or no just load new control) as no influence. 此外,无论用户点击消息框(是保存还是不加载新控件)都没有影响。

So my question is : Is there anyway to ask the user what he want to do in the event handler? 所以我的问题是:无论如何要问用户他想在事件处理程序中做什么?

I hope i was clear enought, sorry English isn't my first langage 我希望我很清楚,抱歉英语不是我的第一句话

/edit /编辑

Here the code from BeforeSelect and IsCtrlFormUnsave 这里的代码来自BeforeSelect和IsCtrlFormUnsave

private void tv_BeforeSelect(object sender, TreeViewCancelEventArgs e)
{
    if (IsCtrlFormUnsave())
    {
        e.Cancel = true;
        //Invoke(new Action(AvertirUser)); //this is in case the save action didn't worked
    }
}

private bool IsCtrlFormUnsave()
{
    if (_ctrlForm != null && _ctrlForm.unsavedChange)
    {
        DialogResult dr = MessageBox.Show("Le formulaire présentement ouvert contient des données qui n'ont pas été sauvegardées. Voulez-vous les enregistrés avant de poursuivre?",
                                                "Attention!", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
        if (dr == DialogResult.Yes)
            if (!_ctrlForm.Save())
                return true;

        _ctrlForm = null;
    }
    return false;
}

The function AvertirUser only containt a messagebox 功能AvertirUser只包含一个消息框

you need to check if the selection is the user action or a revert action of user cancel etc. 您需要检查选择是用户操作还是用户取消的还原操作等。

one way of fixing it is to add another bool value: 修复它的一种方法是添加另一个bool值:

bool IsChecked=false;
private void tv_BeforeSelect(object sender, TreeViewCancelEventArgs e)
{
    if (!IsChecked && IsCtrlFormUnsave()) //Check if it's already confirmed with the user
    {
        e.Cancel = true;           
    }
}

private bool IsCtrlFormUnsave()
{
    IsChecked=true; //set it to true to jump out of the loop
    if (_ctrlForm != null && _ctrlForm.unsavedChange)
    {   

        DialogResult dr = MessageBox.Show("Le formulaire présentement ouvert contient des données qui n'ont pas été sauvegardées. Voulez-vous les enregistrés avant de poursuivre?",
                                                "Attention!", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
        if (dr == DialogResult.Yes)
            if (!_ctrlForm.Save())
                return true;

        _ctrlForm = null;
    }
    return false;
}

And remember to reset IsChecked=false when you load second control again. 并且记得在再次加载second control时重置IsChecked=false

Add a Close() method to your UserControl , and place the messagebox code there. UserControl添加Close()方法,并将消息框代码放在那里。 Then it can call its own Save() method: 然后它可以调用自己的Save()方法:

    public void Close()
    {
        if (this.unsavedChange)
        {
            DialogResult dr = MessageBox.Show("Le formulaire présentement ouvert contient des données qui n'ont pas été sauvegardées. Voulez-vous les enregistrés avant de poursuivre?",
                                                    "Attention!", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
            if (dr == DialogResult.Yes)
            {
                this.Save();
            }
        }
    }

Back in the Form, you can do: 回到表格中,您可以:

    private void tv_BeforeSelect(object sender, TreeViewCancelEventArgs e)
    {
        if (_ctrlForm != null)
        {
            _ctrlForm.Close();
            _ctrlForm.Dispose();
            _ctrlForm = null;
        }
    }

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

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