C# MessageBox在用户输入的特定时间显示,并带有数字UpDown

C# MessageBox在用户输入的特定时间显示,并带有数字UpDown,c#,winforms,C#,Winforms,所以,我把它存储在by按钮中,它只是将所有用户的输入连接起来。仍然不确定如何让消息框在用户使用数字UpDown和using DateTime设置的特定时间显示。最简单的是,我们可以有一个计时器,每分钟启动一次,检查提醒日期是否在过去,如果是,则显示提醒文本,然后停止计时器。要安排另一个提醒,请选择将来的日期,然后再次启动计时器: private void RemindButton_Click(object sender, EventArgs e) { string Fin

所以,我把它存储在by按钮中,它只是将所有用户的输入连接起来。仍然不确定如何让消息框在用户使用数字UpDown和using DateTime设置的特定时间显示。

最简单的是,我们可以有一个计时器,每分钟启动一次,检查提醒日期是否在过去,如果是,则显示提醒文本,然后停止计时器。要安排另一个提醒,请选择将来的日期,然后再次启动计时器:

private void RemindButton_Click(object sender, EventArgs e)
    {
        string FinalMessage = "";

        //add text from the radio buttons 

        if (MathRadioButton.Checked == true)
        {
            FinalMessage += MathRadioButton.Text;
        }

        if (HistoryRadioButton.Checked == true)
        {
            FinalMessage += HistoryRadioButton.Text;
        }

        if (EnglishRadioButton.Checked == true)
        {
            FinalMessage += EnglishRadioButton.Text;
        }

        if (ScienceRadioButton.Checked == true)
        {
            FinalMessage += ScienceRadioButton.Text;
        }

        if (ElectiveRadioButton.Checked == true)
        {
            FinalMessage += ElectiveRadioButton.Text;
        }

        FinalMessage += " work from ";
        string Teacher_Name = TeacherNameTextBox.Text;
        FinalMessage += Teacher_Name + " is due on ";

        string Month = MonthNumericUpDown.Value.ToString();
        string Day = DayNumericUpDown.Value.ToString();
        string Str_Time = TimeNumericUpDown.Value.ToString();
       // int Int_Time = (int)TimeNumericUpDown.Value;

        FinalMessage += Month + "/" + Day + " at " + Str_Time;
        FinalMessage += " and is about " + DescriptionTextBox.Text;

        MessageBox.Show(FinalMessage);



}
我将把它留作一个练习,让您设置提醒文本(您已经将其作为最终消息)和_rementerDate(需要对您的输入进行一些解析,记住在将来将其设置为日期,因为这是我们的逻辑成功的方式(如果时间是过去的,消息将立即显示).设置好这些后,启动计时器

如果你想要一个重复的提醒,而不是停止计时器,重新安排未来另一个日期的提醒日期(例如明天同一时间)


Christopher评论说,我们可以设置计时器,使其在提醒日期触发-似乎是一个合理的选择:计算提醒日期和现在之间的毫秒数,并将计时器的间隔设置为该值


如果你有一个定时触发的计时器,你可以更新UI,在下一次提醒之前提供几分钟的倒计时-你编写的代码很大程度上取决于你想做什么。最简单的是,我们可以有一个每分钟触发一次的计时器,检查提醒日期是否在过去,如果是,它会显示提醒文本和时间en停止计时器。若要安排另一个提醒,请选择将来的日期,然后再次启动计时器:

private void RemindButton_Click(object sender, EventArgs e)
    {
        string FinalMessage = "";

        //add text from the radio buttons 

        if (MathRadioButton.Checked == true)
        {
            FinalMessage += MathRadioButton.Text;
        }

        if (HistoryRadioButton.Checked == true)
        {
            FinalMessage += HistoryRadioButton.Text;
        }

        if (EnglishRadioButton.Checked == true)
        {
            FinalMessage += EnglishRadioButton.Text;
        }

        if (ScienceRadioButton.Checked == true)
        {
            FinalMessage += ScienceRadioButton.Text;
        }

        if (ElectiveRadioButton.Checked == true)
        {
            FinalMessage += ElectiveRadioButton.Text;
        }

        FinalMessage += " work from ";
        string Teacher_Name = TeacherNameTextBox.Text;
        FinalMessage += Teacher_Name + " is due on ";

        string Month = MonthNumericUpDown.Value.ToString();
        string Day = DayNumericUpDown.Value.ToString();
        string Str_Time = TimeNumericUpDown.Value.ToString();
       // int Int_Time = (int)TimeNumericUpDown.Value;

        FinalMessage += Month + "/" + Day + " at " + Str_Time;
        FinalMessage += " and is about " + DescriptionTextBox.Text;

        MessageBox.Show(FinalMessage);



}
我将把它留作一个练习,让您设置提醒文本(您已经将其作为最终消息)和_rementerDate(需要对您的输入进行一些解析,记住在将来将其设置为日期,因为这是我们的逻辑成功的方式(如果时间是过去的,消息将立即显示).设置好这些后,启动计时器

如果你想要一个重复的提醒,而不是停止计时器,重新安排未来另一个日期的提醒日期(例如明天同一时间)


Christopher评论说,我们可以设置计时器,使其在提醒日期触发-似乎是一个合理的选择:计算提醒日期和现在之间的毫秒数,并将计时器的间隔设置为该值


如果您有一个定时启动的计时器,您可以更新UI,在几分钟内提供倒计时,直到下一个提醒-您编写的代码在很大程度上取决于您使用asynchronous
Task.Delay()执行什么操作。
您可以用一种更简单的方式来完成,而无需使用计时器的锅炉板代码

计算从现在到提供的时间之间的延迟时间。
使用
DateTimePicker
选择有效的日期和时间,而不是多个数字框

通过添加
async
关键字使事件处理程序异步

public class Reminder{

    private Timer _t = new Timer();

    private string _reminderText = "Remember remember!";
    private string _reminderDate = new DateTime(2019, 11, 16, 12, 34, 56); //12:34:56 on 16 nov 2019

    public Reminder(){ //constructor
        _t.Tick += TimerTick;
        _t.Interval = 60000;//60 seconds
    }

    private void TimerTick(Object sender, EventArgs e){
        if(DateTime.Now > _reminderDate)
        {
            _t.Stop();
            MessageBox.Show(_reminderText);

        }
    }

    private void RemindButton_Click(object sender, EventArgs e){

        //add code in here to set the _reminderText, _reminderDate, and finally Start() the timer

    }
}
正如@BenVoigt所建议的那样
Task.Delay
方法具有重载,该重载接受
CancellationToken
,您可以稍后使用取消令牌取消提醒。
下面是只处理一个提醒的简化方法

private async void RemindButton_Click(object sender, EventArgs e)
{
    string FinalMessage = "";

    //add text from the radio buttons 
    // ...

    FinalMessage += " work from ";
    string Teacher_Name = TeacherNameTextBox.Text;
    FinalMessage += Teacher_Name + " is due on ";

    DateTime reminderTime = DateTimePicker.Value;

    FinalMessage += {reminderTime:yyyy/MM/dd 'at' HH:mm}; // will print 2019/11/25 at 12:23
    FinalMessage += " and is about " + DescriptionTextBox.Text;

    // calculate delay from now
    var messageDelay = reminderTime - DateTime.Now;

    await Task.Delay(messageDelay);

    MessageBox.Show(FinalMessage);       
}
在提醒按钮中,创建源代码并将令牌传递给任务。延迟

// instance variable
private CancellationTokenSource _reminderTokenSource;

private async void CancelRimnderButton_Click(object sender, EventArgs e)
{
    _reminderTokenSource?.Cancel();
}    

使用asynchronous
Task.Delay()
可以以一种更简单的方式完成,而无需定时器的锅炉板代码

计算从现在到提供的时间之间的延迟时间。
使用
DateTimePicker
选择有效的日期和时间,而不是多个数字框

通过添加
async
关键字使事件处理程序异步

public class Reminder{

    private Timer _t = new Timer();

    private string _reminderText = "Remember remember!";
    private string _reminderDate = new DateTime(2019, 11, 16, 12, 34, 56); //12:34:56 on 16 nov 2019

    public Reminder(){ //constructor
        _t.Tick += TimerTick;
        _t.Interval = 60000;//60 seconds
    }

    private void TimerTick(Object sender, EventArgs e){
        if(DateTime.Now > _reminderDate)
        {
            _t.Stop();
            MessageBox.Show(_reminderText);

        }
    }

    private void RemindButton_Click(object sender, EventArgs e){

        //add code in here to set the _reminderText, _reminderDate, and finally Start() the timer

    }
}
正如@BenVoigt所建议的那样
Task.Delay
方法具有重载,该重载接受
CancellationToken
,您可以稍后使用取消令牌取消提醒。
下面是只处理一个提醒的简化方法

private async void RemindButton_Click(object sender, EventArgs e)
{
    string FinalMessage = "";

    //add text from the radio buttons 
    // ...

    FinalMessage += " work from ";
    string Teacher_Name = TeacherNameTextBox.Text;
    FinalMessage += Teacher_Name + " is due on ";

    DateTime reminderTime = DateTimePicker.Value;

    FinalMessage += {reminderTime:yyyy/MM/dd 'at' HH:mm}; // will print 2019/11/25 at 12:23
    FinalMessage += " and is about " + DescriptionTextBox.Text;

    // calculate delay from now
    var messageDelay = reminderTime - DateTime.Now;

    await Task.Delay(messageDelay);

    MessageBox.Show(FinalMessage);       
}
在提醒按钮中,创建源代码并将令牌传递给任务。延迟

// instance variable
private CancellationTokenSource _reminderTokenSource;

private async void CancelRimnderButton_Click(object sender, EventArgs e)
{
    _reminderTokenSource?.Cancel();
}    

计时器是您正在寻找的droid。DateTime仅在您获得某种形式的轮询时对您有帮助。我强烈建议您不要使用它。请注意,.NET中有5个以上的计时器,使用哪一个取决于您的显示技术。WPF/UWP、WindowsForms、ASP.NET、其他?请指定以便我们可以帮助您。Windows form AppOh wait,这是Callendar/Reminder应用程序。我甚至没有看大部分代码。是的,为此你需要一个轮询循环。计时器是你正在寻找的机器人。DateTime只会在你有某种轮询形式的情况下对你有帮助。我强烈建议不要使用。请注意,.NET中有5个以上的计时器,使用哪一个取决于你的显示技术。WPF/UWP,Windows窗体、ASP.Net、其他?请指定以便我们可以帮助您。Windows窗体AppOh wait,这是一个调用者/提醒应用程序。我甚至没有查看大部分代码。是的,为此,您需要一个轮询循环。计算提醒日期和现在之间的毫秒数,并将计时器间隔设置为该值?当然。
sSystem.Timers.Timer
在另一个线程上触发已用事件。小心从那里访问GUI控件。感谢Lars,切换到windows窗体Timer计算提醒日期和现在之间的毫秒数,并将计时器的间隔设置为该值?当然。
System.Timers.Timer
在另一个线程上触发已用事件t线程。小心从那里访问GUI控件。谢谢Lars,切换到windows窗体Timer。唯一的问题是请求延迟后无法与延迟交互……您可能希望重载同时接受timespan和取消令牌。@BenVoigt,是的,但OP没有提供有关延迟的详细信息应该处理w延迟。取消令牌源生存期必须超出事件处理程序的范围,所以我们需要将其保存到实例变量。表单/应用程序