本文共 9069 字,大约阅读时间需要 30 分钟。
1、DateTimePicker控件(日期控件)
DateTimePicker控件用于选择日期和时间,只能够选择一个时间而不是连续的时间段,也可以直接输入日期和时间。
DateTimePicker显示为两部分:一部分为下拉列表,用文本形式表示的日期,另一部分为网格。(在单击列表旁边的箭头显示)
(1)使用DateTimePicker控件显示时间
通过将控件的Format属性设置为Time,可以实现控件只显示时间。Format属性用于获取或设置控件中显示的日期和时间格式。
语法:
public DateTimePickerFormat Format{ get;set;}//属性值表示DateTimePicker值之一,默认为Long
DateTimePickerFormat枚举的值如下:
Custom:DateTimePicker控件以自定义格式显示日期/时间值。
Long:DateTimePicker控件以用户操作系统设置的长日期格式显示日期/时间值。
Short:DateTimePicker控件以用户操作系统设置的短日期格式显示日期时间值。
Time:DateTimePicker控件以用户操作系统设置的时间格式显示日期时间值。
示例程序:基本操作
public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void dateTimePicker1_ValueChanged(object sender, EventArgs e) { dateTimePicker1.Format = DateTimePickerFormat.Short; textBox1.Text = dateTimePicker1.Text; } private void Form1_Load(object sender, EventArgs e) { dateTimePicker1.Format = DateTimePickerFormat.Short; textBox1.Text = dateTimePicker1.Text; } }
注意:如果要设置dateTimePicker控件的显示日期和字体式样,使用CalendarFont属性。
(2)使用DateTimePicker控件以自定义格式显示日期
通过dateTimePicker控件的CustomFormat属性可以自定义日期时间格式字符串。
语法:
public string CustomFormat{ get;set;}//属性值表示自定义日期时间格式的字符串
所有有效格式字符串以及其说明如下 :
d:一位数或两位数的天数。
dd:两位数的天数,一位数天数的前面+0.
ddd:三个字符的星期几缩写。
dddd:完整的星期几名称。
h:12小时格式的一位数或两位数小时数。
hh:12小时格式的两位数小时数,一位数数值前面+0.
H:24小时格式的一位数或两位数小时数。
HH:24小时格式的两位数小时数。一位数数值前面+0。
m:一位数或两位数分钟值。
mm:两位数分钟值,一位数数值前面+0.
M:一位数或两位数月份至。
MM:两位数月份值,一位数前面+0.
MMM:三个字符的月份缩写。
MMMM:月份的完整名称。
s:一位或者两位s数。
ss:两位秒数,一位数数值前面+0.
t:单字母AM,PM缩写,A,P
tt:双字母AM,PM
y:一位数年份。
yy:年份的最后徐两位数。
yyy:年份的完整名称。
示例程序:
public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void dateTimePicker1_ValueChanged(object sender, EventArgs e) { textBox1.Text = dateTimePicker1.Text; } private void Form1_Load(object sender, EventArgs e) { dateTimePicker1.Format = DateTimePickerFormat.Custom; //设置其属性为自定义 dateTimePicker1.CustomFormat = "MMMM dd,yyyy-dddd"; textBox1.Text = dateTimePicker1.Text; } }
(3)返回dateTimePicker控件中所选择的日期
调用控件中的Text属性从而返回和控件中的格式相同的完整值,或调用Value属性的适当方法来返回部分值。这些方法包括Year,Month和Day方法等,使用ToString将信息转换为可以显示给用户的相关字符串。
示例程序:
public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void dateTimePicker1_ValueChanged(object sender, EventArgs e) { textBox1.Text = dateTimePicker1.Text; textBox2.Text = dateTimePicker1.Value.Year.ToString(); textBox3.Text = dateTimePicker1.Value.Month.ToString(); textBox4.Text = dateTimePicker1.Value.Day.ToString(); } private void Form1_Load(object sender, EventArgs e) { dateTimePicker1.Format = DateTimePickerFormat.Custom; //设置其属性为自定义 dateTimePicker1.CustomFormat = "MMMM dd,yyyy-dddd"; textBox1.Text = dateTimePicker1.Text; textBox2.Text = dateTimePicker1.Value.Year.ToString(); textBox3.Text = dateTimePicker1.Value.Month.ToString(); textBox4.Text = dateTimePicker1.Value.Day.ToString(); } }
2、其他高级控件
(1)使用ErrorProvider控件验证文本框输入
ErrorProvider控件可以在不打扰用户的情况下向用户显示有错误发生。当验证用户在窗体中的输入或显示数据集内的错误的时候,一般要用到该控件。
一般使用SetError方法来设置指定控件的错误描述字符。语法:
public void SetError(Control control,string value)//control:表示要为期设置错误描述字符串的控件。//value:表示错误描述字符串。
判断文本框中输入的数据是否准确,需要在控件的Validating事件中进行判断,然后设置ErrorProvider控件的错误描述字符串,当控件正在验证的时候会引发此事件。
如果要设置错误图标的闪烁时间,可以使用控件的BlinkStyle属性 。
示例程序:用户注册程序,查看用户注册时的错误。用户输入信息要求不符合的时候,就会出现错误提示。
在调用C#regex类的时候必须using System.Text.RegularExpressions;注意!
public partial class Form1 : Form { bool isError = true; public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { if (isError) { MessageBox.Show("successed", " ", MessageBoxButtons.OK, MessageBoxIcon.Warning); } else { MessageBox.Show("faile", " ", MessageBoxButtons.OK, MessageBoxIcon.Hand); } } private void Form1_Load(object sender, EventArgs e) { //关于提示的附加: toolTip1.SetToolTip(textBox1,"必填,要求至少两个汉字"); toolTip1.SetToolTip(textBox2,"必填,要求至少五个字符,只能包含数字和字母"); toolTip1.SetToolTip(textBox3,"必填再次输入密码"); //关于出现错误提示的设置方法: errorProvider1.BlinkStyle = ErrorBlinkStyle.AlwaysBlink; } private void textBox3_TextChanged(object sender, EventArgs e) { if (textBox3.Text != textBox2.Text) { errorProvider1.SetError(textBox3, "the same"); isError = false; } } private void textBox1_TextChanged(object sender, EventArgs e) { if (!Regex.IsMatch(textBox1.Text, "^[\u4e00-\u9fa5]{2,}$")) { errorProvider1.SetError(textBox1, "you must do this"); isError = false; } } private void textBox2_TextChanged(object sender, EventArgs e) { if (!Regex.IsMatch(textBox2.Text, @"^\w{5,}$")) { errorProvider1.SetError(textBox2, "you must do this"); isError = false; } } }
(2)使用HelpProvider控件调用帮助文件
HelpProvider可以帮助文件(.html或者.chm文件)与Windows应用程序相关联,为特定对话框或者对话框中的特定控件提供区分上下文的帮助。打开帮助文件到特定部分,如目录,索引,或者搜索功能的主页。通过设置控件的HelpNamespace属性以及SetShowHelp方法,实现当按下F1的时候,打开指定的帮助文件。
语法:
HelpNamespace属性可以设置一个值,该值指定和HelpProvider对象相关联的帮助文件名:
public virtual string HelpNamespace{ get;set;}//属性值表示帮助文件的名称
SetShowHelp方法用于指定是否显示指定控件的帮助信息:
public virtual void SetShowHelp(Control ctl,bool value)//ctl表示控制其帮助信息已打开或关闭。value表示如果显示控件的帮助信息,则为true,否则为false。
注意:在ShowHelp方法或者HelpNamespace属性中制定了帮助文件的路径,则使用相对路径是可能会遇到问题。因此一定要使用绝对文件路径来指定帮助文件。
示例程序:
namespace HelpProviderTest{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void 帮助ToolStripMenuItem_Click(object sender, EventArgs e) { helpProvider1.SetShowHelp(this,true); //为窗体设置帮助信息。 } private void Form1_Load(object sender, EventArgs e) { string path = @"F:\学习文件\HTML5练习\index10.html"; helpProvider1.HelpNamespace = path; helpProvider1.SetShowHelp(this,true); } }}
(3)使用Timer控件设置时间间隔
Timer控件可以定期引发事件,此控件为Windows窗体环境设计的。时间间隔的长度由Interval属性定义,其值以毫秒为单位。启用了该组件,则每个时间间隔引发一个Tick时间,然后在该事件中添加要执行的代码。
Interval属性用于设置计时器开始计时的时间间隔。语法:
public int Interval{ get;set;}//属性值表示计时器每次开始计时之间的毫秒数。该值不小于1。
当指定的计时器间隔已过去而且计时器处于启动状态时会引发控件的Tick事件。Enabled属性用于设置是否启用计时器。
public virtual bool Enabled{ get;set;}//属性值表示,如果计时器当前处于启用状态,则为true,否则为false。
注意:使用Timer组件必须保证他的Enabled属性为true。
示例程序:使用该组件制作左右飘动的窗体。
private void timer1_Tick(object sender, EventArgs e) { Rectangle rect = Screen.GetWorkingArea(this); if (this.Left != rect.Width - this.Width) { this.Left++; this.Top += 1; } else { timer1.Enabled = false; timer2.Enabled = true; } } private void timer2_Tick(object sender, EventArgs e) { Rectangle rect = Screen.GetWorkingArea(this); if (this.Left==0) { timer1.Enabled = true; timer2.Enabled = false; } else { this.Left--; this.Top -= 1; } }
(4)使用ProgressBar控件显示程序运行的进度条
ProgressBar通过水平放置的方框中显示适当数目的矩形,只是工作的进度。工作完成时,进度条被填满。进度条用于帮助用户了解等待一项工作完成的进度。
ProgressBar主要属性有Value、Minimum和Maximum。Minimum和Maximum主要设置进度条的最小值和最大值,Value属性表示操作过程中已经完成的进度。而控件的Step属性用于指定Value属性递增的值。调用PerformStep方法来递增该值。
示例程序:
public partial class Form1 : Form { public int IntpercentValue; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { } private void timer1_Tick(object sender, EventArgs e) { if (this.progressBar1.Value == this.progressBar1.Maximum) { this.progressBar1.Value = this.progressBar1.Minimum; } else { this.progressBar1.PerformStep(); } IntpercentValue = 100 * (progressBar1.Value - progressBar1.Minimum) / (progressBar1.Maximum - progressBar1.Minimum); label1.Text = IntpercentValue.ToString() + "%"; } private void button1_Click(object sender, EventArgs e) { if (timer1.Enabled) { timer1.Enabled = false; button1.Text = "开始"; } else { timer1.Enabled = true; button1.Text = "停止"; } } }
注意:如果step设置为大于1的话,进度条走不到头就会结束。只有设置为1才能完整走完进度条而且是100%。
转载地址:http://lysr.baihongyu.com/