最近寫程序當中需要做一個表單提交(WinForm)
關於自驗證本文框
關於Observer模式
直接給代碼
第一步
IObserver接口
public interface IObserver
void Update();
void Attach(ISubject sub);
void Detach(ISubject obs);
IList MySubject
}
ISubject接口
public interface ISubject
IList MyObserver
void Attach(IObserver obs);
void Detach(IObserver obs);
void Notify();
bool IsValided();
}
第二步
TextBox控件
public class CNWTextbox : TextBoxRegex
private ArrayList obsList;
public CNWTextbox()
obsList = new ArrayList();
this
}
ISubject 成員#region ISubject 成員
public IList MyObserver
get
}
public void Attach(IObserver obs)
obsList
obs
}
public void Detach(IObserver obs)
obsList
obs
}
public void Notify()
foreach (IObserver obs in obsList)
obs
}
}
#endregion
protected override void OnTextChanged(EventArgs e)
base
//通知所有Observer更新自己
Notify();
}
public bool IsValided()
try
string temp = this
return true;
}
catch (Chopeen
Console
return false;
}
}
}
Button控件
public class CNWButton:Button
private ArrayList mySubjects;
public CNWButton()
mySubjects = new ArrayList();
}
IObserver 成員#region IObserver 成員
void IObserver
foreach (ISubject sub in mySubjects)
if (sub
this
}
else
this
break;
}
}
}
void IObserver
mySubjects
}
void IObserver
mySubjects
}
System
get
}
#endregion
}
第三步
cnwTextbox
private void Form
cnwTextbox
cnwTextbox
}
第四步
From:http://tw.wingwit.com/Article/program/net/201311/13047.html