Menu

[6edf59]: / IPTextBox.cs  Maximize  Restore  History

Download this file

240 lines (220 with data), 7.4 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
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
//-----------------------------------------------------------------------
// <copyright file="IPTextBox.cs" company="NoteFly">
// NoteFly a note application.
// Copyright (C) 2012 Tom
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
// </copyright>
//-----------------------------------------------------------------------
namespace NoteFly
{
using System.Drawing;
using System.Windows.Forms;
/// <summary>
/// IP address textbox.
/// With background color ip address validation hint.
/// </summary>
internal partial class IPTextBox : TextBox
{
/// <summary>
/// IPAddress object.
/// </summary>
private System.Net.IPAddress ipaddr;
/// <summary>
/// Use a IPv4 address.
/// </summary>
private bool useipv4addr = true;
/// <summary>
/// Use a IPv6 address
/// </summary>
private bool useipv6addr = true;
/// <summary>
/// Gets or sets a value indicating whether a IPv4 address is used.
/// </summary>
public bool UseIPv4addr
{
get
{
return this.useipv4addr;
}
set
{
this.useipv4addr = value;
}
}
/// <summary>
/// Gets or sets a value indicating whether a IPv6 address is used.
/// </summary>
public bool UseIPv6addr
{
get
{
return this.useipv6addr;
}
set
{
this.useipv6addr = value;
}
}
/// <summary>
/// Get the IP address
/// </summary>
/// <returns>The IP address as string or empty string if no address entered</returns>
public string GetIPAddress()
{
if (this.ipaddr != null)
{
return this.ipaddr.ToString();
}
else
{
return string.Empty;
}
}
/// <summary>
/// Check if the ip address in the text propertie is valid.
/// And set the backgroundcolor of this control if text contains a valid ip address to LightGreen.
/// And if the text contains an invalid ip address to Salmon.
/// </summary>
public void ValidateIP()
{
bool validipaddr = System.Net.IPAddress.TryParse(this.Text, out this.ipaddr);
if (validipaddr)
{
if (this.getnumofdots() == 3 && this.getnumofdash() == 0)
{
// valid IPv4 address
if (this.useipv4addr)
{
this.BackColor = System.Drawing.Color.LightGreen;
}
else
{
// IPv4 address not allowed, but ip valid, orange for "warning".
this.BackColor = System.Drawing.Color.Orange;
}
}
else if (this.getnumofdots() == 0 && this.getnumofdash() >= 2)
{
// valid IPv6 address
if (this.useipv6addr)
{
this.BackColor = System.Drawing.Color.LightGreen;
}
else
{
// IPv6 address not allowed, but ip valid, orange for "warning".
this.BackColor = System.Drawing.Color.Orange;
}
}
}
else
{
if (this.TextLength > 0)
{
this.BackColor = System.Drawing.Color.Salmon;
}
else
{
this.BackColor = System.Drawing.SystemColors.Window;
}
}
}
/// <summary>
/// Character entered.
/// Allow only IPv4 and IPv6 characters to be added.
/// </summary>
/// <param name="e">Keyevent arguments</param>
protected override void OnKeyDown(KeyEventArgs e)
{
int k = e.KeyValue;
// 'a'-'f'
// ':'
// '.'
// '0'-'9'
// shift, backspace, left, right, delete, end and home key
if ((k >= 65 && k <= 70) || (k == 186 && e.Shift) || (k == 190 && !e.Shift) || (k >= 48 && k <= 57) || (k == 8 || k == 16 || k == 37 || k == 39 || k == 46 || k == 35 || k == 36))
{
e.SuppressKeyPress = false;
}
else
{
e.SuppressKeyPress = true;
}
base.OnKeyDown(e);
}
/// <summary>
/// Validate ip address on key up.
/// </summary>
/// <param name="e">Keyboard event arguments</param>
protected override void OnKeyUp(KeyEventArgs e)
{
this.ValidateIP();
base.OnKeyUp(e);
}
/// <summary>
/// Revalidate ip address on enable/disable change of this control.
/// </summary>
/// <param name="e">Event arguments</param>
protected override void OnEnabledChanged(System.EventArgs e)
{
if (!this.Enabled)
{
this.BackColor = SystemColors.Control;
}
else
{
this.BackColor = SystemColors.Window;
if (this.TextLength > 0)
{
this.ValidateIP();
}
}
base.OnEnabledChanged(e);
}
/// <summary>
/// Get the number of dots in the Text content.
/// </summary>
/// <returns>The number of dots</returns>
private int getnumofdots()
{
return this.getnumofchar('.');
}
/// <summary>
/// Get the number of dashes in the Text content.
/// </summary>
/// <returns>The number of dashes</returns>
private int getnumofdash()
{
return this.getnumofchar(':');
}
/// <summary>
/// Get the number of occurance of a particular character in the Text content.
/// </summary>
/// <param name="c">The characters to count</param>
/// <returns>The number of chracters</returns>
private int getnumofchar(char c)
{
int numdots = 0;
for (int i = 0; i < this.Text.Length; i++)
{
if (this.Text[i] == c)
{
numdots++;
}
}
return numdots;
}
}
}
MongoDB Logo MongoDB