One Piece

WinForm控件之【DateTimePicker】

基本介绍

时间控件应用较为广泛,属性设置项也比较完善是非常好用的控件。

常设置属性、事件

CustomFormat:当Format属性设置为自定义类型时可自定义控件时间的显示格式;

Enabled:指示是否启用该控件,true为启用状态可编辑,false为禁用状态不可编辑;

MaxDate:设置控件可选择或输入的最大日期;

MinDate:设置控件可选择或输入的最小日期;

Name:指示代码中用来标识该对象的名称;

ShowUpDown:是否使用下拉日历修改日期,false为下拉日历模式,true为区域数字增减模式;

Text:与控件关联的文本,显示给用户看的内容说明;

ValueChanged事件:控件值更改时发生;

 

事例举例

 

 

 

 

        //切换时间调整模式及显示格式
        private void btn_ChangeType_Click(object sender, EventArgs e)
        {
            string strValue = txt_TimeFormat.Text;
            if (!string.IsNullOrWhiteSpace(strValue))
            {
                this.dateTimePicker1.Format = DateTimePickerFormat.Custom;
                this.dateTimePicker1.CustomFormat = strValue;
                this.dateTimePicker1.ShowUpDown = true;


                //部分时间格式转换
                StringBuilder sb = new StringBuilder();
                strValue = this.dateTimePicker1.Text;
                if (!string.IsNullOrWhiteSpace(strValue))
                {
                    try
                    {
                        DateTime dtime = this.dateTimePicker1.Value;

                        if (string.IsNullOrWhiteSpace(cb_format.Text))
                        {
                            string[] strFormat = new string[cb_format.Items.Count];
                            for (int i = 0, counti = cb_format.Items.Count; i < counti; i++)
                            {
                                strFormat[i] = cb_format.Items[i].ToString();
                            }

                            for (int i = 0, counti = strFormat.Length; i < counti; i++)
                            {
                                string str = strFormat[i];

                                string value1 = string.Format("【{0}→{1}】", strValue, str);
                                sb.AppendFormat("{0,30}{1}", value1, dtime.ToString(str));

                                sb.AppendLine();
                                sb.AppendLine();

                                if (i == counti - 1)
                                {
                                    System.Globalization.GregorianCalendar gc = new System.Globalization.GregorianCalendar();
                                    int weekOfYear = gc.GetWeekOfYear(dtime, System.Globalization.CalendarWeekRule.FirstDay, DayOfWeek.Monday);
                                    value1 = string.Format("【{0}→{1}】", strValue, "DayOfWeek");
                                    sb.AppendFormat("{0,30}{1}年第{2}周", value1, dtime.Year, weekOfYear);

                                    sb.AppendLine();
                                    sb.AppendLine();
                                    value1 = string.Format("【{0}→{1}】", strValue, "DayOfYear");
                                    sb.AppendFormat("{0,30}{1}年第{2}天", value1, dtime.Year, dtime.DayOfYear);

                                    sb.AppendLine();
                                    sb.AppendLine();
                                    value1 = string.Format("【{0}→{1}】", strValue, "Number");
                                    sb.AppendFormat("{0,30}{1}|【基数:{2}】", value1, Helpers.UtilityHelper.DateToNumberValue(dtime, date_Base.Value), date_Base.Value);
                                }
                            }

                        }
                        else
                        {
                            strValue = string.Format("【{0}→{1}】", strValue, cb_format.Text);
                            sb.AppendFormat("{0,30}{1}", strValue, dtime.ToString(cb_format.Text));
                            sb.AppendLine();
                            sb.AppendLine();
                        }


                        txt_TiemResult.Text = sb.ToString();
                    }
                    catch (Exception ex)
                    {
                        Helpers.LogHelper.ErrorMsgRecord(string.Format("【FrmText.btn_ChangeType_Click】{0}", ex.Message), ex, Helpers.UtilityHelper.GetSerialNumber());
                        MessageBox.Show("时间转换报错:" + ex.Message);
                    }
                }
            }
        }

 

 时间控件的下拉日历也有比较完善的属性设置就不一一例举了,至于格式的转换目前也只是例举出了一小部分。

 

posted @ 2019-07-03 18:50  BoneMan  阅读(2852)  评论(0编辑  收藏  举报