-
Notifications
You must be signed in to change notification settings - Fork 0
/
DynamicInputParamBox.cs
137 lines (129 loc) · 5.32 KB
/
DynamicInputParamBox.cs
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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Lab7
{
public partial class DynamicInputParamBox : Form
{
private DataTable dt;
private List<InputField> fields;
public DynamicInputParamBox(String operType, String tableName, DataTable dataSource)
{
InitializeComponent();
fields = new List<InputField>();
this.Text = "Operation: " + operType;
this.actionButton.Text = operType;
this.dt = dataSource;
this.columnNameLabel.Text = tableName;
foreach (DataColumn column in dt.Columns) {
InputField field = new InputField(column.ColumnName, "", true);
this.flowLayoutPanel1.Controls.Add(field);
this.fields.Add(field);
}
this.flowLayoutPanel1.Controls.Add(this.actionButton);
}
public DynamicInputParamBox(String operType, String tableName, DataTable dataSource, List<String> primaryKeys, DataRow defaultValue) {
InitializeComponent();
fields = new List<InputField>();
this.Text = "Operation: " + operType;
this.actionButton.Text = operType;
this.dt = dataSource;
this.columnNameLabel.Text = tableName;
foreach (DataColumn column in dt.Columns)
{
String value = defaultValue[column].ToString();
bool isActiveField = primaryKeys.Contains(column.ColumnName) ? false : true;
InputField field = new InputField(column.ColumnName, value, isActiveField);
this.flowLayoutPanel1.Controls.Add(field);
this.fields.Add(field);
}
this.flowLayoutPanel1.Controls.Add(this.actionButton);
}
public List<String> getFieldsData() {
var data = new List<String>();
foreach (InputField field in fields)
data.Add(field.getTextBoxData());
return data;
}
private bool ParseArgument(String value, Type type) {
if (type == typeof(int))
{
int result;
return int.TryParse(value, out result);
}
else if (type == typeof(double))
{
double result;
return double.TryParse(value, out result);
}
else if (type == typeof(DateTime))
{
DateTime result;
return DateTime.TryParse(value, out result);
}
else if (type == typeof(short)) {
short result;
return short.TryParse(value, out result);
}
return true;
}
private void actionButton_Click(object sender, EventArgs e)
{
int i = 0;
foreach (DataColumn column in dt.Columns) {
if (fields[i].getTextBoxData() == "") {
MessageBox.Show(column.ColumnName + "'s value field can't be empty.",
"Warning",
MessageBoxButtons.OK,
MessageBoxIcon.Warning,
MessageBoxDefaultButton.Button1,
MessageBoxOptions.DefaultDesktopOnly);
return;
}
bool parseResult = ParseArgument(fields[i].getTextBoxData(), column.DataType);
if (!parseResult) {
MessageBox.Show(column.ColumnName + "'s value field doesn't match with " + column.ColumnName + " type.",
"Warning",
MessageBoxButtons.OK,
MessageBoxIcon.Warning,
MessageBoxDefaultButton.Button1,
MessageBoxOptions.DefaultDesktopOnly);
return;
}
i++;
}
this.DialogResult = DialogResult.OK;
this.Close();
}
}
class InputField : GroupBox
{
private TextBox textBox;
public InputField(String label, String value, bool isActive) {
textBox = new TextBox();
textBox.Font = new System.Drawing.Font("Tahoma", 11.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point);
textBox.Location = new System.Drawing.Point(5, 29);
textBox.Size = new System.Drawing.Size(310, 49);
textBox.TabIndex = 1;
textBox.Text = value;
textBox.Enabled = isActive;
this.Anchor = System.Windows.Forms.AnchorStyles.Top;
this.Controls.Add(textBox);
this.Font = new System.Drawing.Font("Bahnschrift SemiBold SemiConden", 14F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point);
this.Location = new System.Drawing.Point(3, 32);
this.Size = new System.Drawing.Size(320, 67);
this.TabIndex = 1;
this.TabStop = false;
this.Text = label;
}
public String getTextBoxData() {
return textBox.Text;
}
}
}