检查窗体中数据填写正确性的方法

类别:.NET开发 点击:0 评论:0 推荐:

//检查窗体中数据填写正确性的方法(类)
利用每一个控件的Tag属性,确定这个控件的数据类型,是否允许空 (个位为1表示不能为空,个位为0表示可以为空,十位数表示数据类型;参看源码)

如果改为使用正则表达式,可能更好。
//使用:
try
{
CheckFormControls.TCheckFormControls.CheckForm_Data(Object parentControl);
}
catch(Exception err)
   {
    MessageBox.Show(err.Message);
   }

//源码
namespace CheckFormControls
{
 public enum TKhyhDataType: int
 {
  Unknow=0,
  Integer=1,
  Float=2,
  String=3,
  Date=4,
  DateTime=5,
  Time=6,
  Number=7 //it only use 0-9 numbers, for example: 08688122784
 }
 /// <summary>
 /// CheckFormControls
 /// </summary>
 public class TCheckFormControls
 {
  public const string SELECT_ITEM="Please select a item in \"{0}\".";
  public const string COULD_NOT_EMPTY="\"{0}\" could not be empty, please check it.";
  public const string ERROR_IN_CONVERT="'{0}' couldn't be convert to {1}, please check your input in \"{2}\".";
  public const string ERROR_INPUT="It only can be input {0}, please check your input in \"{1}\"";

  private static bool ParseTypeValue(object tag, out TKhyhDataType  ofDataType, out string sDataType, out bool bNeedData)
  {
   if(tag==null)
   {
    ofDataType=TKhyhDataType.Unknow;
    sDataType=ofDataType.ToString();
    bNeedData=false;
    return false;
   }
  
   int ofTypeValue;
   try
   {
    ofTypeValue=Convert.ToInt32(tag.ToString());
    bNeedData=((ofTypeValue%10)==1);
    ofTypeValue=(ofTypeValue-(ofTypeValue%10))/10;

    ofDataType=(TKhyhDataType)System.Enum.Parse(typeof(TKhyhDataType),ofTypeValue.ToString());
    sDataType=ofDataType.ToString();
   }
   catch
   {
    ofDataType=TKhyhDataType.Unknow;
    sDataType=ofDataType.ToString();
    bNeedData=false;
    return false;
   }
   return true;
  }
  /// <summary>
  /// CheckForm_Data
  /// </summary>
  /// <param name="parentSender"></param>
  /// It check control sort by control's TabIndex
  /// Data Type is by control's Tag
  /// Control's name is read from controls' AccessibleDescription
  public static void CheckForm_Data(Control parentSender)
  {
   Control obj, obj2;
   TKhyhDataType ofDataType;
   bool bNeedData;
   string objName;
   string sDataType;

   ArrayList arrList=new ArrayList();
   int idx;
   for(int i=0; i<parentSender.Controls.Count; i++)
   {
    obj=parentSender.Controls[i];
    idx=arrList.Count;
    for(int j=0; j<arrList.Count; j++)
    {
     obj2=(Control)arrList[j];
     if(obj.TabIndex<obj2.TabIndex)
     {
      idx=j;
      break;
     }
    }
    arrList.Insert(idx,obj);
   }

   for(int i=0; i<arrList.Count; i++)
   {
    obj=(Control)arrList[i];
    if(obj is Panel)
    {
     CheckForm_Data(obj);
     continue;
    }

    if(!ParseTypeValue(obj.Tag,out ofDataType, out sDataType, out bNeedData))
     continue;

    if(obj.Tag==null)
     continue;

    //MessageBox.Show(obj.Name.ToString());
    //continue;

    if(obj.AccessibleDescription==null)
     objName="";
    else
     objName=obj.AccessibleDescription.ToString();
    if(objName.Length==0)
     objName=obj.Name;

    if (obj is ListBox)
    {
     ListBox lstObj=obj as ListBox;
     if(bNeedData && lstObj.SelectedItems.Count==0)
     {
      //lstObj.BackColor=Color.Red;
      throw new Exception(string.Format(SELECT_ITEM,objName));
     }
    }
    else if(obj is ComboBox)
    {
     ComboBox lstObj=obj as ComboBox;
     if(bNeedData && lstObj.SelectedItem==null)
     {
      //lstObj.BackColor=Color.Red;
      throw new Exception(string.Format(SELECT_ITEM,objName));
     }
    }
    else if(obj is TextBox)
    {
     TextBox textObj=obj as TextBox;
     string v=textObj.Text;

     if(v.Length==0)
     {
      if(bNeedData)
       throw new Exception(string.Format(COULD_NOT_EMPTY,objName));
      else
       continue;
     }

     switch(ofDataType)
     {
      case TKhyhDataType.Integer:
       try
       {
        Convert.ToInt32(v);
       }
       catch
       {
        throw new Exception(string.Format(ERROR_IN_CONVERT,v,sDataType,objName));
       }
       break;

      case TKhyhDataType.Float:
       try
       {
        Convert.ToDouble(v);
       }
       catch
       {
        throw new Exception(string.Format(ERROR_IN_CONVERT,v,sDataType,objName));
       }
       break;
      case TKhyhDataType.String:
       break;
      case TKhyhDataType.Date:
      case TKhyhDataType.DateTime:
      case TKhyhDataType.Time:
       try
       {
        Convert.ToDateTime(v);
       }
       catch
       {
        throw new Exception(string.Format(ERROR_IN_CONVERT,v,sDataType,objName));
       }
       break;
      case TKhyhDataType.Number:
       try
       {
        Convert.ToInt32(v);
       }
       catch
       {
        throw new Exception(string.Format(ERROR_INPUT,sDataType,objName));
       }
       break;
      default:
       break;
     }
    }
   }
  }
 }
}

本文地址:http://com.8s8s.com/it/it42423.htm