tangxu
2025-01-13 4f7cb65b079d88d5a829688b24d26d5145c5df47
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
 
using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Windows.Forms;
 
namespace DPumpHydr.WinFrmUI.WenSkin.Design.Editor
{
    public partial class StringCollectionEditor
    {
        /// <summary>
        ///  StringCollectionForm allows visible editing of a string array.
        ///  Each line in the edit box is an array entry.
        /// </summary>
        private class StringCollectionForm : CollectionForm
        {
            private Label _instruction;
            private TextBox _textEntry;
            private Button _okButton;
            private Button _cancelButton;
 
            private readonly StringCollectionEditor _editor;
 
            /// <summary>
            ///  Constructs a StringCollectionForm.
            /// </summary>
            public StringCollectionForm(CollectionEditor editor)
                : base(editor)
            {
                _editor = (StringCollectionEditor)editor;
                InitializeComponent();
                HookEvents();
            }
 
            private void Edit1_keyDown(object sender, KeyEventArgs e)
            {
                if (e.KeyCode != Keys.Escape)
                {
                    return;
                }
 
                _cancelButton.PerformClick();
                e.Handled = true;
            }
 
            private void StringCollectionEditor_HelpButtonClicked(object sender, CancelEventArgs e)
            {
                e.Cancel = true;
                _editor.ShowHelp();
            }
 
            private void Form_HelpRequested(object sender, HelpEventArgs e)
            {
                _editor.ShowHelp();
            }
 
            private void HookEvents()
            {
                _textEntry.KeyDown += new KeyEventHandler(Edit1_keyDown);
                _okButton.Click += new EventHandler(OKButton_click);
                HelpButtonClicked += new CancelEventHandler(StringCollectionEditor_HelpButtonClicked);
            }
 
            /// <summary>
            ///  NOTE: The following code is required by the form designer.
            ///  It can be modified using the form editor.  Do not modify it using the code editor.
            /// </summary>
            private void InitializeComponent()
            {
                _instruction = new Label();
                _textEntry = new TextBox();
                _okButton = new Button();
                _cancelButton = new Button();
                SuspendLayout();
                // instruction
                //
                this._instruction.AutoSize = true;
                this._instruction.Location = new System.Drawing.Point(12, 9);
                this._instruction.Name = "label1";
                this._instruction.Size = new System.Drawing.Size(185, 12);
                this._instruction.TabIndex = 1;
                this._instruction.Text = "在集合中输入字符串(每行一个)";
                _instruction.Name = "instruction";
                //
                // textEntry
                //
                this._textEntry.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;
                this._textEntry.Location = new System.Drawing.Point(14, 35);
                this._textEntry.Multiline = true;
                this._textEntry.Name = "textBox1";
                this._textEntry.ScrollBars = System.Windows.Forms.ScrollBars.Both;
                this._textEntry.Size = new System.Drawing.Size(560, 278);
                this._textEntry.TabIndex = 0;
                _textEntry.AcceptsTab = true;
                _textEntry.AcceptsReturn = true;
                _textEntry.Name = "textEntry";
                //
                // okButton
                //
                this._okButton.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
                this._okButton.Location = new System.Drawing.Point(416, 319);
                this._okButton.Name = "button1";
                this._okButton.Size = new System.Drawing.Size(75, 30);
                this._okButton.TabIndex = 2;
                this._okButton.Text = "确定";
                this._okButton.UseVisualStyleBackColor = true;
                _okButton.DialogResult = DialogResult.OK;
                _okButton.Name = "okButton";
                //
                // cancelButton
                //
                this._cancelButton.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
                this._cancelButton.Location = new System.Drawing.Point(497, 319);
                this._cancelButton.Name = "button2";
                this._cancelButton.Size = new System.Drawing.Size(75, 30);
                this._cancelButton.TabIndex = 3;
                this._cancelButton.Text = "取消";
                this._cancelButton.UseVisualStyleBackColor = true;
                _cancelButton.DialogResult = DialogResult.Cancel;
                _cancelButton.Name = "cancelButton";
                //
                // StringCollectionEditor
                //
                this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.ClientSize = new System.Drawing.Size(584, 361);
                this.Text = "字符串集合编辑器";
                AutoScaleMode = AutoScaleMode.Font;
                Controls.Add(_instruction);
                Controls.Add(_textEntry);
                Controls.Add(_okButton);
                Controls.Add(_cancelButton);
 
                MaximizeBox = false;
                MinimizeBox = false;
                Name = "StringCollectionEditor";
                ShowIcon = false;
                ShowInTaskbar = false;
                HelpRequested += new HelpEventHandler(Form_HelpRequested);
                ResumeLayout(false);
                PerformLayout();
            }
 
            /// <summary>
            ///  Commits the changes to the editor.
            /// </summary>
            private void OKButton_click(object sender, EventArgs e)
            {
                char[] delims = new char[] { '\n' };
                char[] trims = new char[] { '\r' };
 
                string[] strings = _textEntry.Text.Split(delims);
                object[] curItems = Items;
 
                int nItems = strings.Length;
                for (int i = 0; i < nItems; i++)
                {
                    strings[i] = strings[i].Trim(trims);
                }
 
                bool dirty = true;
                if (nItems == curItems.Length)
                {
                    int i;
                    for (i = 0; i < nItems; ++i)
                    {
                        if (!strings[i].Equals((string)curItems[i]))
                        {
                            break;
                        }
                    }
 
                    if (i == nItems)
                        dirty = false;
                }
 
                if (!dirty)
                {
                    DialogResult = DialogResult.Cancel;
                    return;
                }
 
                // If the final line is blank, we don't want to create an item from it
                if (strings.Length > 0 && strings[strings.Length - 1].Length == 0)
                {
                    nItems--;
                }
 
                object[] values = new object[nItems];
                for (int i = 0; i < nItems; i++)
                {
                    values[i] = strings[i];
                }
 
                Items = values;
            }
 
            /// <summary>
            ///  This is called when the value property in the CollectionForm has changed.
            ///  In it you should update your user interface to reflect the current value.
            /// </summary>
            protected override void OnEditValueChanged()
            {
                object[] items = Items;
                string text = string.Empty;
 
                for (int i = 0; i < items.Length; i++)
                {
                    if (items[i] is string)
                    {
                        text += (string)items[i];
                        if (i != items.Length - 1)
                        {
                            text += "\r\n";
                        }
                    }
                }
 
                _textEntry.Text = text;
            }
        }
    }
}