/// <summary> /// Điền giá trị lên Form từ thông tin một Object /// </summary> public void Fill(object obj) { // Nếu obj null thì return không thực hiện gì if (obj == null) { return; } // Type của obj Type type = obj.GetType(); ListInputs.ForEach(c => { // Name var pname = (c as Control).Tag.ToString(); // Lấy ra Property var pi = type.GetProperty(pname); // Nếu có Property mới Fill if (pi != null) { c.SetValue(pi.GetValue(obj, null)); } }); }
/// <summary> /// Điền dữ liệu trong Panel vào đối tượng /// </summary> /// <typeparam name="T"></typeparam> /// <param name="t"></param> public void ParseTo <T>(T t, bool hasValidate = true, Action <T> afterThrowMessageInValid = null) { if (t == null) { return; } // Type của obj Type type = t.GetType(); ListInputs.ForEach(c => { // Name var pname = (c as Control).Tag.ToString(); // Lấy ra Property var pi = type.GetProperty(pname); // Nếu có Property mới Fill if (pi != null) { pi.SetValue(t, c.GetValue()); } }); //// Lấy ra các giá trị ở trong Panel rồi điền vào đối tượng //t.Parse(GetValuesInput(),false); //// Nếu không thực hiện validate thì return //if (!hasValidate) return; // Thực hiện validate //var validator = t.Validate(ListInputs.Cast<Control>().OrderBy(c => c.TabIndex).Select(c => c.Tag.ToString()).ToArray()); //// Nếu dữ liệu được valid nhưng t lại có mở rộng validate riêng thì thực hiện validate tiếp //// if (validator.IsValid && (t is IValidator)) validator = (t as IValidator).Validate(); //// nếu validator báo có dữ liệu không hợp lệ //if (!validator.IsValid) //{ // // Focus Control // var inputFocus = this.ListInputs.Cast<Control>().FirstOrDefault(i => i.Tag.ToString() == validator.FieldName); // // Nếu tồn tại control thì focus // if (inputFocus.IsNotNull()) inputFocus.Focus(); // // Thực hiện trước khi đưa ra thông báo lỗi // if (afterThrowMessageInValid != null) afterThrowMessageInValid(t); // // nếu như không có label hiển thị message thì bắn ra Exception // if (this.labelMessage.IsNull()) throw new ShMessageException(validator.Message); // // Hiển thị Message thông báo validate ko thành công // this.ShowMessage(validator.Message, Color.Black); // // Bắn ra Exception không sử lý // throw new ShNoProcessException(); //} }
/// <summary> /// Lấy các giá trị được nhập ở trong Panel /// </summary> /// <returns></returns> public Dictionary <string, object> GetValuesInput() { // Khai báo một Dictionary để lưu giá trị trả về Dictionary <string, object> dic = new Dictionary <string, object>(); // Lấy những Control mà là IInput, Tag khác rỗng ListInputs.ForEach(c => dic[(c as Control).Tag.ToString()] = c.GetValue()); // Trả về kết quả return(dic); }
/// <summary> /// Điền dữ liệu trong Panel vào đối tượng /// </summary> /// <typeparam name="T"></typeparam> /// <param name="t"></param> public bool ParseToEx <T>(T t, bool hasValidate = true, Action <T> afterThrowMessageInValid = null) { // Lấy ra các giá trị ở trong Panel rồi điền vào đối tượng t.Parse(GetValuesInput()); // Nếu không thực hiện validate thì return if (!hasValidate) { return(true); } // Thực hiện validate var validator = t.Validate(ListInputs.Cast <Control>().OrderBy(c => c.TabIndex).Select(c => c.Tag.ToString()).ToArray()); // Nếu dữ liệu được valid nhưng t lại có mở rộng validate riêng thì thực hiện validate tiếp // if (validator.IsValid && (t is IValidator)) validator = (t as IValidator).Validate(); // nếu validator báo có dữ liệu không hợp lệ if (!validator.IsValid) { // Focus Control var inputFocus = this.ListInputs.Cast <Control>().FirstOrDefault(i => i.Tag.ToString() == validator.FieldName); // var nameFx = string.Empty;//t.GetType().GetProperty(validator.FieldName).GetAttribute<FieldAttribute>().FieldName; // Nếu tồn tại control thì focus if (inputFocus.IsNotNull()) { inputFocus.Focus(); } // Thực hiện trước khi đưa ra thông báo lỗi if (afterThrowMessageInValid != null) { afterThrowMessageInValid(t); } // nếu như không có label hiển thị message thì bắn ra Exception if (this.labelMessage.IsNull()) { throw new ShMessageException(validator.Message); } // Hiển thị Message thông báo validate ko thành công this.ShowMessage(validator.Message, Color.Black); return(true); // Bắn ra Exception không sử lý // throw new ShNoProcessException(); } return(false); }
/// <summary> /// Thực hiện Clear Form /// </summary> public void ClearForm() { ListInputs.ForEach(c => c.Clear()); }