forked from 0000duck/VisionEdit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCTextBox.cs
More file actions
190 lines (181 loc) · 5.41 KB
/
CTextBox.cs
File metadata and controls
190 lines (181 loc) · 5.41 KB
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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Controls.Properties;
namespace Controls
{
public delegate void DTextStrChanged(string textStr);
public partial class CTextBox : UserControl
{
public CTextBox()
{
InitializeComponent();
tbx_text.Text = DefaultText;
//if (PasswordChar && tbx_text.Text != DefaultText)
//{
//}
//else
//{
//}
//if (PasswordChar)
//{
// tbx_text.PasswordChar = '*';
// btn_eye.Visible = true;
//}
//else
//{
//}
if (!PasswordChar)
{
tbx_text.PasswordChar = '\0';
btn_eye.Visible = false;
}
else
{
tbx_text.PasswordChar = '\0';
btn_eye.Visible = true ;
}
}
/// <summary>
/// 文本改变事件
/// </summary>
public event DTextStrChanged TextStrChanged;
/// <summary>
/// 控件文本
/// </summary>
private string _text = string.Empty;
public string TextStr
{
get
{
return _text;
}
set
{
if (value == string.Empty)
{
tbx_text.PasswordChar = '\0';
tbx_text.ForeColor = Color.DarkGray;
tbx_text.Text = DefaultText;
_text = value;
}
else
{
if (PasswordChar)
tbx_text.PasswordChar = '*';
else
tbx_text.PasswordChar = '\0';
tbx_text.ForeColor = Color.FromArgb(64, 64, 64);
_text = value;
tbx_text.Text = _text;
}
}
}
/// <summary>
/// 默认文本
/// </summary>
private string _defaultText = string.Empty;
public string DefaultText
{
get { return _defaultText; }
set { _defaultText = value; }
}
/// <summary>
/// 是否以密码形式显示
/// </summary>
private bool _passwordChar = false;
public bool PasswordChar
{
get { return _passwordChar; }
set
{
_passwordChar = value;
if (PasswordChar)
{
tbx_text.PasswordChar = '*';
}
else
{
tbx_text.PasswordChar = '\0';
}
}
}
private void TextBox_Enter(object sender, EventArgs e)
{
lbl_line.Height = 2;
lbl_line.BackColor = Color.FromArgb(18, 150, 219);
if (tbx_text.Text == DefaultText)
{
tbx_text.SelectionStart = 0;
tbx_text.SelectionLength = 0;
}
}
private void TextBox_Leave(object sender, EventArgs e)
{
lbl_line.Height = 1;
lbl_line.BackColor = Color.Gray;
}
private void tbx_text_TextChanged(object sender, EventArgs e)
{
if (tbx_text.Text != DefaultText && tbx_text.Text != TextStr)
TextStr = tbx_text.Text;
if (TextStrChanged != null)
TextStrChanged(TextStr);
if (PasswordChar && TextStr != string.Empty)
tbx_text.PasswordChar = '*';
else if (TextStr ==string .Empty )
tbx_text.PasswordChar = '\0';
}
private void tbx_text_KeyDown(object sender, KeyEventArgs e)
{
if (tbx_text.Text == DefaultText && e.KeyCode != Keys.Back)
tbx_text.Text = string.Empty;
}
private void tbx_text_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
if (tbx_text.Text == DefaultText)
{
tbx_text.SelectionStart = 0;
tbx_text.SelectionLength = 0;
}
}
}
private void btn_eye_MouseDown(object sender, MouseEventArgs e)
{
tbx_text.PasswordChar = '\0';
btn_eye.BackgroundImage = Resources.Show;
//tbx_text.Width = tbx_text.Width - 30;
}
private void btn_eye_MouseUp(object sender, MouseEventArgs e)
{
if (TextStr != string.Empty)
tbx_text.PasswordChar = '*';
btn_eye.BackgroundImage = Resources.Hide;
tbx_text.Width = tbx_text.Width + 30;
tbx_text.Focus();
tbx_text.SelectionStart = 0;
tbx_text.SelectionLength = 0;
}
private void TextBox_Load(object sender, EventArgs e)
{
if (!PasswordChar)
{
tbx_text.PasswordChar = '\0';
btn_eye.Visible = false;
}
else
{
tbx_text.PasswordChar = '\0';
btn_eye.Visible = true ;
}
}
}
}