简体   繁体   English

关闭程序Form1时,如何避免显示另一个表单?

[英]How can i avoid from showing another Form when closing the program Form1?

In the closing event i have this: 在结束时我有这个:

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

FormIsClosing is a flag. FormIsClosing是一面旗帜。

Then this is the KeysValuesUpdate function: 那么这就是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);
                }
            }
        }

The problem is if im doing just if (FormIsClosing == true) the the next line i want not to take effect im getting error on it: 问题是,如果即时通信只做(FormIsClosing == true)下一行我不想生效我得到错误:

Error 1 Embedded statement cannot be a declaration or labeled statement And the line after it : dr is unsigned. 错误1嵌入语句不能是声明或带标签的语句和它之后的行:dr是无符号的。

If im doing: 如果即时通讯:

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

Then dr is unsign on this line: if (dr == DialogResult.OK) 然后博士在这一行上没有签名:if(dr == DialogResult.OK)

What i want to archive is that if i close my application just dont show this dialog first. 我要归档的是,如果我关闭我的应用程序,请不要先显示此对话框。 And the line that show the dialog is: DialogResult dr = crawlLocaly1.ShowDialog(this); 显示对话框的行是:DialogResult dr = crawlLocaly1.ShowDialog(this);

Your condition refers only to next line as you have put nothing in brackets. 您的条件仅指下一行,因为您没有在括号中放置任何内容。

This should work, I guess: 这应该有用,我想:

        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);
            }
            }
        }

Update from OP's comment OP的评论更新

Here's how you can display the form when the program is not closing: 以下是程序未关闭时如何显示表单:

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

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

相关问题 我如何从Form2调用参数为Form1的方法并在Form1的图表上绘图 - How can i call a method with parameter of form1 from form2 and plot on chart on form1 如何在Form2中使用Form1中的变量? - How can I use variables from Form1 in Form2? 如何将 Timer 从 Form1 导入 Form2? - How can I import a Timer from Form1 to Form2? 如何从另一个用户控件访问 form1 上的 label4? - How can i access label4 on form1 from another user control? C#:如何在另一个类中更改form1中的标签文本? - C#: How can i change the text of a label thats in form1 from another class? 如何从另一个 class 将变量放入 Form1 class listBox 中? - How can I put variables in Form1 class listBox from another class? 当我/拖动/移动新窗体后面的form1使其同时与form1一起移动时,我该如何做? - How can i make that when i /drag/move form1 around the new form behind it will move with form1 on the same time? 如何从对话框中更改 Form1 中的某些内容? - How can I Change something in a Form1 from a DialogBox? 当我按下C#中的Form1上的按钮时,如何从Form1内部显示原始Form2 - how would I show the original Form2 from inside Form1 when I press button on Form1 in C# 如何将列表从 form1 传递到 form2 以在 form2 的 ButtonEvent 中访问它? - How can I pass a list from form1 to form2 to access it in a ButtonEvent in form2?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM