[Yugong Series] Detailed Explanation of DateTimePicker Control on Winform Controls in September 2023


foreword

Winform controls are user interface elements in Windows Forms. They can be used to create various visual and interactive components of Windows applications, such as buttons, labels, text boxes, drop-down list boxes, check boxes, radio boxes, progress bars, etc. . Developers can use Winform controls to build user interfaces and respond to user actions to create powerful desktop applications.

1. Detailed Explanation of DateTimePicker Control

The DateTimePicker control is a commonly used date and time selection control in Windows Forms, allowing users to select dates and times from calendars and time pickers.

The following are some common operations using the DateTimePicker control:

  1. Add the DateTimePicker control: In Visual Studio, open the Form designer and select the DateTimePicker control from the toolbox, drag it onto the form to add the control.

  2. Set the format of DateTimePicker: The DateTimePicker control provides a variety of date and time formats, you can customize the format by setting its CustomFormat property.

  3. Get the value of DateTimePicker: You can use the Value property of the DateTimePicker control to get the selected date and time.

  4. Set the minimum and maximum values ​​of the DateTimePicker: You can use the MinDate and MaxDate properties of the DateTimePicker control to set the minimum and maximum values ​​of the date.

  5. Handle the value change event of DateTimePicker: You can use the ValueChanged event of the DateTimePicker control to respond to changes in the value of the control.

For example, the following code demonstrates how to format the date and time of a DateTimePicker control and get its value:

// 设置DateTimePicker控件的自定义格式
dateTimePicker1.CustomFormat = "yyyy年MM月dd日 HH:mm:ss";
dateTimePicker1.Format = DateTimePickerFormat.Custom;

// 获取DateTimePicker控件的值
DateTime selectedDate = dateTimePicker1.Value;
MessageBox.Show("选定的日期和时间为:" + selectedDate.ToString());

1. Attribute introduction

1.1 checked

The Checked property of the DateTimePicker control is used to indicate whether the control is selected (that is, has a value). If the Checked property is True, the control has a valid value, you can use the Value property to get the value; if the Checked property is False, the control does not have a valid value, and the Value property will return DateTime.MinValue.

The following are some common operations using the Checked property of the DateTimePicker control:

  1. Get the checked state of the DateTimePicker control: You can use the Checked property of the DateTimePicker control to get the checked state of the control.

  2. Set the selected state of the DateTimePicker control: You can use the Checked property of the DateTimePicker control to set the selected state of the control.

  3. Handle the CheckedChanged event of the DateTimePicker control: You can use the CheckedChanged event of the DateTimePicker control to respond to changes in the selected state of the control.

For example, the following code demonstrates how to set the value of a DateTimePicker control to the current date and time when it is selected, and clear its value when the DateTimePicker control is deselected:

// 选中DateTimePicker控件时,设置其值为当前日期和时间
private void dateTimePicker1_CheckedChanged(object sender, EventArgs e)
{
    
    
    if (dateTimePicker1.Checked)
    {
    
    
        dateTimePicker1.Value = DateTime.Now;
    }
}

// 取消选中DateTimePicker控件时,清除其值
private void dateTimePicker1_Validating(object sender, CancelEventArgs e)
{
    
    
    if (!dateTimePicker1.Checked)
    {
    
    
        dateTimePicker1.Value = DateTime.MinValue;
    }
}

1.2 CustomFormat

The CustomFormat property of the DateTimePicker control can be used to customize the date and time format display. It allows you to display dates or times in any format you want. The CustomFormat property accepts a string parameter that can contain the following specific format characters:

  • d: Indicates the date, the format is MM/dd/yyyy.
  • t: Indicates the time, the format is hh:mm:ss tt.
  • yyyy: Indicates the four-digit year.
  • MM: Indicates the two-digit month.
  • dd: Indicates a two-digit date.
  • hh: Indicates the hour in 12-hour format.
  • HH: Indicates the hour in 24-hour format.
  • mm: Indicates the number of minutes.
  • ss: Indicates the number of seconds.
  • tt: means "AM" or "PM".

The following is a sample code that sets the DateTimePicker control's CustomFormat property to "yyyy/MM/dd" to display the year/month/day format:

