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
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Design;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
 
namespace DPumpHydr.WinFrmUI.WenSkin.Controls
{
    [Designer("WenSkin.Design.Designer.WenLableTextBoxDesigner")]
    public class WenLableTextBox : WenControl
    {
        public WenLableTextBox() : base()
        {
            InitializeComponent();
        }
        private void InitializeComponent()
        {
            textBox = new WenTextBox
            {
                Dock = DockStyle.Fill,
            };
            textBox.TextChanged += (s, e) =>
            {
                //值改变事件
                WenLableTextChangeEventArgs wenChoiceTextChangeEventArgs = new WenLableTextChangeEventArgs(beforeText, textBox.Text);
                this.TextBeforeChanged?.Invoke(s, wenChoiceTextChangeEventArgs);
                if (wenChoiceTextChangeEventArgs.State)
                {
                    beforeText = textBox.Text;
                    this.Text = textBox.Text;
                }
                else
                    textBox.Text = beforeText;
            };
 
            button = new WenButton()
            {
                Size = new Size(20, textBox.Height),
                Dock = DockStyle.Right,
                Image = Properties.Resources.edit,
                ImageSize = new Size(16, 16)
            };
            button.Click += Button_Click;
 
            label = new Label
            {
                TextAlign = System.Drawing.ContentAlignment.MiddleLeft,
                Dock = DockStyle.Left,
                Size = new Size(0, 0)
            };
 
 
            this.Controls.Add(textBox);
            this.Controls.Add(label);
            this.Controls.Add(button);
 
            this.Size = new Size(200, this.Height);
        }
 
        private void Button_Click(object sender, EventArgs e)
        {
            this.textBox.Focus();
            WenLableTextBoxButtonEventArgs args = new WenLableTextBoxButtonEventArgs() { Text = this.Text };
 
            ButtonClick?.Invoke(this, args);
 
            if (ShowTextBox && args.ShowTextBox)
            {
                WenSkin.Forms.WenForm form = new Forms.WenForm()
                {
                    StartPosition = FormStartPosition.CenterScreen,
                    Size = new Size(500, 350),
                    Text = "文本输入框"
                };
 
                WenTextBox textBox = new WenTextBox()
                {
                    Text = this.textBox.Text,
                    Dock = DockStyle.Fill,
                    Multiline = true,
                    PasswordChar = this.textBox.PasswordChar,
                    ReadOnly = this.textBox.ReadOnly,
                    ScrollBars = ScrollBars.Both,
                    WordWrap = false
                };
 
                WenButton button = new WenButton()
                {
                    Text = "确定",
                    Dock = DockStyle.Bottom,
                };
                button.Click += (s, ex) =>
                {
                    this.textBox.Text = textBox.Text;
                    form.Dispose();
                };
 
                form.Controls.Add(textBox);
                form.Controls.Add(button);
                form.ShowDialog();
            }
        }
 
        #region 私有属性
 
        private WenTextBox textBox;
        private WenButton button;
        private Label label;
 
        private string beforeText;
 
        private bool password = false;
        #endregion
 
        #region 委托事件
 
        [Category("Wen"), Description("按钮点击事件")]
        public event WenChoiceTextBoxButtonEventHandler ButtonClick;
 
        [Category("Wen"), Description("值改变前事件")]
        public event WenChoiceTextChangeEventHandler TextBeforeChanged;
        #endregion
 
        #region 公有属性
 
        public override Font Font { get => base.Font; set { textBox.Font = value; base.Font = value; } }
 
        [AmbientValue(""), Category("Wen"), Description("标签显示文本内容")]
        public string TextLable
        {
            get => label.Text;
            set
            {
                label.Text = value;
                label.Size = new Size(TextRenderer.MeasureText(TextLable, this.Font).Width, this.Height);
            }
        }
 
        [DefaultValue(true), AmbientValue(true), Category("Wen"), Description("点击按钮是否弹出输入框")]
        [RefreshProperties(RefreshProperties.Repaint)]
        public bool ShowTextBox { get; set; } = true;
 
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        [Category("Wen")]
        [Description("标签文本显示")]
        [Browsable(true)]
        [Bindable(true)]
        [EditorBrowsable(EditorBrowsableState.Always)]
        [RefreshProperties(RefreshProperties.Repaint)]
        public new string Text
        {
            get => textBox.Text;
            set
            {
                textBox.Text = value;
                base.Text = value;
                if (beforeText == null)
                    beforeText = value;
            }
        }
 
        [DefaultValue(false)]
        [RefreshProperties(RefreshProperties.Repaint)]
        [Category("Wen")]
        [Description("是否可输入状态")]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public bool ReadOnly { get => textBox.ReadOnly; set => textBox.ReadOnly = value; }
 
        [DefaultValue(false), AmbientValue(false), Category("Wen"), Description("是否用●掩盖字符")]
        [RefreshProperties(RefreshProperties.Repaint)]
        public bool Password
        {
            get => this.password;
            set
            {
                password = value;
                if (value)
                {
                    textBox.PasswordChar = '●';
                }
                else
                {
                    textBox.PasswordChar = '\0';
                }
            }
        }
 
        [DefaultValue(true), AmbientValue(true), Category("Wen"), Description("是否显示按钮")]
        public bool ButtonVisible { get => button.Visible; set => button.Visible = value; }
 
        [Category("Wen"), Description("是否开启下拉搜索框"), DefaultValue(false)]
        public bool StateComDown { get => textBox.StateComDown; set => textBox.StateComDown = value; }
 
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        [Editor("System.Windows.Forms.Design.ListControlStringCollectionEditor", typeof(UITypeEditor))]
        public AutoCompleteStringCollection AutoCompleteCustomSource { get => textBox.AutoCompleteCustomSource; set => textBox.AutoCompleteCustomSource = value; }
 
        [Category("Wen"), Description("按钮图标"), DefaultValue(false)]
        public Image Image { get => button.Image; set => button.Image = value; }
        #endregion
 
        #region 重写事件
 
        //保持控件宽度
        protected override void SetBoundsCore(int x, int y, int width, int height, BoundsSpecified specified)
        {
            base.SetBoundsCore(x, y, width, textBox.Height, specified);
        }
 
        #endregion
 
        public new void Focus()
        {
            textBox.Focus();
        }
    }
 
}