#region
using System;
using System.Data;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using CSFrameworkV5.CodeGeneratorCore;
using CSFrameworkV5.Core;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Grid;
#endregion
namespace CSFrameworkV5.Library.CommonClass
{
public class CommonTools
{
public enum IntRuler
{
不判断,
大于0,
小于0,
不能等于0
}
public static void ClearContainerEditorText(Control container)
{
for (var i = 0; i < container.Controls.Count; i++)
if (container.Controls[i] is BaseEdit)
((BaseEdit)container.Controls[i]).EditValue = null;
else if (container.Controls[i] is TextBoxBase)
((TextBoxBase)container.Controls[i]).Clear();
}
/// 自动绑定编辑区域的数据源。规则:txt+字段名,或者chk+字段名,其它类型可以扩展
///
/// Panel容器
/// 数据源
public static void DoBindingEditorPanel(Control editorPanel,
DataTable dataSource)
{
DoBindingEditorPanel(editorPanel, dataSource, "txt");
}
public static void DoBindingEditorPanel(Control editorPanel,
DataTable dataSource, string prefix)
{
var fieldName = "";
var length = prefix.Length;
try
{
for (var i = 0; i <= editorPanel.Controls.Count - 1; i++)
{
//TextEdit/CheckEdit
if (editorPanel.Controls[i] is BaseEdit)
{
var edit = editorPanel.Controls[i] as BaseEdit;
if (edit.Name.Substring(0, length) == prefix)
{
fieldName = edit.Name.Substring(length,
edit.Name.Length - length);
DataBinder.BindingTextEditBase(edit, dataSource,
fieldName);
continue;
}
}
//递归绑定数据源
if (editorPanel.Controls[i] is PanelControl ||
editorPanel.Controls[i] is GroupControl)
DoBindingEditorPanel(editorPanel.Controls[i],
dataSource, prefix);
}
}
catch (Exception ex)
{
Msg.Warning("字段:" + fieldName + "\r\n" + ex.Message);
}
}
private static void edit_EditValueChanged(object sender, EventArgs e)
{
(sender as BaseEdit).ErrorText = string.Empty;
}
///
/// 获取记录主键
///
///
///
///
///
public static string GetKeys(DataTable dt, string keyField,
char seprator)
{
var sb = new StringBuilder();
foreach (DataRow R in dt.Rows)
sb.Append((sb.Length == 0 ? "" : seprator.ToStringEx()) +
R[keyField].ToStringEx());
return sb.ToStringEx();
}
public static string InfoID_GetFullInfoID(string SimpleInfoID)
{
if (SimpleInfoID.Length > 6) return SimpleInfoID;
return 'A' + SimpleInfoID.PadLeft(6, '0');
}
public static string InfoID_GetSimpleInfoID(string InfoID)
{
var re = new Regex(@"[1-9]\d*(\.\d*)?");
var a = re.Matches(InfoID);
if (a.Count > 0) return a[0].Value;
return null;
}
public static bool IsNotEmpBaseEdit(BaseEdit edit,
string ErrorText = "", IntRuler Ruler = IntRuler.大于0)
{
if (edit.Visible == false) return true;
if ((edit.EditValue is int || edit.EditValue is decimal) &&
Ruler != IntRuler.不判断)
{
var tmp = false;
switch (Ruler)
{
case IntRuler.大于0:
tmp = ConvertEx.ToDecimal(edit.EditValue) > 0;
break;
case IntRuler.不能等于0:
tmp = ConvertEx.ToDecimal(edit.EditValue) != 0;
break;
case IntRuler.小于0:
tmp = ConvertEx.ToDecimal(edit.EditValue) < 0;
break;
}
if (tmp == false)
{
edit.ErrorIconAlignment = ErrorIconAlignment.MiddleRight;
edit.ErrorText = ErrorText;
edit.EditValueChanged -= edit_EditValueChanged;
edit.EditValueChanged += edit_EditValueChanged;
return tmp;
}
}
if (string.IsNullOrEmpty(ConvertEx.ToString(edit.EditValue)))
{
if (ErrorText != "")
{
edit.ErrorIconAlignment = ErrorIconAlignment.MiddleRight;
edit.ErrorText = ErrorText;
edit.EditValueChanged -= edit_EditValueChanged;
edit.EditValueChanged += edit_EditValueChanged;
}
return false;
}
return true;
}
///
/// 替换记录对应字段的数据。
///
/// 数据源
/// 需要替换的记录
public static void ReplaceDataRowChanges(DataRow sourceRow,
DataRow destRow)
{
string fieldName;
//循环处理当前记录的所有字段
for (var i = 0; i <= sourceRow.Table.Columns.Count - 1; i++)
{
fieldName = sourceRow.Table.Columns[i].ColumnName;
//如果字段名相同,替换对应字段的数据。
if (destRow.Table.Columns.IndexOf(fieldName) >= 0)
destRow[fieldName] = sourceRow[fieldName];
}
}
///
/// 设置容器内所有可输入控件的状态.ReadOnly or Enable = false/true
///
/// 容器
/// false/true
public static void SetControlAccessable(Control container, bool value)
{
if (container is Label) return;
if (container is LabelControl) return;
if (container is UserControl) return;
if (container.HasChildren == false) return; //没有子控件,不处理
foreach (Control c in container.Controls)
//最常用组件,首先处理
if (c is BaseEdit) //输入框
(c as BaseEdit).Properties.ReadOnly = !value;
else if (c is GridControl) //表格
((c as GridControl).Views[0] as GridView).OptionsBehavior
.Editable = value;
else
SetControlAccessableByProp(container,
value); //其它组件,反射属性设置状态
}
///
/// 设置一个控件的可用状态,通过反射ReadOnly,Properties属性
///
/// 控件
/// 值
public static void SetControlAccessableByProp(Control control,
bool value)
{
var type = control.GetType();
var infos = type.GetProperties();
foreach (var info in infos)
{
if (info.Name == "ReadOnly") //Properties.ReadOnly
{
info.SetValue(control, !value, null);
return;
}
if (info.Name == "Properties")
{
var o = info.GetValue(control, null);
//处理特殊组件
if (o is RepositoryItemButtonEdit &&
(o as RepositoryItemButtonEdit).Buttons.Count >
0) //ButtonEdit
(o as RepositoryItemButtonEdit).Buttons[0].Enabled =
value;
else if (o is RepositoryItemDateEdit &&
(o as RepositoryItemDateEdit).Buttons.Count >
0) //DateEdit
(o as RepositoryItemDateEdit).Buttons[0].Enabled =
value;
if (o is RepositoryItem)
(o as RepositoryItem).ReadOnly = !value;
return;
}
}
}
///
/// 给输入控件绑定的数据源赋值.
///
/// 输入控件
/// 值
/// 是否给控件的EditValue属性赋值
public static void SetEditorBindingValue(Control bindingControl,
object value, bool setEditorValue)
{
try
{
object temp = null; //空值为null
if (value != DBNull.Value) temp = value;
//有绑定数据源, 给数据源赋值
if (bindingControl.DataBindings.Count > 0)
{
var dataSource = bindingControl.DataBindings[0].DataSource;
var field = bindingControl.DataBindings[0].BindingMemberInfo
.BindingField;
if (dataSource is DataTable)
(dataSource as DataTable).Rows[0][field] = value;
else
DataConverter.SetValueOfObject(dataSource, field,
value);
}
//给输入组件的赋值
if (setEditorValue)
{
if (bindingControl is BaseEdit)
(bindingControl as BaseEdit).EditValue = value;
else
DataConverter.SetValueOfObject(bindingControl,
"EditValue", value);
}
}
catch
{
} //这里不用显示异常信息.
}
}
}