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

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

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

Windows Forms Programming In C# 读书笔记 - 第三章 Dialogs (2)// 遍历所有的control,包括子control(比如Group box中的)
Windows Forms Programming In C# 读书笔记 - 第三章 Dialogs (2)
// 浅层次的control会先返回,子control较后检查
{
Windows Forms Programming In C# 读书笔记 - 第三章 Dialogs (2)      ArrayList allControls 
= new ArrayList();
Windows Forms Programming In C# 读书笔记 - 第三章 Dialogs (2)      Queue queue 
= new
 Queue();
Windows Forms Programming In C# 读书笔记 - 第三章 Dialogs (2)      queue.Enqueue(
this
.Controls);
Windows Forms Programming In C# 读书笔记 - 第三章 Dialogs (2)
{
Windows Forms Programming In C# 读书笔记 - 第三章 Dialogs (2)           Control.ControlCollection controls 
=
 (Control.ControlCollection)queue.Dequeue();
Windows Forms Programming In C# 读书笔记 - 第三章 Dialogs (2)          
if( controls == null || controls.Count == 0 ) continue
;
Windows Forms Programming In C# 读书笔记 - 第三章 Dialogs (2)
{
Windows Forms Programming In C# 读书笔记 - 第三章 Dialogs (2)                allControls.Add(control);
Windows Forms Programming In C# 读书笔记 - 第三章 Dialogs (2)                queue.Enqueue(control.Controls);
Windows Forms Programming In C# 读书笔记 - 第三章 Dialogs (2)           }

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

Windows Forms Programming In C# 读书笔记 - 第三章 Dialogs (2)      
return (Control[])allControls.ToArray(typeof
(Control));
Windows Forms Programming In C# 读书笔记 - 第三章 Dialogs (2)}

Windows Forms Programming In C# 读书笔记 - 第三章 Dialogs (2)
{
Windows Forms Programming In C# 读书笔记 - 第三章 Dialogs (2)    
// Validate each control manually
{
Windows Forms Programming In C# 读书笔记 - 第三章 Dialogs (2)          
// Validate this control
Windows Forms Programming In C# 读书笔记 - 第三章 Dialogs (2)
          control.Focus();
{
Windows Forms Programming In C# 读书笔记 - 第三章 Dialogs (2)               
this.DialogResult = DialogResult.None;
Windows Forms Programming In C# 读书笔记 - 第三章 Dialogs (2)               
break;
Windows Forms Programming In C# 读书笔记 - 第三章 Dialogs (2)          }

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

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


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

{
Windows Forms Programming In C# 读书笔记 - 第三章 Dialogs (2)  // Use tooltips to populate the "information provider"
{
Windows Forms Programming In C# 读书笔记 - 第三章 Dialogs (2)    
string toolTip = toolTip1.GetToolTip(control);
Windows Forms Programming In C# 读书笔记 - 第三章 Dialogs (2)    
if( toolTip.Length == 0 ) continue;
Windows Forms Programming In C# 读书笔记 - 第三章 Dialogs (2)    infoProvider.SetError(control, toolTip);
Windows Forms Programming In C# 读书笔记 - 第三章 Dialogs (2)  }

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

Windows Forms Programming In C# 读书笔记 - 第三章 Dialogs (2)
{
Windows Forms Programming In C# 读书笔记 - 第三章 Dialogs (2)  
string toolTip = toolTip1.GetToolTip((Control)sender);
{
Windows Forms Programming In C# 读书笔记 - 第三章 Dialogs (2)    
// 当text box未空时,提示错误
Windows Forms Programming In C# 读书笔记 - 第三章 Dialogs (2)
    errorProvider.SetError((Control)sender, toolTip);
Windows Forms Programming In C# 读书笔记 - 第三章 Dialogs (2)    infoProvider.SetError((Control)sender, 
null);
Windows Forms Programming In C# 读书笔记 - 第三章 Dialogs (2)    e.Cancel 
= true;
Windows Forms Programming In C# 读书笔记 - 第三章 Dialogs (2)  }

{
Windows Forms Programming In C# 读书笔记 - 第三章 Dialogs (2)    
// Show the info when there is text in the text box
Windows Forms Programming In C# 读书笔记 - 第三章 Dialogs (2)
    errorProvider.SetError((Control)sender, null);
Windows Forms Programming In C# 读书笔记 - 第三章 Dialogs (2)    infoProvider.SetError((Control)sender, toolTip);
Windows Forms Programming In C# 读书笔记 - 第三章 Dialogs (2)  }

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

相关文章: