Spread Studio 表格控件提供 MaskCellType 來限制最終用戶輸入格式。論壇中也有一些用戶提交關於 MaskCellType 的問題。本篇文章中將闡述如何繼承 MaskCellType 來限制最終用戶只能以「XXX-XX-XXX」格式輸入「#」、「^」、「-」和字母,在用戶輸入結束時,將把字母所有轉化爲大寫。 c#
咱們經過如下步驟來實現上述功能: ide
1.繼承 MaskCellType 建立自定義單元格類型。 code
2.重載 OnCustomMaskCharacter 方法,在方法中校驗輸入字符。 component
3.重寫 GetEditorValue, Format 和 Parse 方法,格式化爲大寫字母。 orm
1: class CustomMaskCellType: MaskCellType 2: { 3: public CustomMaskCellType() 4: : base() 5: { 6: base.Mask = "000-00-000"; //Using custom format so it can be handle in CustomMaskCharacter event 7: } 8: protected override void OnCustomMaskCharacter(object sender, FarPoint.Win.SuperEdit.CustomMaskEventArgs e) 9: { 10: int charval = Convert.ToInt32(e.InputCharacter); 11: e.Valid = (Char.IsLetter(e.InputCharacter) && ((0x0041 <= charval && charval <= 0x005A) || (0x0061 <= charval && charval <= 0x007A))) 12: || e.InputCharacter == '#' || e.InputCharacter == '^' || e.InputCharacter == '-'; 13: if(e.Valid) 14: e.InputCharacter = Char.ToUpperInvariant(e.InputCharacter); 15: } 16: public override object GetEditorValue() 17: { 18: return base.GetEditorValue().ToString().ToUpperInvariant(); 19: } 20: public override string Format(object obj) 21: { 22: return base.Format(obj).ToUpperInvariant(); 23: } 24: 25: public override object Parse(string s) 26: { 27: return base.Parse(s).ToString().ToUpperInvariant(); 28: } 29: } 30: }
效果圖: htm
注意:本示例中,將在用戶完成單元格編輯後轉換小寫字母爲大寫. blog
下載示例:VS 2010 + Spread Studio Winform 7.0 + .NET 4.0 繼承
DownloadSample_CS
DownloadSample_VB ip