[英]How can i add seconds in a date time picker in c#?
I want that when you press a button, it adds 30 seconds to the date timer picker.我希望当您按下按钮时,它会为日期计时器选择器增加 30 秒。 but what happens to me is that they do not add up.但发生在我身上的是它们没有加起来。
This is my code:这是我的代码:
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;
}
}
Update the control's value in the click event:在点击事件中更新控件的值:
private void s30_Click(object sender, EventArgs e)
{
seg = 30;
this.dateTimePicker1.Value = this.dateTimePicker1.Value.AddSeconds(seg);
}
The Form_Load
event only happens when the form is first loaded. 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.) (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.)
Of course, since seg
never changes to anything but its hard-coded value, you don't really need it.当然,由于seg
除了它的硬编码值之外什么都不会改变,所以你并不真的需要它。 Just increment the seconds by that hard-coded value:只需将秒数增加该硬编码值即可:
private void s30_Click(object sender, EventArgs e)
{
this.dateTimePicker1.Value = this.dateTimePicker1.Value.AddSeconds(30);
}
Then you can remove the seg
variable from your code entirely.然后,您可以完全从代码中删除seg
变量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.