dateTimePicker1.CustomFormat = "yyyy/MM/dd";

After this setting, the date displayed by the control will be presented in the format of "year/month/day". You can customize other date formats as needed.

1.3 font

You can use the following code to change the font property of the DateTimePicker control to Arial, 12 point font:

dateTimePicker1.Font = new System.Drawing.Font("Arial", 12);

Among them, dateTimePicker1 is the name of the specified DateTimePicker control.

1.4 ShowUpDown

The DateTimePicker control is one of the commonly used time selection controls in Winform, which allows users to select a time by selecting a calendar or a drop-down list. The ShowUpDown property can change the DateTimePicker control to only display up and down arrows, and the user can increase or decrease the date or time by clicking the arrow.

Here's an example of using the ShowUpDown property:

  1. In the form designer, drag a DateTimePicker control onto the form.

  2. Open the Properties window and set the ShowUpDown property to True.

  3. Run the program, click the up and down arrows to increase or decrease the date or time.

The sample code is as follows:

private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
{
    
    
    // 显示当前选中的日期和时间
    MessageBox.Show(dateTimePicker1.Value.ToString());
}

In the above example, we bound the ValueChanged event to the DateTimePicker control, which is called when the user changes the date or time. In the event handler, we can get the currently selected date and time and display it in the message box.

1.5 MaxDate and MinDate and value

The DateTimePicker control has three commonly used properties in the Winform form: MaxDate, MinDate and Value.

  1. MaxDate: Set the maximum date that the DateTimePicker control can select. If the user tries to select a date greater than the value of the MaxDate property, it will not take effect. Example usage:
dateTimePicker1.MaxDate = DateTime.Now; // 设置最大日期为当前日期
  1. MinDate: Set the minimum date that the DateTimePicker control can select. If the user tries to select a date that is less than the value of the MinDate attribute, it will not take effect. Example usage:
dateTimePicker1.MinDate = new DateTime(1970, 1, 1); // 设置最小日期为1970年1月1日
  1. Value: Set or get the currently selected date and time of the DateTimePicker control. Example usage:
dateTimePicker1.Value = DateTime.Now; // 设置当前选定日期为当前日期
DateTime selectedDate = dateTimePicker1.Value; // 获取当前选定日期

2. Common scenarios

The DateTimePicker control has many commonly used scenarios in Winform, the following are a few more common scenarios:

  1. Date selection: You can use the DateTimePicker control to allow users to select a date, such as selecting a birthday, meeting date, and so on.

  2. Time selection: You can use the DateTimePicker control to allow users to select a time, such as selecting appointment time, task deadline, etc.

  3. DateTime Selection: You can use the DateTimePicker control to allow users to select a combination of date and time, such as selecting the time when an event occurs.

  4. Custom formatting: You can use the DateTimePicker control to allow users to customize the display format of the date and time to meet different needs.

  5. Data filtering: You can use the DateTimePicker control to filter data between a certain date/time, such as querying sales records within a certain period of time.

  6. Calendar display: You can use the DateTimePicker control to display a month's calendar, allowing users to conveniently select a date.

3. Specific cases

The following is an example of the DateTimePicker control in WinForms:

Suppose you need a control to select appointment date and time, you can use DateTimePicker control to accomplish this task.

  1. First, create a new WinForms Application project in Visual Studio.

  2. Drag and drop a DateTimePicker control on the form and set its properties as follows:

  • Name: dateTimePicker1
  • Format: Custom
  • CustomFormat: yyyy-MM-dd hh:mm tt
  1. Add a button to the form that displays the date and time selected by the user.

  2. Add the following code to the button's Click event handler:

private void button1_Click(object sender, EventArgs e)
{
    
    
    string selectedDateTime = dateTimePicker1.Value.ToString("yyyy-MM-dd hh:mm tt");
    MessageBox.Show("您选择的日期和时间是:" + selectedDateTime);
}
  1. Run the application and select a date and time, then click the button to display the selected date and time.

Through the above steps, you will successfully use the DateTimePicker control to select the date and time, and display it in the message box.

insert image description here

Guess you like

Origin blog.csdn.net/aa2528877987/article/details/132707044