簡體   English   中英

如何在 c# 的日期時間選擇器中添加秒數?

[英]How can i add seconds in a date time picker in c#?

我希望當您按下按鈕時,它會為日期計時器選擇器增加 30 秒。 但發生在我身上的是它們沒有加起來。

這是我的代碼:

public partial class Form1 : Form
{
    double seg;

    public void Form1_Load(object sender, EventArgs e)
    {
        this.dateTimePicker1.Value = this.dateTimePicker1.Value.AddSeconds(seg);    
    }
    private void s30_Click(object sender, EventArgs e)
    {
        seg = 30;
    }
}

在點擊事件中更新控件的值:

private void s30_Click(object sender, EventArgs e)
{
    seg = 30;
    this.dateTimePicker1.Value = this.dateTimePicker1.Value.AddSeconds(seg);
}

Form_Load事件僅在首次加載表單時發生。 (Note that this is specific to Windows Forms. In Web Forms you'll need to take this a step further and persist the value somewhere, because every post-back would load a new instance of the form and the value would start over.)

當然,由於seg除了它的硬編碼值之外什么都不會改變,所以你並不真的需要它。 只需將秒數增加該硬編碼值即可:

private void s30_Click(object sender, EventArgs e)
{
    this.dateTimePicker1.Value = this.dateTimePicker1.Value.AddSeconds(30);
}

然后,您可以完全從代碼中刪除seg變量。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM