Skip to main content Skip to footer

Custom MaskCellType to Input Letters or Special Characters

Spread's MaskCellType is used for masking characters to limit user entry wherein we specify which subsets of characters are allowed for each item in the mask. One day, one of our customers came up with this requirement of MaskCellType wherein he wanted to input letter or special character like "#", "^" and "-" in format "XXX-XX-XXX" along with changing lower case input to upper case in MaskedCellType. Hence, in this blog we will explain an approach to achieve this using Custom MaskCellType :-

  1. Create a custom celltype inheriting from MaskCellType
  2. Override OnCustomMaskCharacter method and check for valid character i.e. letter or special character (pre-defined)
  3. Override GetEditorValue, Format and Parse methods and return them in upper case

class CustomMaskCellType: MaskCellType  
 {  
    public CustomMaskCellType()  
      : base()  
    {  
      base.Mask = "000-00-000"; //Using custom format so it can be handle in CustomMaskCharacter event  
    }  
protected override void OnCustomMaskCharacter(object sender, FarPoint.Win.SuperEdit.CustomMaskEventArgs e)  
    {  
      int charval = Convert.ToInt32(e.InputCharacter);  
      e.Valid = (Char.IsLetter(e.InputCharacter) && ((0x0041 <= charval && charval <= 0x005A) || (0x0061 <= charval && charval <= 0x007A)))  
      || e.InputCharacter == '#' || e.InputCharacter == '^' || e.InputCharacter == '-';  
      if(e.Valid)  
        e.InputCharacter = Char.ToUpperInvariant(e.InputCharacter);  
    }  
    public override object GetEditorValue()  
    {  
      return base.GetEditorValue().ToString().ToUpperInvariant();  
    }  
    public override string Format(object obj)  
    {  
      return base.Format(obj).ToUpperInvariant();  
    }  

    public override object Parse(string s)  
    {  
      return base.Parse(s).ToString().ToUpperInvariant();  
    }  
  }  
}  


Disclaimer: This approach has a limitation that the character does not change in upper case right after input from keyboard but it will change to upper case when we end editing. You can download the samples given below for detailed implementation. DownloadSample_CS DownloadSample_VB

MESCIUS inc.

comments powered by Disqus