欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

C# WinForms 时间(时分秒)控件

程序员文章站 2022-06-08 17:58:21
...

时间控件在开发中挺常见,用户只输入时分秒的控件可曾用过。前段时间开发网上没找到,自己撸出来了,今天出来分享一下。先上代码撸一撸


namespace WinForm
{
    partial class MainForm
    {
        /// <summary>
        /// 必需的设计器变量。
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// 清理所有正在使用的资源。
        /// </summary>
        /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows 窗体设计器生成的代码

        /// <summary>
        /// 设计器支持所需的方法 - 不要修改
        /// 使用代码编辑器修改此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            this.demoDateTimePicker = new System.Windows.Forms.DateTimePicker();
            this.getDTButton = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // demoDateTimePicker
            // 
            this.demoDateTimePicker.CustomFormat = "HH:mm:ss";
            this.demoDateTimePicker.Format = System.Windows.Forms.DateTimePickerFormat.Time;
            this.demoDateTimePicker.Location = new System.Drawing.Point(45, 31);
            this.demoDateTimePicker.Name = "demoDateTimePicker";
            this.demoDateTimePicker.ShowUpDown = true;
            this.demoDateTimePicker.Size = new System.Drawing.Size(200, 25);
            this.demoDateTimePicker.TabIndex = 0;
            // 
            // getDTButton
            // 
            this.getDTButton.Location = new System.Drawing.Point(77, 89);
            this.getDTButton.Name = "getDTButton";
            this.getDTButton.Size = new System.Drawing.Size(100, 40);
            this.getDTButton.TabIndex = 1;
            this.getDTButton.Text = "得到数值";
            this.getDTButton.UseVisualStyleBackColor = true;
            this.getDTButton.Click += new System.EventHandler(this.getDTButton_Click);
            // 
            // MainForm
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 15F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(800, 450);
            this.Controls.Add(this.getDTButton);
            this.Controls.Add(this.demoDateTimePicker);
            this.Name = "MainForm";
            this.Text = "MainForm";
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.DateTimePicker demoDateTimePicker;
        private System.Windows.Forms.Button getDTButton;
    }
}


再来看一下咱们撸出来的效果!
C# WinForms 时间(时分秒)控件

可以清楚的看到,这个控件可以随意给出一天中的任意时间。最后我们看一下得到的值:C# WinForms 时间(时分秒)控件
最后得到一个当天的DateTime类型的结果,我们在底层略微处理一下非常完美!