




已阅读5页,还剩4页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
ErrorProvider组件1功能ErrorProvider组件可以在不打扰用户的情况下向用户显示有错误发生。当验证用户在窗体中的输入或显示数据集内的错误时,一般要用到该控件。图1所示为ErrorProvider组件。图1 ErrorProvider组件2属性ErrorProvider组件常用属性及说明如表1所示。表1 ErrorProvider组件常用属性及说明下面对比较重要的属性进行详细介绍。(1)BlinkRate属性。该属性获取或设置错误图标的闪烁速率。语法:public int BlinkRate get; set; 属性值:错误图标的闪烁速率(以ms为单位)。默认值为250ms。(2)BlinkStyle属性。获取或设置一个值,通过该值指示错误图标的闪烁时间。语法:public ErrorBlinkStyle BlinkStyle get; set; 属性值:ErrorBlinkStyle值之一。默认为BlinkIfDifferentError。ErrorBlinkStyle值有以下几种取值:当值为AlwaysBlink时,表示当错误图标第一次显示时,或者当为控件设置了错误说明字符串并且错误图标已经显示时,总是闪烁;当值为BlinkIfDifferentError时,表示当图标已经显示并且为控件设置了新的错误字符串时闪烁;当值为NeverBlink时,表示错误图标从不闪烁。示例BlinkRate属性和BlinkStyle属性的使用本示例通过设置BlinkRate属性指定错误图标的闪烁速率,通过设置BlinkStyle属性指定错误图标的闪烁时间,运行结果如图2所示。图2 BlinkRate属性和BlinkStyle属性程序主要代码如下:this.errorProvider1.BlinkRate = 100;this.errorProvider1.BlinkStyle = ErrorBlinkStyle.AlwaysBlink;完整程序代码如下:主程序文件完整程序代码using System;using System.Collections.Generic;using System.Windows.Forms;namespace _8_27static class Program/ / 应用程序的主入口点。/ STAThreadstatic void Main()Application.EnableVisualStyles();Application.SetCompatibleTextRenderingDefault(false);Application.Run(new frmErrorProvider();using System;using System.Drawing;using System.Windows.Forms;namespace _8_27public class ErrorProvider23 : System.Windows.Forms.Formprivate System.Windows.Forms.Label label1;private System.Windows.Forms.Label label2;private System.Windows.Forms.Label label4;private System.Windows.Forms.Label label5;private System.Windows.Forms.Label label6;private System.Windows.Forms.Label label3;private System.Windows.Forms.TextBox nameTextBox1;private System.Windows.Forms.NumericUpDown ageUpDownPicker;private System.Windows.Forms.ComboBox favoriteColorComboBox;private System.Windows.Forms.ErrorProvider ageErrorProvider;private System.Windows.Forms.ErrorProvider nameErrorProvider;private System.Windows.Forms.ErrorProvider favoriteColorErrorProvider;public ErrorProvider23()TextBox1 = new System.Windows.Forms.TextBox();this.label1 = new System.Windows.Forms.Label();this.label2 = new System.Windows.Forms.Label();this.ageUpDownPicker = new System.Windows.Forms.NumericUpDown();this.favoriteColorComboBox = new System.Windows.Forms.ComboBox();this.label3 = new System.Windows.Forms.Label();this.label4 = new System.Windows.Forms.Label();this.label5 = new System.Windows.Forms.Label();this.label6 = new System.Windows.Forms.Label();/ Name Labelthis.label1.Location = new System.Drawing.Point(56, 32);this.label1.Size = new System.Drawing.Size(40, 23);this.label1.Text = Name:;/ Age Labelthis.label2.Location = new System.Drawing.Point(40, 64);this.label2.Size = new System.Drawing.Size(56, 23);this.label2.Text = Age (3-5);/ Favorite Color Labelthis.label3.Location = new System.Drawing.Point(24, 96);this.label3.Size = new System.Drawing.Size(80, 24);this.label3.Text = Favorite color;/ ErrorBlinkStyle.AlwaysBlink Labelthis.label4.Location = new System.Drawing.Point(264, 32);this.label4.Size = new System.Drawing.Size(160, 23);this.label4.Text = ErrorBlinkStyle.AlwaysBlink;/ ErrorBlinkStyle.BlinkIfDifferentError Labelthis.label5.Location = new System.Drawing.Point(264, 64);this.label5.Size = new System.Drawing.Size(200, 23);this.label5.Text = ErrorBlinkStyle.BlinkIfDifferentError;/ ErrorBlinkStyle.NeverBlink Labelthis.label6.Location = new System.Drawing.Point(264, 96);this.label6.Size = new System.Drawing.Size(200, 23);this.label6.Text = ErrorBlinkStyle.NeverBlink;/ Name TextBTextBox1.Location = new System.Drawing.Point(112, 32);TextBox1.Size = new System.Drawing.Size(120, 20);TextBox1.TabIndex = 0;TextBox1.Validated += new System.EventHandler(TextBox1_Validated);/ Age NumericUpDownthis.ageUpDownPicker.Location = new System.Drawing.Point(112, 64);this.ageUpDownPicker.Maximum = new System.Decimal(new int 150,0,0,0);this.ageUpDownPicker.TabIndex = 4;this.ageUpDownPicker.Validated += new System.EventHandler(this.ageUpDownPicker_Validated);/ Favorite Color ComboBoxthis.favoriteColorComboBox.Items.AddRange(new object None,Red,Yellow,Green,Blue,Purple);this.favoriteColorComboBox.Location = new System.Drawing.Point(112, 96);this.favoriteColorComboBox.Size = new System.Drawing.Size(120, 21);this.favoriteColorComboBox.TabIndex = 5;this.favoriteColorComboBox.Validated += new System.EventHandler(this.favoriteColorComboBox_Validated);/ Set up how the form should be displayed and add the controls to the form.this.ClientSize = new System.Drawing.Size(464, 150);this.Controls.AddRange(new System.Windows.Forms.Control this.label6,this.label5,this.label4,this.label3,this.favoriteColorComboBox,this.ageUpDownPicker,this.label2,this.label1,TextBox1);this.Text = Error Provider Example;/ Create and set the ErrorProvider for each data entry ErrorProvider = new System.Windows.Forms.ErrorProvider();nameErrorProvider.SetIconAlignment (TextBox1, ErrorIconAlignment.MiddleRight);nameErrorProvider.SetIconPadding (TextBox1, 2);nameErrorProvider.BlinkRate = 1000;nameErrorProvider.BlinkStyle = System.Windows.Forms.ErrorBlinkStyle.AlwaysBlink;ageErrorProvider = new System.Windows.Forms.ErrorProvider();ageErrorProvider.SetIconAlignment (this.ageUpDownPicker, ErrorIconAlignment.MiddleRight);ageErrorProvider.SetIconPadding (this.ageUpDownPicker, 2);ageErrorProvider.BlinkStyle = System.Windows.Forms.ErrorBlinkStyle.BlinkIfDifferentError;favoriteColorErrorProvider = new System.Windows.Forms.ErrorProvider();favoriteColorErrorProvider.SetIconAlignment (this.favoriteColorComboBox, ErrorIconAlignment.MiddleRight);favoriteColorErrorProvider.SetIconPadding (this.favoriteColorComboBox, 2);favoriteColorErrorProvider.BlinkRate = 1000;favoriteColorErrorProvider.BlinkStyle = System.Windows.Forms.ErrorBlinkStyle.NeverBlink;private void nameTextBox1_Validated(object sender, System.EventArgs e)if(IsNameValid()/ Clear the error, if any, in the error ErrorProvider.SetError(TextBox1, );else/ Set the error if the name is not ErrorProvider.SetError(TextBox1, Name is required.);private void ageUpDownPicker_Validated(object sender, System.EventArgs e)if (IsAgeTooYoung()/ Set the error if the age is too young.ageErrorProvider.SetError(this.ageUpDownPicker, Age not old enough);else if (IsAgeTooOld()/ Set the error if the age is too old.ageErrorProvider.SetError(this.ageUpDownPicker, Age is too old);else/ Clear the error, if any, in the error provider.ageErrorProvider.SetError(this.ageUpDownPicker, );private void favoriteColorComboBox_Validated(object sender, System.EventArgs e)if (!IsColorValid()/ Set the error if the favorite color is not valid.favoriteColorErrorProvider.SetError(this.favoriteColorComboBox, Must select a color.);else/ Clear the error, if any, in the error provider.favoriteColorErrorProvider.SetError(this.favoriteColorComboBox, );/ Functions to verify data.private bool IsNameValid()/ Determine whether the text box contains a zero-length string.return (nameTextBox1.Text.Length 0);private bool IsAgeTooYoung()/ Determine whether the age value is less than three.return (ageUpDownPicker.Value 5 );private bool IsColorValid()/ Determine whether the favorite color has a valid value.return (favoriteColorComboBox.SelectedItem != null) &(!favoriteColorComboBox.SelectedItem.ToString().Equals(None);private void InitializeComponent()this.SuspendLayout();/ ErrorProvider23/this.ClientSize = new System.Drawing.Size(292, 266);this.Name = ErrorProvider23;this.Load += new System.EventHandler(this.Form1_Load);this.ResumeLayout(false);private void Form1_Load(object sender, EventArgs e)using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;namespace _8_27public partial class frmErrorProvider : Formpublic frmErrorProvider()InitializeComponent();/在验证控件时发生private void textBox1_Validating(object sender, CancelEventArgs e)tryint x = Int32.Parse(textBox1.Text);errorProvider1.SetError(textBox1, );catch (Exception ee)errorProvider1.SetError(textBox1, 请输入一个数);private
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- GSNOR-IN-1-生命科学试剂-MCE
- 2025福建泉州晋江国际机场招聘25人模拟试卷及答案详解(新)
- 安全培训效果评价计划课件
- 2025吉林农业大学招聘高层次人才7人模拟试卷及参考答案详解1套
- 广州货架安全管理培训课件
- 2025年第2批次浙江宁波前湾产业集团有限公司招聘9人考前自测高频考点模拟试题及完整答案详解
- 首饰产品质量承诺书6篇
- 智能制造产业智能化生产
- 2025福建泉州市安溪县部分公办学校专项招聘编制内新任教师65人(三)考前自测高频考点模拟试题及参考答案详解
- 直播带货合伙人合同协议书5篇
- 5.15 探寻新航路 课件 2024-2025学年部编版九年级历史上学期
- 联合投标协议书新(2024版)
- 新《主体结构及装饰装修》考试习题库大全-中(多选题)
- 长期护理机构照护服务记录表(护理员用表)2-4-5
- 2024年江苏省射阳县事业单位招聘35人历年高频考题难、易错点模拟试题(共500题)附带答案详解
- 产品营销市场调查报告
- 传奇地图编号
- 宠物用品授权书
- GB/T 1504-2024铸铁轧辊
- 食品行业创新与研发
- 电力各种材料重量表总
评论
0/150
提交评论