All articles(网络文学目录) All Pictures(图片目录) All Softwares(软件目录)

 
C#中DateTimePicker控件应用实例

Writer: qq Article type: Programming skills(编程技巧) Time: 2014/7/11 9:16:09 Browse times: 312 Comment times: 0

C#中DateTimePicker控件应用实例


Head photo

Go homepage
Upload pictures
Write articles

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);




There are 0 records,
Comment:
Must be registered users to comment(必须是注册用户才能发表评论)

Disclaimer Privacy Policy About us Site Map
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.