啊鑫
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
#region
 
using System;
using System.Data;
using System.Windows.Forms;
using CSFrameworkV5.Common;
using CSFrameworkV5.Core;
using DevExpress.XtraEditors;
 
#endregion
 
namespace CSFrameworkV5.Library.PermissionForms
{
    /// <summary>
    ///     点击确定应用数据事件
    /// </summary>
    /// <param name="detail">数据表</param>
    /// <param name="listBox">复选列表框</param>
    /// <returns></returns>
    public delegate bool OnUserRoleEditorApply(DataTable detail,
        CheckedListBoxControl listBox, DateTime expireDate);
 
    public partial class frmUserRolePicker : frmBase
    {
        private string _codeField;
        private DataTable _detail;
        private string _displayField;
        private bool _isOK;
        private DataTable _list;
        private OnUserRoleEditorApply _myMethod;
        private string _nameField;
        private bool _supportRemoveItem;
 
        private frmUserRolePicker()
        {
            InitializeComponent();
        }
 
        private void btnApply_Click(object sender, EventArgs e)
        {
            try
            {
                frmWaitingEx.ShowMe(this);
 
                if (_myMethod != null)
                {
                    _isOK = _myMethod(_detail, chkList, txtExpireDate.DateTime);
                    if (_isOK)
                    {
                        Msg.ShowInformation("操作成功!");
                        btnApply.Tag = "OK";
                        Close();
                    }
                    else
                    {
                        Msg.Warning("处理数据发生错误!");
                    }
                }
                else
                {
                    Msg.Warning("没有提供处理事件:OnDetailItemEditorApply!");
                }
            }
            finally
            {
                frmWaitingEx.HideMe(this);
            }
        }
 
        private void btnCancel_Click(object sender, EventArgs e)
        {
            btnApply.Tag = null;
            Close();
        }
 
        private void btnSearch_Click(object sender, EventArgs e)
        {
            if (txtCodeName.Text != "")
            {
                var filter = _codeField + " LIKE '%" + txtCodeName.Text +
                             "%' OR " + _nameField + " LIKE '%" +
                             txtCodeName.Text + "%'";
                var dt = _list.Copy();
                dt.DefaultView.RowFilter = filter;
                dt = dt.DefaultView.ToTable();
                InitEditor(dt);
            }
            else
            {
                InitEditor(_list);
            }
        }
 
        /// <summary>
        ///     通用接口
        /// </summary>
        /// <param name="callBack">点确定按钮回调函数</param>
        /// <param name="detail">本次处理的数据</param>
        /// <param name="list">参考数据,用于填充ListBox列表组件数据源</param>
        /// <param name="codeField">取值字段名</param>
        /// <param name="nameField">标题字段名</param>
        /// <param name="displayField">显示字段名</param>
        /// <param name="supportRemoveItem">是否支持删除项目</param>
        /// <returns></returns>
        public static bool Execute(OnUserRoleEditorApply callBack,
            DataTable detail,
            DataTable list,
            string codeField, string nameField, string displayField,
            bool supportRemoveItem)
        {
            var form = new frmUserRolePicker();
            form._myMethod = callBack;
            form._codeField = codeField;
            form._detail = detail;
            form._displayField = displayField;
            form._list = list;
            form._nameField = nameField;
            form._supportRemoveItem = supportRemoveItem;
            form.InitEditor(list);
            form.ShowDialog();
            return form.btnApply.Tag != null;
        }
 
        private void frmDetailItemEditor_Load(object sender, EventArgs e)
        {
            txtExpireDate.DateTime = DateTime.Today.AddYears(10);
        }
 
        private void InitEditor(DataTable listData)
        {
            if (_displayField == "") _displayField = _nameField;
 
            CheckState chk;
            string code;
            chkList.Items.Clear();
            foreach (DataRow R in listData.Rows)
            {
                code = ConvertEx.ToString(R[_codeField]);
                var rs = _detail.Select(_codeField + "='" + code + "'");
                if (rs.Length > 0)
                    chk = CheckState.Checked;
                else
                    chk = CheckState.Unchecked;
 
                if (_supportRemoveItem) //支持删除功能
                    chkList.Items.Add(code, R[_displayField].ToStringEx(), chk,
                        true);
                else //编辑器不支持删除(去掉勾),要禁止已勾选的项目
                    chkList.Items.Add(code, R[_displayField].ToStringEx(), chk,
                        rs.Length == 0);
            }
        }
    }
}