簡體   English   中英

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

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

上下文:我的表單中有一個TreeView。 根據TreeView的選擇,我在面板中加載UserControl。 一個面板是只讀的,但在另一個面板中,用戶可以修改/添加我保存在數據庫中的數據。 如果用戶嘗試在TreeView(事件BeforeSelect)中進行新選擇並且他在第二個控件(mod / add)中並且他有未保存的數據,我想在加載新控件之前詢問他是否要保存。 問題是,當顯示消息框時,事件有點失去了焦點,並在循環中調用(大約20次)。 此外,無論用戶點擊消息框(是保存還是不加載新控件)都沒有影響。

所以我的問題是:無論如何要問用戶他想在事件處理程序中做什么?

我希望我很清楚,抱歉英語不是我的第一句話

/編輯

這里的代碼來自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;
}

功能AvertirUser只包含一個消息框

您需要檢查選擇是用戶操作還是用戶取消的還原操作等。

修復它的一種方法是添加另一個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;
}

並且記得在再次加載second control時重置IsChecked=false

UserControl添加Close()方法,並將消息框代碼放在那里。 然后它可以調用自己的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();
            }
        }
    }

回到表格中,您可以:

    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