简体   繁体   English

检测用户何时按下红色x

[英]Detecting when user presses the red x

Super new to C#, but ran into a little roadblock on my most recent project. C#超级新手,但在我最近的项目中遇到了一些障碍。 I have a couple window forms that the user is able to click through. 我有几个用户可以单击的窗口形式。 In the main program I have a while loop that will keep looping through the forms and will initiate the form depending on which button the user pressed in the previous form (variable is called traverse). 在主程序中,我有一个while循环,该循环将不断循环访问表单,并将根据用户在上一个表单中按下的按钮(变量称为遍历)来启动表单。 Right now I put in an exit button on each form that will break the while loop. 现在,我在每种表单上都设置了退出按钮,这将打破while循环。 What I'm having trouble with is when the user presses the red X at the top right corner, the form will not close (will just keep reloading). 我遇到的麻烦是,当用户按下右上角的红色X时,该表单将不会关闭(将继续重新加载)。 I put a ** next to the line of the code that I thought might help with the issue (but obviously did not). 我在我认为可能有助于解决该问题的代码行旁边加上了**(但显然没有帮助)。

    while (Program.exit != "exit")
    {
        **if (Application.Exit = true)
        {
            break
        } else 
          {
            if (Program.traverse == "form4")
            {
                Application.Run(new Form4());
            }
            if (Program.traverse == "form1")
            {
                Application.Run(new Form1());
            }
            if (Program.traverse == "form5")
            {
                Application.Run(new Form5());
            }
          }
        }
    }

Is there a way C# can detect when the user presses the red X to break out of the loop? C#有什么方法可以检测到用户何时按下红色X来退出循环?

First: your approuch is totaly wrong. 第一:您的粗鲁完全是错误的。

Second: befor starting i am totally recommend to you read some books about Windows Forms technology or, what better about Windows Presentation Foundation (WPF) technology. 第二:从头开始我完全建议您阅读一些有关Windows Forms技术的书,或者有关Windows Presentation Foundation (WPF)技术的书。 After this you will no ask so wired questions and will improve you development speed and qualty dramaticly. 在此之后,您将不会提出任何有线问题,并且将大大提高您的开发速度和质量。

Third: direct answer to you question: 第三:直接回答您的问题:

in you Form code behind you can do next: 在您后面的表单代码中,您可以执行以下操作:

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            Closed += OnClosed;
        }

        private void OnClosed(object sender, EventArgs e)
        {
            // DO, what you need when windows closed
        }

    }

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

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