Windows Forms Programming In C# 读书笔记 - 第三章 Dialogs (2)

博客展示了C#中Windows Forms编程的代码。包含遍历所有控件的方法,以及在按钮点击、窗口加载、文本框验证等事件中对控件进行验证和信息提示的代码,如手动验证控件、根据文本框内容设置错误或信息提示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1。Data Validation       如何防止用户输入的非法(格式不正确)数据对我们的程序造成影响呢?当然要对用户的输入作出检验

     可以利用控件的 Validating 事件,加上正则表达式来检验用户输入的格式是否正确。但是这样有个缺点:只有当焦点从一个control移动到另一个时,Validating 事件才会被触发,如果有某个 control 有不合法数据,但是从未得到过焦点,它就不会被检验

    解决方法:在 Form 的 ok 按钮的处理事件中加入验证数据的代码。 GetAllControls()是重要方法,以后会经常出现

None.gif// 遍历所有的control,包括子control(比如Group box中的)
None.gif
// 浅层次的control会先返回,子control较后检查
ExpandedBlockStart.gifContractedBlock.gif
Control[] GetAllControls() dot.gif{
InBlock.gif      ArrayList allControls 
= new
 ArrayList();
InBlock.gif      Queue queue 
= new
 Queue();
InBlock.gif      queue.Enqueue(
this
.Controls);
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif   
while( queue.Count > 0 ) dot.gif
{
InBlock.gif           Control.ControlCollection controls 
=
 (Control.ControlCollection)queue.Dequeue();
InBlock.gif          
if( controls == null || controls.Count == 0 ) continue
;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif       
foreach( Control control in controls ) dot.gif
{
InBlock.gif                allControls.Add(control);
InBlock.gif                queue.Enqueue(control.Controls);
ExpandedSubBlockEnd.gif           }

ExpandedSubBlockEnd.gif       }

InBlock.gif      
return (Control[])allControls.ToArray(typeof
(Control));
ExpandedBlockEnd.gif}

None.gif
ExpandedBlockStart.gifContractedBlock.gif
void okButton_Click(object sender, EventArgs e) dot.gif{
InBlock.gif    
// Validate each control manually
ExpandedSubBlockStart.gifContractedSubBlock.gif
      foreach( Control control in GetAllControls() ) dot.gif{
InBlock.gif          
// Validate this control
InBlock.gif
          control.Focus();
ExpandedSubBlockStart.gifContractedSubBlock.gif          
if!this.Validate() ) dot.gif{
InBlock.gif               
this.DialogResult = DialogResult.None;
InBlock.gif               
break;
ExpandedSubBlockEnd.gif          }

ExpandedSubBlockEnd.gif      }

ExpandedBlockEnd.gif}


2。为用户提供帮助(未完成)
    利用ToolTip 以及 ErrorProvider 控件

ExpandedBlockStart.gifContractedBlock.gifvoid LoanApplicationDialog_Load(object sender, EventArgs e) dot.gif{
InBlock.gif  
// Use tooltips to populate the "information provider"
ExpandedSubBlockStart.gifContractedSubBlock.gif
  foreach( Control control in GetAllControls() ) dot.gif{
InBlock.gif    
string toolTip = toolTip1.GetToolTip(control);
InBlock.gif    
if( toolTip.Length == 0 ) continue;
InBlock.gif    infoProvider.SetError(control, toolTip);
ExpandedSubBlockEnd.gif  }

ExpandedBlockEnd.gif}

None.gif
ExpandedBlockStart.gifContractedBlock.gif
void applicantNameTextBox_Validating(object sender, CancelEventArgs e) dot.gif{
InBlock.gif  
string toolTip = toolTip1.GetToolTip((Control)sender);
ExpandedSubBlockStart.gifContractedSubBlock.gif  
if( ((TextBox)sender).Text.Length == 0 ) dot.gif{
InBlock.gif    
// 当text box未空时,提示错误
InBlock.gif
    errorProvider.SetError((Control)sender, toolTip);
InBlock.gif    infoProvider.SetError((Control)sender, 
null);
InBlock.gif    e.Cancel 
= true;
ExpandedSubBlockEnd.gif  }

ExpandedSubBlockStart.gifContractedSubBlock.gif  
else dot.gif{
InBlock.gif    
// Show the info when there is text in the text box
InBlock.gif
    errorProvider.SetError((Control)sender, null);
InBlock.gif    infoProvider.SetError((Control)sender, toolTip);
ExpandedSubBlockEnd.gif  }

ExpandedBlockEnd.gif}

转载于:https://www.cnblogs.com/anf/archive/2005/02/28/110345.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值