Go homepage(回首页) Upload pictures (上传图片) Write articles (发文字帖)
The author:(作者)qqpublished in(发表于) 2014/7/11 9:16:09 C#中DateTimePicker控件应用实例
C#中DateTimePicker控件应用实例
DateTimePicker控件功能用户可以从日期或时间列表中选择单个项,用来表示日期。该控件显示为两部分:一部分为下拉列表(以文本形式表示的日期),另一部分为网格(在单击列表旁边的向下箭头时显示)。图1为DateTimePicker控件。图1 DateTimePicker控件2.属性DateTimePicker控件常用属性及说明如表1所示。表1 DateTimePicker控件常用属性及说明下面详细介绍Value属性,此属性是指当前控件的日期或时间值。语法:public DateTime Value { get; set; }属性值:分配给控件的DateTime值。说明:如果Value属性未在代码中或被用户更改,它将设置为当前的日期和时间(DateTime.Now)。示例使用Value属性获得控件当前选中值本示例通过使用Value属性,把控件当前选中的值赋给一个文本框。程序主要代码如下:textBox1.Text = dateTimePicker1.Value.ToString();3.方法ToString方法用于返回表示当前DateTimePicker控件的字符串。语法:public override string ToString ()返回值:返回表示当前DateTimePicker的字符串,该字符串包括该控件的类型和Value属性。完整程序代码如下:★★★★★主程序文件完整程序代码★★★★★using System;using System.Collections.Generic;using System.Windows.Forms;namespace _8_13{static class Program{/// /// 应用程序的主入口点。/// [STAThread]static void Main(){Application.EnableVisualStyles();Application.SetCompatibleTextRenderingDefault(false);Application.Run(new frmData());}}}★★★★★frmData窗体设计文件完整程序代码★★★★★using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;namespace _8_13{public partial class frmData : Form{public frmData(){InitializeComponent();}private void dateTimePicker1_CloseUp(object sender, EventArgs e){this.textBox1.Text = dateTimePicker1.Value.ToString();}private void button1_Click(object sender, EventArgs e){}private void dateTimePicker1_ValueChanged(object sender, EventArgs e){}private void frmData_Load(object sender, EventArgs e){}}}★★★★★frmData窗体代码文件完整程序代码★★★★★namespace _8_13{partial class frmData{/// /// 必需的设计器变量。/// private System.ComponentModel.IContainer components = null;/// /// 清理所有正在使用的资源。/// /// 如果应释放托管资源,为 true;否则为 false。protected override void Dispose(bool disposing){if (disposing && (components != null)){components.Dispose();}base.Dispose(disposing);}#region Windows 窗体设计器生成的代码/// /// 设计器支持所需的方法 - 不要/// 使用代码编辑器修改此方法的内容。/// private void InitializeComponent(){this.dateTimePicker1 = new System.Windows.Forms.DateTimePicker();this.textBox1 = new System.Windows.Forms.TextBox();this.button1 = new System.Windows.Forms.Button();this.SuspendLayout();//// dateTimePicker1//this.dateTimePicker1.CustomFormat = "yyyy-mm-dd";this.dateTimePicker1.Location = new System.Drawing.Point(36, 41);this.dateTimePicker1.Name = "dateTimePicker1";this.dateTimePicker1.Size = new System.Drawing.Size(126, 21);this.dateTimePicker1.TabIndex = 0;this.dateTimePicker1.ValueChanged += new System.EventHandler(this.dateTimePicker1_ValueChanged);this.dateTimePicker1.CloseUp += new System.EventHandler(this.dateTimePicker1_CloseUp);//// textBox1//this.textBox1.Location = new System.Drawing.Point(36, 80);
赞