关闭程序Form1时,我如何避免显示另一个Form

本文关键字:何避免 显示 另一个 Form Form1 关闭程序 | 更新日期: 2023-09-27 18:00:02

在关闭事件中,我有这个:

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            FormIsClosing = true;
            KeysValuesUpdate();
        }

FormIsClosing是一个标志。

然后是KeysValuesUpdate函数:

public void KeysValuesUpdate()
        {
            using (var w = new StreamWriter(keywords_path_file))
            {
                if (FormIsClosing == true)
                crawlLocaly1 = new CrawlLocaly(this);
                crawlLocaly1.StartPosition = FormStartPosition.CenterParent;
                if (FormIsClosing == true)
                DialogResult dr = crawlLocaly1.ShowDialog(this);
                if (dr == DialogResult.OK)
                {
                    if (LocalyKeyWords.ContainsKey(mainUrl))
                    {
                        LocalyKeyWords[mainUrl].Clear();
                        LocalyKeyWords[mainUrl].Add(crawlLocaly1.getText());
                    }
                    else
                    {
                        LocalyKeyWords[mainUrl] = new List<string>();
                        LocalyKeyWords[mainUrl].Add(crawlLocaly1.getText());
                    }
                    Write(w);
                    ClearListBox();
                }
                if (dr == DialogResult.Cancel)
                {
                    Write(w);
                }
            }
        }

问题是,如果我只做如果(FormIsClosing==true)我不想生效的下一行,我会得到错误:

错误1嵌入语句不能是声明或标记语句后面的一行:dr是无符号的。

如果我在做:

if (FormIsClosing == true)
{
                    DialogResult dr = crawlLocaly1.ShowDialog(this);
}

则dr在这一行上取消签名:if(dr==DialogResult.OK)

我想归档的是,如果我关闭我的应用程序,就不要先显示这个对话框。显示对话框的行是:DialogResult dr=crawlLocaly1.ShowDialog(this);

关闭程序Form1时,我如何避免显示另一个Form

根据OP的评论更新

以下是在程序未关闭时显示表单的方法:

            DialogResult dr = DialogResult.None;//Increase accessibility domain, setup a good default value
            if (FormIsClosing != true)
            {
              dr = crawlLocaly1.ShowDialog(this);
            }

您的条件只引用下一行,因为您没有在括号中放入任何内容。

我想这应该行得通:

        using (var w = new StreamWriter(keywords_path_file))
        {
            if (FormIsClosing == true)
            {
            crawlLocaly1 = new CrawlLocaly(this);
            crawlLocaly1.StartPosition = FormStartPosition.CenterParent;
            DialogResult dr = crawlLocaly1.ShowDialog(this);
            if (dr == DialogResult.OK)
            {
                if (LocalyKeyWords.ContainsKey(mainUrl))
                {
                    LocalyKeyWords[mainUrl].Clear();
                    LocalyKeyWords[mainUrl].Add(crawlLocaly1.getText());
                }
                else
                {
                    LocalyKeyWords[mainUrl] = new List<string>();
                    LocalyKeyWords[mainUrl].Add(crawlLocaly1.getText());
                }
                Write(w);
                ClearListBox();
            }
            if (dr == DialogResult.Cancel)
            {
                Write(w);
            }
            }
        }