啊鑫
2024-07-09 0552fcc8cb73fc3021e2915129f55a42ed3f20e5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#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+字段名,其它类型可以扩展
        /// </summary>
        /// <param name="editorPanel">Panel容器</param>
        /// <param name="dataSource">数据源</param>
        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;
        }
 
        /// <summary>
        ///     获取记录主键
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="keyField"></param>
        /// <param name="seprator"></param>
        /// <returns></returns>
        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;
        }
 
        /// <summary>
        ///     替换记录对应字段的数据。
        /// </summary>
        /// <param name="sourceRow">数据源</param>
        /// <param name="destRow">需要替换的记录</param>
        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];
            }
        }
 
        /// <summary>
        ///     设置容器内所有可输入控件的状态.ReadOnly or Enable = false/true
        /// </summary>
        /// <param name="container">容器</param>
        /// <param name="value">false/true</param>
        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); //其它组件,反射属性设置状态
        }
 
        /// <summary>
        ///     设置一个控件的可用状态,通过反射ReadOnly,Properties属性
        /// </summary>
        /// <param name="control">控件</param>
        /// <param name="value">值</param>
        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;
                }
            }
        }
 
        /// <summary>
        ///     给输入控件绑定的数据源赋值.
        /// </summary>
        /// <param name="bindingControl">输入控件</param>
        /// <param name="value">值</param>
        /// <param name="setEditorValue">是否给控件的EditValue属性赋值</param>
        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
            {
            } //这里不用显示异常信息. 
        }
    }
}