/// <summary> /// Check mandatory field /// </summary> /// <param name="obj"></param> /// <param name="field"></param> /// <returns></returns> public static string[][] CheckMandatoryFiled(object obj, params string[] field) { ObjectMandatoryField omf = new ObjectMandatoryField(); if (field != null) { foreach (string f in field) { MandatoryField mc = new MandatoryField(); mc.FieldName = f; mc.ControlName = f; mc.MandatoryMessage = f; omf.AddProperty(mc); } } else if (obj != null && obj is object) { PropertyInfo[] props = obj.GetType().GetProperties(); if (props != null) { foreach (PropertyInfo prop in props) { MandatoryField mc = new MandatoryField(); mc.FieldName = prop.Name; mc.ControlName = prop.Name; mc.MandatoryMessage = prop.Name; omf.AddProperty(mc); } } } return(CheckMandatoryFiled(obj, omf)); }
public bool PrintBC(string templateFileName, PrintContent printContent, MandatoryField manF) { //未加載模板文件或者模板發生變更時,重新加載新的模板 if (lbl.Documents.Count == 0 || templateFileName.IndexOf(lbl.ActiveDocument.Name) == -1) { lbl.Documents.Open(templateFileName, false);// 调用设计好的label文件 } Document doc = lbl.ActiveDocument; try { if (manF.Ctcodem == "0") { doc.Variables.FormVariables.Item("ctcode").Value = printContent.CtCode; //给参数传值 可以不傳參數 } int Num = 1; //打印数量 doc.Printer.SwitchTo(DefaultPrinter()); doc.PrintLabel(1, 1, 1, Num, 1, ""); //doc.PrintDocument(Num); //打印 } catch (Exception ex) { return(false); //返回,後面代碼不執行 } finally { doc.FormFeed(); } return(true); }
/// <summary> /// Check mandatory field /// </summary> /// <param name="obj"></param> /// <param name="field"></param> /// <param name="return_ctrl"></param> /// <returns></returns> public static string[][] CheckMandatoryFiled(object obj, MandatoryField field, bool return_ctrl = false) { List <string> nullLst = new List <string>(); List <string> nullCtrlLst = new List <string>(); if (field is ListMandatoryField) { ListMandatoryField lfield = field as ListMandatoryField; if (IsNullOrEmpty(obj)) { nullLst.Add(field.MandatoryMessage); nullCtrlLst.Add(field.FieldName); } else { IList lst = null; if (obj is HttpRequestBase) { lst = new List <string>(); HttpRequestBase req = obj as HttpRequestBase; if (lfield.Rows != null) { foreach (MandatoryField mf in lfield.Rows) { lst.Add(req[mf.FieldName]); } } } else { lst = obj as IList; if (lst.Count <= 0) { nullLst.Add(field.MandatoryMessage); nullCtrlLst.Add(field.FieldName); } } if (lst.Count > 0) { for (int idx = 0; idx < lst.Count; idx++) { if (lfield.Index != null) { if (idx > lfield.Index) { break; } if (idx != lfield.Index) { continue; } } MandatoryField mf = lfield.Field; if (mf == null && lfield.Rows != null) { if (idx < lfield.Rows.Count) { mf = lfield.Rows[idx]; } } if (mf == null) { break; } string[][] sLst = CheckMandatoryFiled(lst[idx], mf, return_ctrl); if (sLst != null) { if (lfield.Rows != null || lfield.Index != null) { nullLst.AddRange(sLst[0]); if (sLst.Length > 1) { nullCtrlLst.AddRange(sLst[1]); } } else { string txt = string.Format("Row {0} [{1}]", (idx + 1), TextList(sLst[0])); if (field.MandatoryMessage != null) { txt = field.MandatoryMessage + " " + txt; } nullLst.Add(txt); if (sLst.Length > 1) { nullCtrlLst.Add(string.Format("{0}:{1}", (idx + 1), TextList(sLst[1]))); } if (lfield.IsBreakLoop) { break; } } } } } } } else if (field is ObjectMandatoryField) { ObjectMandatoryField ofield = field as ObjectMandatoryField; if (ofield.Properties != null) { if (obj == null) { nullLst.Add(ofield.MandatoryMessage); nullCtrlLst.Add(ofield.ControlName); } else { foreach (MandatoryField mf in ofield.Properties) { PropertyInfo prop = obj.GetType().GetProperty(mf.FieldName); if (prop != null) { object val = prop.GetValue(obj, null); string[][] sLst = CheckMandatoryFiled(val, mf, return_ctrl); if (sLst != null) { nullLst.AddRange(sLst[0]); if (sLst.Length > 1) { nullCtrlLst.AddRange(sLst[1]); } } } } } } } else { if (field.FieldName == null || obj == null) { if (IsNullOrEmpty(obj)) { nullLst.Add(field.MandatoryMessage != null ? field.MandatoryMessage : ""); nullCtrlLst.Add(field.ControlName != null ? field.ControlName : field.FieldName != null ? field.FieldName : ""); } } else { PropertyInfo prop = obj.GetType().GetProperty(field.FieldName); if (prop != null) { object val = prop.GetValue(obj, null); if (IsNullOrEmpty(val)) { nullLst.Add(field.MandatoryMessage); nullCtrlLst.Add(field.ControlName != null ? field.ControlName : field.FieldName); } } else { if (IsNullOrEmpty(obj)) { nullLst.Add(field.MandatoryMessage); nullCtrlLst.Add(field.ControlName != null ? field.ControlName : field.FieldName); } } } } if (nullLst.Count > 0) { int length = 1; if (return_ctrl) { length = 2; } string[][] res = new string[length][]; res[0] = nullLst.ToArray(); if (length == 2 && nullCtrlLst.Count > 0) { res[1] = nullCtrlLst.ToArray(); } return(res); } return(null); }