在Windows窗体中,DateTimePicker控件用于选择和显示窗体中具有特定格式的日期/时间。 FlowLayoutPanel类用于表示Windows DateTimePicker控件,还提供不同类型的属性,方法和事件。它在System.Windows.Forms命名空间下定义。您可以创建两种不同类型的DateTimePicker,作为带有以文本表示的日期的下拉列表,或者作为单击给定列表旁边的向下箭头时出现的日历。在C#中,可以使用两种不同的方法在Windows窗体中创建DateTimePicker:
1.设计时:这是创建DateTimePicker控件的最简单方法,如以下步骤所示:
- 第1步:创建一个Windows窗体,如下图所示:
Visual Studio->文件->新建->项目-> WindowsFormApp - 步骤2:接下来,将DateTimePicker控件从工具箱拖放到窗体,如下图所示:
- 步骤3:拖放之后,您将转到DateTimePicker的属性,以根据需要修改DateTimePicker。
输出:
2.运行时:比上述方法有些棘手。在此方法中,可以借助DateTimePicker类提供的语法以编程方式创建DateTimePicker。以下步骤显示如何动态设置创建DateTimePicker:
- 步骤1:使用DateTimePicker类提供的DateTimePicker()构造函数创建DateTimePicker。
// Creating a DateTimePicker DateTimePicker d = new DateTimePicker();
- 步骤2:创建DateTimePicker之后,设置DateTimePicker类提供的DateTimePicker的属性。
// Setting the location of the DateTimePicker d.Location = new Point(360, 162); // Setting the size of the DateTimePicker d.Size = new Size(292, 26); // Setting the maximum date of the DateTimePicker d.MaxDate = new DateTime(2500, 12, 20); // Setting the minimum date of the DateTimePicker d.MinDate = new DateTime(1753, 1, 1); // Setting the format of the DateTimePicker d.Format = DateTimePickerFormat.Long; // Setting the name of the DateTimePicker d.Name = "MyPicker"; // Setting the font of the DateTimePicker d.Font = new Font("Comic Sans MS", 12); // Setting the visibility of the DateTimePicker d.Visible = true; // Setting the value of the DateTimePicker d.Value = DateTime.Today;
- 步骤3:最后,将此DateTimePicker控件添加到表单中,并使用以下语句在DateTimePicker上添加其他控件:
// Adding this control // to the form this.Controls.Add(d);
例子:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace WindowsFormsApp48 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { // Creating and setting the // properties of the Label Label l = new Label(); l.Location = new Point(183, 162); l.Size = new Size(172, 20); l.Text = "Select Date and Time"; l.Font = new Font("Comic Sans MS", 12); // Adding this control // to the form this.Controls.Add(l); // Creating and setting the // properties of the DateTimePicker DateTimePicker d = new DateTimePicker(); d.Location = new Point(360, 162); d.Size = new Size(292, 26); d.MaxDate = new DateTime(2500, 12, 20); d.MinDate = new DateTime(1753, 1, 1); d.Format = DateTimePickerFormat.Long; d.Name = "MyPicker"; d.Font = new Font("Comic Sans MS", 12); d.Visible = true; d.Value = DateTime.Today; // Adding this control // to the form this.Controls.Add(d); } } }
输出:
建设者
Constructor | Description |
---|---|
DateTimePicker() | This constructor is used to initializes a new instance of the DateTimePicker class. |
领域
Fields | Description |
---|---|
DefaultMonthBackColor | This is field specifies the default month background color of the DateTimePicker control. This field is read-only. |
DefaultTitleBackColor | This is field specifies the default title back color of the DateTimePicker control. This field is read-only. |
DefaultTitleForeColor | This is field specifies the default title foreground color of the DateTimePicker control. This field is read-only. |
DefaultTrailingForeColor | This is field specifies the default trailing foreground color of the DateTimePicker control. This field is read-only. |
MaxDateTime | This is field specifies the maximum date value of the DateTimePicker control. This field is read-only. |
MinDateTime | This is field get the minimum date value of the DateTimePicker control. |
特性
Property | Description |
---|---|
AutoSize | This property is used to get or set a value that indicates whether the control resizes based on its contents. |
AutoSizeMode | This property indicates the automatic sizing behavior of the control. |
BackColor | This property is used to get or set the background color for the control. |
BorderStyle | This property indicates the border style for the control. |
CalendarFont | This property is used to get or set the font style applied to the calendar. |
CalendarForeColor | This property is used to get or set the foreground color of the calendar. |
CalendarMonthBackground | This property is used to get or set the background color of the calendar month. |
CalendarTitleBackColor | This property is used to get or set the background color of the calendar title. |
CalendarTitleForeColor | This property is used to get or set the foreground color of the calendar title. |
CalendarTrailingForeColor | This property is used to get or set the foreground color of the calendar trailing dates. |
Font | This property is used to get or set the font of the text displayed by the control. |
ForeColor | This property is used to get or set the foreground color of the control. |
Format | This property is used to get or set the format of the date and time displayed in the control. |
Height | This property is used to get or set the height of the control. |
Location | This property is used to get or set the coordinates of the upper-left corner of the DateTimePicker control relative to the upper-left corner of its form. |
MaxDate | This property is used to get or set the maximum date and time that can be selected in the control. |
MaximumDateTime | This property is used to get the maximum date value allowed for the DateTimePicker control. |
MinDate | This property is used to get or set the minimum date and time that can be selected in the control. |
MinimumDateTime | This property is used to set the minimum date value allowed for the DateTimePicker control. |
Name | This property is used to get or set the name of the control. |
ShowUpDown | This property is used to get or set a value indicating whether a spin button control (also known as an up-down control) is used to adjust the date/time value. |
ShowCheckBox | This property is used to get or set a value indicating whether a check box is displayed to the left of the selected date. |
Size | This property is used to get or set the height and width of the control. |
Visible | This property is used to get or set a value indicating whether the control and all its child controls are displayed. |
Value | This property is used to get or set the date/time value assigned to the control. |
Width | This property is used to get or set the width of the control. |