简体   繁体   English

我如何使messageBox在单击按钮的时限内不显示? C#

[英]How would I make a messageBox not show within a time limit of a button click? c#

I am making a save system in my program, and I wanted to make it where if you have not saved a log within 1 minute it will appear a messagebox. 我正在程序中创建一个保存系统,我想将其保存在如果您未在1分钟内保存日志的地方,它将出现一个消息框。 In other words I want the messageBox not show if you clicked a button within 1 minute. 换句话说,如果您在1分钟内单击一个按钮,我希望messageBox不显示。 How would I go about doing this? 我将如何去做呢?

 private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
        if (e.CloseReason == CloseReason.UserClosing)
        {
            e.Cancel = true;
            DialogResult choice = MessageBox.Show("          Would you like to save the Log?", "Save Log?", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Asterisk);
            if (choice == DialogResult.Yes)
            {
                button6.PerformClick();
                e.Cancel = false;
            }

            else if (choice == DialogResult.No)
            {
                e.Cancel = false;
            }

        }
    }

You can check if the window from the log save is still open. 您可以检查日志保存窗口是否仍然打开。 Next you can create a thread with Thread.sleep(miliseconds) and show, after the delay has passed the message box. 接下来,您可以使用Thread.sleep(miliseconds)创建一个线程,并在延迟超过消息框后显示。 You can create a function on window exit so that it will also activate the message box when the log window is closed within one min. 您可以在窗口退出时创建一个函数,以便在日志窗口在一分钟内关闭时也将激活消息框。

 private void CreateTimer()
 {
     static System.Windows.Forms.Timer myTimer = new System.Windows.Forms.Timer();
     myTimer.Tick += new EventHandler(TimerEventProcessor);

      // Sets the timer interval to 1 minute.
      myTimer.Interval = 60000;
      myTimer.Start();
 }

private static void TimerEventProcessor(Object myObject,
                                        EventArgs myEventArgs) {
   myTimer.Stop();

   // Write code here to show the message box asking the user to save
   // Once the user has saved restart the timer
   myTimer.Start();


}

Remember when the user saves you need to stop and start the timer. 请记住,当用户保存时,您需要停止并启动计时器。 Write that code in your save method. 在您的保存方法中编写该代码。

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

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