tangxu
2025-02-26 2fc64c1af548596c4719d4a3abdc10de7a1d7e8f
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Data;
using System.Drawing;
using System.Reflection;
using System.Windows.Forms;
namespace DPumpHydr.WinFrmUI.WenSkin.Controls
{
    [Designer(typeof(Design.Designer.WenDataTextBoxDesigner))]
    public class WenDataTextBox : WenControl
    {
        public WenDataTextBox() : base()
        {
            Size = new Size(300, 300);
            button = new WenButton("确定")
            {
                Location = new Point(238, 270),
                Size = new Size(60, 28),
                Anchor = AnchorStyles.Bottom | AnchorStyles.Right,
                BorderStyle = WenButton.WenButtonBorderStyle.RoRectangle,
            };
            button.Click += (s, e) =>
            {
                OnButtonClick(e);
            };
 
            flow = new FlowLayoutPanel()
            {
                Location = new Point(1, 1),
                Size = new Size(298, 298),
                Anchor = AnchorStyles.Bottom | AnchorStyles.Right | AnchorStyles.Top | AnchorStyles.Left,
                AutoScroll = true,
            };
 
            this.Controls.Add(button);
            this.Controls.Add(flow);
        }
 
        #region 私有属性
 
        private FlowLayoutPanel flow;
        private WenButton button;
        private object dataSource;
        private WenDataTextBoxColumnTextCollection items;
 
        #endregion
 
        #region 公有属性
 
        //绑定数据
        [Browsable(false)]
        public object DataSource
        {
            get => dataSource;
            set
            {
                dataSource = value;
                switch (value)
                {
                    case DataRow dw:
                        DataBind(dw);
                        break;
                    case DataTable dt:
                        DataBind(dt.Rows[0]);
                        break;
                }
            }
        }
 
