如何在 c sharp .net 的窗口形式应用程序中设置 datatimepicker。当我关注该日期文本框时,会显示我的 datetimepicker 控件,然后单击该日期中的特定日期,然后在文本框中显示 myformatted 输出。我使用简单的 datetimepicker 控件。
我只在 c#.net 的窗口窗体应用程序中使用 datetimepicker 控件
您可以使用ValueChanged
Event indateTimePicker
轻松完成此操作。
private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
{
textBox1.Text = YourFormatedMethod(dateTimePicker1.Text);
}
private string YourFormatedMethod(string selectedDate)
{
// Your implementation
}
您可以使用数据绑定:
将其放在表单加载中:
//textBox1.DataBindings.Add("Text", dateTimePicker1, "Text");
//or...
Binding b = new Binding("Text", dateTimePicker1, "Value",true);
b.Format += new ConvertEventHandler(b_Format);
textBox1.DataBindings.Add(b);
然后是 b_Format 处理程序:
private void b_Format(object sender, ConvertEventArgs e)
{
//some checking you might want to do...
e.Value = //your formatting here.
}
private void dateTimePickerBillDate_ValueChanged(object sender, EventArgs e)
{
DateTime pickdt = Convert.ToDateTime(dateTimePickerBillDate.Text);
maskedtxtBillDate.Text = pickdt.ToString();
}
// date time picker event code
private void dtpfromdate_ValueChanged(object sender, EventArgs e){
dtpfromdate.Format = DateTimePickerFormat.Custom;
//here we can select our desired format.
dtpfromdate.CustomFormat = "dd-MM-yyyy";
txtfromdate.Text = dtpfromdate.CustomFormat;
//assign the datetime picker value to textbox.
txtfromdate.Text = dtpfromdate.Value.ToString("dd-MM-yyyy");
}