如何将日历控件中的日期插入Windows窗体的文本框中 [英] How to insert date from a calendar control into text box for windows forms
本文介绍了如何将日历控件中的日期插入Windows窗体的文本框中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
private void monthCalendar1_DateChanged(object sender, DateRangeEventArgs e)
{
monthCalendar1.DateSelected = txtdatein.Text;
}
/*我已经键入此代码,以便接受日历控件"monthCalendar1"中的日期到文本框"txtdatein"的日期,但是会产生错误.我知道asp.net的代码,但不知道ado.net的代码,我希望知道. */
产生的错误是
错误1事件"System.Windows.Forms.MonthCalendar.DateSelected"只能出现在+ =或-=
/* i have typed this code inorder to accept date from my calendar control "monthCalendar1" to my text box "txtdatein" but it generates error. I know the code for asp.net but not for ado.net, which i am expecting to know. */
the error generated is
Error 1 The event ''System.Windows.Forms.MonthCalendar.DateSelected'' can only appear on the left hand side of += or -=
推荐答案
的左侧您要在Windows Forms程序中使用DateTimePicker控件.
dateTimePicker1_ValueChanged.此事件处理程序在DateTimePicker上非常有用.每当用户更改DateTime时,都会执行此事件处理程序.
尝试这种方式:
i think You want to use the DateTimePicker control in your Windows Forms program.
dateTimePicker1_ValueChanged. This event handler is very useful on the DateTimePicker. Whenever the user changes the DateTime, this event handler is executed.
try this way :
private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
{
// Set title bar to selected date.
DateTime result = dateTimePicker1.Value;
TExtbox1.Text= = result.ToString();
}
此控件通常使用MonthCalendar.SelectionRange
属性显示单个日期,而不显示日期范围.您可以使用MonthCalendar.SelectionRange.Start
或MonthCalendar.SelectionStart
在文本框中显示此范围的开始.
请参阅:
http://msdn.microsoft.com/en-us/library/system.windows.forms.monthcalendar.selectionrange.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/system.windows.forms.selectionrange.start.aspx [ ^ ].
您需要使用具有适当格式的System.DateTime.ToString
获取时间的字符串表示形式(System.DateTime
结构).请参阅:
http://msdn.microsoft.com/en-us/library/system.datetime.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/zdtaw1bw.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/az4se3k1.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx [ ^ ].
最后两个链接引用了有关如何使用日期/时间格式的MSDN文章.
—SA
This control generally shows not a single date, but a range of dates, using the property ofMonthCalendar.SelectionRange
. You can display the start of this range in you text box, usingMonthCalendar.SelectionRange.Start
orMonthCalendar.SelectionStart
.
Please see:
http://msdn.microsoft.com/en-us/library/system.windows.forms.monthcalendar.selectionrange.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.selectionrange.start.aspx[^].
You need to get a string representation of the time (System.DateTime
structure) usingSystem.DateTime.ToString
with appropriate format. Please see:
http://msdn.microsoft.com/en-us/library/system.datetime.aspx[^],
http://msdn.microsoft.com/en-us/library/zdtaw1bw.aspx[^],
http://msdn.microsoft.com/en-us/library/az4se3k1.aspx[^],
http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx[^].
Last two links reference the MSDN article on how to use date/time format.
—SA
您可以在dateTimePicker1_ValueChanged事件中编写以下代码.
Hi,
You can write the folowing code in dateTimePicker1_ValueChanged event.
textBox1.Text= = dateTimePicker1.Value.ToString();
这篇关于如何将日历控件中的日期插入Windows窗体的文本框中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文