        [Category("Wen"), Description("获取或设置输入框列数据"), DefaultValue(null)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        [Editor(typeof(Design.Editor.WenCollectionEditor), typeof(System.Drawing.Design.UITypeEditor))]
        [EditorBrowsable(EditorBrowsableState.Always)]
        public WenDataTextBoxColumnTextCollection Items => items ?? (items = new WenDataTextBoxColumnTextCollection(this));
 
        #region WenSql
 
        [Category("WenSql"), Description("获取或设置数据库名称"), DefaultValue(null)]
        public string DataTabaleName { get; set; }
 
        [Category("WenSql"), Description("获取插入数据库数据语句"), DefaultValue(null)]
        [Browsable(false)]
        public string InsertSqlString
        {
            get
            {
                string insertName = "";
                string insertValue = "";
                foreach (WenDataTextBoxColumnText ct in Items)
                {
 
                    if (!ct.Updater)
                        continue;
                    insertName += ct.ColumnName + ",";
                    insertValue += $"'{ct.Text}',";
                }
                insertName = insertName.Remove(insertName.Length - 1);
                insertValue = insertValue.Remove(insertValue.Length - 1);
                string sql = $"insert into {DataTabaleName} ({insertName}) Values({insertValue})";
                return sql;
            }
        }
        [Category("WenSql"), Description("获取更新数据库数据语句"), DefaultValue(null)]
        [Browsable(false)]
        public string UpdateSqlString
        {
            get
            {
                string setValue = "";
                WenDataTextBoxColumnText identityCloumn = null;
                foreach (WenDataTextBoxColumnText ct in Items)
                {
                    if (ct.Identity)
                    {
                        identityCloumn = ct;
                        continue;
                    }
                    if (!ct.Updater)
                        continue;
 
                    setValue += $"{ct.ColumnName}=N'{ct.Text}',";
                }
                setValue = setValue.Remove(setValue.Length - 1);
 
 
                //获取主键列
 
                if (identityCloumn == null)
                    return null;
 
                string whereValue = identityCloumn.Text.ToString();
                string sql = $"update {DataTabaleName} set {setValue} where {identityCloumn.ColumnName}='{whereValue}'";
                return sql;
            }
        }
 
        #endregion
 
 
        #endregion
 
        #region 委托
        [Category("Wen"), Description("确定按钮点击事件")]
        public event EventHandler ButtonClick;
 
        protected virtual void OnButtonClick(EventArgs e)
        {
            foreach (WenDataTextBoxColumnText ct in Items)
            {
                if (ct.AutoUpdaterDateTime)
                {
                    ct.Text = DateTime.Now.ToString();
                }
            }
            ButtonClick?.Invoke(this, e);
        }
 
        #endregion
 
        private void DataBind(DataRow dw)
        {
            foreach (DataColumn column in dw.Table.Columns)
            {
                WenDataTextBoxColumnText cloumnText = Items[column.ColumnName];
                if (cloumnText != null)
                    cloumnText.WenChoiceTextBox.Text = dw[column]?.ToString();
            }
        }
 
        #region 重绘
 
        protected override void WenOnPaint(Graphics g, Rectangle rec, PaintEventArgs e)
        {
            base.WenOnPaint(g, rec, e);
            using Pen pen = new Pen(Color.White);
            g.DrawRectangle(pen, 0, 0, this.Width - 1, this.Height - 1);
        }
 
        #endregion
 
        #region 子类
        [ToolboxItem(false)]
        [DefaultProperty("TextLable")]
        [DesignTimeVisible(false)]
        public class WenDataTextBoxColumnText:Component
        {
            public WenDataTextBoxColumnText()
            {
                ReadOnly = false;
                Visible = true;
                Identity = false;
                Updater = true;
                AutoUpdaterDateTime = false;
            }
 
            public WenDataTextBoxColumnText(string text) : this()
            {
                TextLable = text;
            }
 
            #region 私有属性
 
            private bool identity;
            private string textLable;
            private string text;
            private bool readOnly;
 
            #endregion
 
 
            #region 公有属性   
 
 
            [Browsable(false)]
            public string Name { get; set; }
 
            [Category("WenInfo"), Description("显示文本"), DefaultValue(null)]
            public string Text
            {
                get => text;
                set { text = value; if (WenChoiceTextBox != null) WenChoiceTextBox.Text = value; }
            }
 
            [Category("WenInfo"), Description("数据列"), DefaultValue(null)]
            public string ColumnName { get; set; }
            [Category("WenInfo"), Description("数据列文本"), DefaultValue(null)]
            public string TextLable
            {
                get => textLable;
                set { textLable = value; if (WenChoiceTextBox != null) WenChoiceTextBox.TextLable = value; }
            }
 
            [Category("Wen设计"), Description("是否不允许编辑"), DefaultValue(false)]
            public bool ReadOnly
            {
                get => readOnly;
                set
                {
                    readOnly = value;
                    if (WenChoiceTextBox != null)
                        WenChoiceTextBox.ReadOnly = value;
                }
            }
            [Category("Wen设计"), Description("是否显示"), DefaultValue(true)]
            public bool Visible { get; set; }
 
            [Category("Wen设计"), Description("是否主键"), DefaultValue(false)]
            public bool Identity
            {
                get => identity; set
                {
                    identity = value;
                    if (value)
                    {
                        Updater = false;
                        ReadOnly = true;
                    }
                }
            }
 
            [Category("Wen设计"), Description("是否更新"), DefaultValue(true)]
            public bool Updater { get; set; }
 
            [Category("Wen设计"), Description("点击确定自动将内容更新为当前时间"), DefaultValue(false)]
            public bool AutoUpdaterDateTime { get; set; }
 
            [Browsable(false)]
            public WenLableTextBox WenChoiceTextBox { get; set; }
 
            #endregion
            public override string ToString()
            {
                return $"{ColumnName}[{TextLable}]";
            }
        }
 
        public class WenDataTextBoxColumnTextCollection : WenCollection
        {
            public WenDataTextBoxColumnTextCollection(WenDataTextBox owner)
            {
                this.owner = owner;
            }
            public WenDataTextBoxColumnText this[int index] { get => Items[index] as WenDataTextBoxColumnText; set => Items[index] = value; }
            public WenDataTextBoxColumnText this[string str]
            {
                get
                {
                    foreach (var item in Items)
                    {
                        if (item is WenDataTextBoxColumnText cloumnText)
                        {
                            if (cloumnText.ColumnName?.ToUpper() == str.ToUpper())
                                return cloumnText;
                        }
                    }
                    return null;
                }
            }
            #region 私有属性
 
            private WenDataTextBox owner { get; set; }
 
            #endregion
 
            public override int Add(object value)
            {
                if (value is WenDataTextBoxColumnText columnText)
                {
                    WenLableTextBox textBox = new WenLableTextBox()
                    {
                        TextLable = columnText.TextLable,
                        ReadOnly = columnText.ReadOnly,
                        Visible = columnText.Visible,
                        Width = 260,
                    };
                    textBox.TextChanged += (s, e) =>
                    {
                        columnText.Text = textBox.Text;
                        if (owner.dataSource is DataRow dw)
                        {
                            try
                            {
                                //Type type = dw.Table.Columns[columnText.ColumnName].DataType;
                                //dw[columnText.ColumnName] = new TypeConverter().ConvertTo(textBox.Text, type);
                                dw[columnText.ColumnName] = textBox.Text;
                            }
                            catch { }
                        }
                    };
                    columnText.WenChoiceTextBox = textBox;
                    owner.flow.Controls.Add(textBox);
                }
                return base.Add(value);
            }
 
            public override void Clear()
            {
                base.Clear();
                owner.flow.Controls.Clear();
            }
        }
        #endregion
    }
}