用户操作
[留言]  [发消息]  [加为好友] 
订阅我的博客
XML聚合    FeedSky
订阅到鲜果
订阅到Google
订阅到抓虾
allenle的公告
<script src="http://www.google-analytics.com/urchin.js" type="text/javascript"> </script> <script type="text/javascript"> _uacct = "UA-2863969-2"; urchinTracker(); </script> <script type="text/javascript"><!-- google_ad_client = "pub-7753985038232859"; google_ad_width = 120; google_ad_height = 600; google_ad_format = "120x600_as"; google_ad_type = "text_image"; //2007-11-01: blog.csdn.net/allenle google_ad_channel = "6738416815"; google_color_border = "C3D9FF"; google_color_bg = "FFFFFF"; google_color_link = "0000FF"; google_color_text = "000000"; google_color_url = "008000"; google_ui_features = "rc:6"; //--> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
文章分类
工具
C#2VB
存档

原创  自验证文本框和Observer模式 收藏

最近写程序当中需要做一个表单提交(WinForm),当所有的表单项目符合要求时,提交按钮为可用,否则不可用,一般我们需要写一个验证的函数,然后触发TextBox的某个事件来调用函数,函数中记录下是否所有的表单项目都符合要求,是则提交按钮可用。


关于自验证本文框,在CodeProject上找到了TextBoxRegex,是一个不错的自验证TextBox。

关于Observer模式,按照四人团的说法,Observer 模式的意图是“定义对象间的一种一对多的依赖关系,当一个对象的状态发生改变时,所有依赖于它的对象都得到通知并自动更新”。


直接给代码:

第一步,定义两个接口

IObserver接口:

    public interface IObserver
    
{
        
void Update();
        
void Attach(ISubject sub);
        
void Detach(ISubject obs);
        IList MySubject 
get;  }
    }

ISubject接口:

public interface ISubject
    
{
        IList MyObserver 
get ;  }
        
void Attach(IObserver obs);
        
void Detach(IObserver obs);
        
void Notify();
        
bool IsValided();
    }

第二步,写TextBox、Button控件。这里用到了TextBoxRegex

TextBox控件:

    public class CNWTextbox : TextBoxRegex, ISubject
    
{
        
private ArrayList obsList;
        
public CNWTextbox()
        
{
            obsList 
= new ArrayList();
            
this.UseInvalidTextException = true;
        }

        
ISubject 成员
        
protected override void OnTextChanged(EventArgs e)
        
{
            
base.OnTextChanged(e);
            
//通知所有Observer更新自己。
            Notify();
        }

        
public bool IsValided()
        
{
            
try
            
{
                
string temp = this.TextValidated;
                
return true;
            }

            
catch (Chopeen.InvalidTextException ex)
            
{
                Console.WriteLine(ex.ToString());
                
return false;
            }

        }

    }

Button控件:

public class CNWButton:Button,IObserver
    
{
        
private ArrayList mySubjects;
        
public CNWButton()
        
{
            mySubjects 
= new ArrayList();
        }

        
IObserver 成员
    }

第三步,使用。

cnwTextbox4,cnwTextbox5,cnwButton1是上面制作的控件。
private void Form1_Load(object sender, EventArgs e)
        
{
            cnwTextbox4.Attach(cnwButton1);
            cnwTextbox5.Attach(cnwButton1);
        }

第四步,运行。

< type="text/javascript"> < type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js">

发表于 @ 2006年11月25日 12:32:00 | 评论( loading... ) | 编辑| 举报| 收藏

旧一篇:SQL SERVER 2000 课程设计 | 新一篇:.net2.0 中的持续集成

  • 发表评论
  • 评论内容:
  •  
Copyright © allenle
Powered by CSDN Blog