1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2008 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
4 | *
|
---|
5 | * This file is part of HeuristicLab.
|
---|
6 | *
|
---|
7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
8 | * it under the terms of the GNU General Public License as published by
|
---|
9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
10 | * (at your option) any later version.
|
---|
11 | *
|
---|
12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | * GNU General Public License for more details.
|
---|
16 | *
|
---|
17 | * You should have received a copy of the GNU General Public License
|
---|
18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
19 | */
|
---|
20 | #endregion
|
---|
21 |
|
---|
22 | // copied from http://www.codeproject.com/KB/edit/IPAddressTextBox.aspx
|
---|
23 |
|
---|
24 | using System;
|
---|
25 | using System.Collections;
|
---|
26 | using System.ComponentModel;
|
---|
27 | using System.Windows.Forms;
|
---|
28 | using System.Text.RegularExpressions;
|
---|
29 |
|
---|
30 | namespace HeuristicLab.Hive.Client.Console {
|
---|
31 | [
|
---|
32 | DefaultProperty("Text"),
|
---|
33 | DefaultEvent("TextChanged"),
|
---|
34 | ]
|
---|
35 | /// <summary>
|
---|
36 | /// IPAddressTextBox
|
---|
37 | /// Control to enter IP-Addresses manually
|
---|
38 | /// Supports Binary and Decimal Notation
|
---|
39 | /// Supports input of CIDR Notation (appending of Bitmask of Subnetmask devided by Slash)
|
---|
40 | /// Support input of IP-Address in IPv6 format
|
---|
41 | /// </summary>
|
---|
42 | public class IPAddressTextBox : System.Windows.Forms.TextBox {
|
---|
43 | /// <summary>
|
---|
44 | /// Required designer variable.
|
---|
45 | /// </summary>
|
---|
46 | private System.ComponentModel.Container components = null;
|
---|
47 |
|
---|
48 | private IPNotation ipNotation = IPNotation.IPv4Decimal;
|
---|
49 | private IPNotation newIPNotation = IPNotation.IPv4Decimal;
|
---|
50 | private bool bOverwrite = true;
|
---|
51 | private bool bPreventLeave = true;
|
---|
52 | private System.Windows.Forms.ErrorProvider error;
|
---|
53 | private Regex regexValidNumbers = new Regex("[0-9]");
|
---|
54 | private ArrayList arlDelimeter = new ArrayList(new char[] { '.' });
|
---|
55 |
|
---|
56 |
|
---|
57 | public enum IPNotation {
|
---|
58 | IPv4Decimal, //192.168.000.001
|
---|
59 | IPv4Binary, //11000000.10101000.00000000.00000001
|
---|
60 | IPv4DecimalCIDR, //192.168.000.001/16
|
---|
61 | IPv4BinaryCIDR, //11000000.10101000.00000000.00000001/16
|
---|
62 | }
|
---|
63 |
|
---|
64 | /// <summary>
|
---|
65 | /// Constructor
|
---|
66 | /// </summary>
|
---|
67 | public IPAddressTextBox()
|
---|
68 | : base() {
|
---|
69 | this.InitializeComponent();
|
---|
70 |
|
---|
71 | this.ResetText();
|
---|
72 | }
|
---|
73 |
|
---|
74 | private void InitializeComponent() {
|
---|
75 | this.error = new System.Windows.Forms.ErrorProvider();
|
---|
76 | }
|
---|
77 |
|
---|
78 | /// <summary>
|
---|
79 | /// Clean up any resources being used.
|
---|
80 | /// </summary>
|
---|
81 | protected override void Dispose(bool disposing) {
|
---|
82 | if (disposing) {
|
---|
83 | if (components != null)
|
---|
84 | components.Dispose();
|
---|
85 | }
|
---|
86 | base.Dispose(disposing);
|
---|
87 | }
|
---|
88 |
|
---|
89 | #region Properties
|
---|
90 | #region Not allowed Properties from BaseClass
|
---|
91 | /// <summary>
|
---|
92 | /// Multiline is not allowed
|
---|
93 | /// </summary>
|
---|
94 | [
|
---|
95 | Category("Behavior"),
|
---|
96 | Description("Multiline is not allowed"),
|
---|
97 | DefaultValue(false),
|
---|
98 | Browsable(false)
|
---|
99 | ]
|
---|
100 | public override bool Multiline {
|
---|
101 | get {
|
---|
102 | return base.Multiline;
|
---|
103 | }
|
---|
104 | set {
|
---|
105 | //base.Multiline = value;
|
---|
106 | base.Multiline = false;
|
---|
107 | }
|
---|
108 | }
|
---|
109 | [
|
---|
110 | Category("Behavior"),
|
---|
111 | Description("AllowDrop is not allowed"),
|
---|
112 | DefaultValue(false),
|
---|
113 | Browsable(false)
|
---|
114 | ]
|
---|
115 | public override bool AllowDrop {
|
---|
116 | get {
|
---|
117 | return base.AllowDrop;
|
---|
118 | }
|
---|
119 | set {
|
---|
120 | //base.AllowDrop = value;
|
---|
121 | base.AllowDrop = false;
|
---|
122 | }
|
---|
123 | }
|
---|
124 | [
|
---|
125 | Category("Behavior"),
|
---|
126 | Description("AcceptsReturn is not allowed"),
|
---|
127 | DefaultValue(false),
|
---|
128 | Browsable(false)
|
---|
129 | ]
|
---|
130 | public new bool AcceptsReturn {
|
---|
131 | get {
|
---|
132 | return base.AcceptsReturn;
|
---|
133 | }
|
---|
134 | set {
|
---|
135 | //base.AcceptsReturn = value;
|
---|
136 | base.AcceptsReturn = false;
|
---|
137 | }
|
---|
138 | }
|
---|
139 | [
|
---|
140 | Category("Behavior"),
|
---|
141 | Description("AcceptsTab is not allowed"),
|
---|
142 | DefaultValue(false),
|
---|
143 | Browsable(false)
|
---|
144 | ]
|
---|
145 | public new bool AcceptsTab {
|
---|
146 | get {
|
---|
147 | return base.AcceptsTab;
|
---|
148 | }
|
---|
149 | set {
|
---|
150 | //base.AcceptTab = value;
|
---|
151 | base.AcceptsTab = false;
|
---|
152 | }
|
---|
153 | }
|
---|
154 | [
|
---|
155 | Category("Behavior"),
|
---|
156 | Description("CharacterCasing is not allowed"),
|
---|
157 | DefaultValue(CharacterCasing.Normal),
|
---|
158 | Browsable(false)
|
---|
159 | ]
|
---|
160 | public new CharacterCasing CharacterCasing {
|
---|
161 | get {
|
---|
162 | return base.CharacterCasing;
|
---|
163 | }
|
---|
164 | set {
|
---|
165 | //base.CharacterCasing = value;
|
---|
166 | base.CharacterCasing = CharacterCasing.Normal;
|
---|
167 | }
|
---|
168 | }
|
---|
169 | [
|
---|
170 | Category("Behavior"),
|
---|
171 | Description("WordWrap is not allowed"),
|
---|
172 | DefaultValue(true),
|
---|
173 | Browsable(false)
|
---|
174 | ]
|
---|
175 | public new bool WordWrap {
|
---|
176 | get {
|
---|
177 | return base.WordWrap;
|
---|
178 | }
|
---|
179 | set {
|
---|
180 | //base.WordWrap = value;
|
---|
181 | base.WordWrap = true;
|
---|
182 | }
|
---|
183 | }
|
---|
184 |
|
---|
185 | /// <summary>
|
---|
186 | /// Maxlength must not be changed by user
|
---|
187 | /// </summary>
|
---|
188 | [
|
---|
189 | Category("Behavior"),
|
---|
190 | Description("Specifies maximum length of a String. Change is not allowed"),
|
---|
191 | DefaultValue(15),
|
---|
192 | Browsable(false)
|
---|
193 | ]
|
---|
194 | public override int MaxLength {
|
---|
195 | get {
|
---|
196 | return base.MaxLength;
|
---|
197 | }
|
---|
198 | set {
|
---|
199 | //base.MaxLength = value;
|
---|
200 | base.MaxLength = this.Text.Length;
|
---|
201 | }
|
---|
202 | }
|
---|
203 |
|
---|
204 | #endregion // Not allowed Properties from BaseClass
|
---|
205 |
|
---|
206 | /// <summary>
|
---|
207 | /// Specifies if IP-Address
|
---|
208 | /// </summary>
|
---|
209 | [
|
---|
210 | Category("Appearance"),
|
---|
211 | Description("Specifies the IP-Address"),
|
---|
212 | DefaultValue(" . . . ")
|
---|
213 | ]
|
---|
214 | public override string Text {
|
---|
215 | get {
|
---|
216 | return base.Text;
|
---|
217 | }
|
---|
218 | set {
|
---|
219 | try {
|
---|
220 | if (IPAddressTextBox.ValidateIP(value, this.newIPNotation, this.arlDelimeter))
|
---|
221 | base.Text = IPAddressTextBox.MakeValidSpaces(value, this.newIPNotation, this.arlDelimeter);
|
---|
222 | }
|
---|
223 | catch {
|
---|
224 |
|
---|
225 | }
|
---|
226 | }
|
---|
227 | }
|
---|
228 |
|
---|
229 | /// <summary>
|
---|
230 | /// Specifies if Numbers should be overwritten
|
---|
231 | /// </summary>
|
---|
232 | [
|
---|
233 | Category("Behavior"),
|
---|
234 | Description("Specifies if Numbers should be overwritten"),
|
---|
235 | DefaultValue(true)
|
---|
236 | ]
|
---|
237 | public bool OverWriteMode {
|
---|
238 | get {
|
---|
239 | return this.bOverwrite;
|
---|
240 | }
|
---|
241 | set {
|
---|
242 | if (value != this.bOverwrite) {
|
---|
243 | this.bOverwrite = value;
|
---|
244 | this.OnOverWriteChanged(value);
|
---|
245 | }
|
---|
246 | }
|
---|
247 | }
|
---|
248 |
|
---|
249 | /// <summary>
|
---|
250 | /// Prevents leaving of Control if there is an input-error
|
---|
251 | /// </summary>
|
---|
252 | [
|
---|
253 | Category("Behavior"),
|
---|
254 | Description("Prevents leaving of Control if there is an input-error"),
|
---|
255 | DefaultValue(true)
|
---|
256 | ]
|
---|
257 | public bool PreventLeaveAtError {
|
---|
258 | get {
|
---|
259 | return this.bPreventLeave;
|
---|
260 | }
|
---|
261 | set {
|
---|
262 | if (value != this.bPreventLeave) {
|
---|
263 | this.bPreventLeave = value;
|
---|
264 | this.OnPreventLeaveChanged(value);
|
---|
265 | }
|
---|
266 | }
|
---|
267 | }
|
---|
268 |
|
---|
269 | /// <summary>
|
---|
270 | /// Specifies if IP-Address Notation (IPv4, IPv6, Binary, Decimal, CIDR
|
---|
271 | /// </summary>
|
---|
272 | [
|
---|
273 | Category("Appearance"),
|
---|
274 | Description("Specifies if IP-Address Notation (IPv4, IPv6, Binary, Decimal, CIDR"),
|
---|
275 | DefaultValue(IPNotation.IPv4Decimal)
|
---|
276 | ]
|
---|
277 | public IPNotation Notation {
|
---|
278 | get {
|
---|
279 | return this.ipNotation;
|
---|
280 | }
|
---|
281 | set {
|
---|
282 | if (value != this.ipNotation) {
|
---|
283 | try {
|
---|
284 | this.newIPNotation = value;
|
---|
285 | this.ChangeNotation(this.ipNotation, this.newIPNotation);
|
---|
286 | this.ipNotation = this.newIPNotation;
|
---|
287 | this.OnNotationChanged(this.newIPNotation);
|
---|
288 | }
|
---|
289 | catch (Exception LastError) {
|
---|
290 | System.Diagnostics.Debug.WriteLine(LastError.Message);
|
---|
291 | throw LastError;
|
---|
292 | }
|
---|
293 | }
|
---|
294 | }
|
---|
295 | }
|
---|
296 |
|
---|
297 | /// <summary>
|
---|
298 | /// Specifies the Errorprovider that appears at invalid IPs
|
---|
299 | /// </summary>
|
---|
300 | [
|
---|
301 | Category("Appearance"),
|
---|
302 | Description("Specifies the Errorprovider that appears at invalid IPs"),
|
---|
303 | DefaultValue(false)
|
---|
304 | ]
|
---|
305 | public ErrorProvider IPError {
|
---|
306 | get {
|
---|
307 | return this.error;
|
---|
308 | }
|
---|
309 | }
|
---|
310 |
|
---|
311 |
|
---|
312 | #endregion //Properties
|
---|
313 |
|
---|
314 | #region Eventhandling
|
---|
315 |
|
---|
316 | /// <summary>
|
---|
317 | /// Delegate for Notation-Events
|
---|
318 | /// </summary>
|
---|
319 | public delegate void NotationChangedEventHandler(IPNotation arg_newValue);
|
---|
320 | /// <summary>
|
---|
321 | /// Event called if AppearanceMode Notation is changed
|
---|
322 | /// </summary>
|
---|
323 | public event NotationChangedEventHandler NotationChanged;
|
---|
324 | /// <summary>
|
---|
325 | /// Delegate for Bool-Properties-Events
|
---|
326 | /// </summary>
|
---|
327 | public delegate void BoolPropertyChangedEventHandler(bool arg_bNewValue);
|
---|
328 | /// <summary>
|
---|
329 | /// Event called if BehaviorMode OverWriteMode is changed
|
---|
330 | /// </summary>
|
---|
331 | public event BoolPropertyChangedEventHandler OverWriteModeChanged;
|
---|
332 | /// <summary>
|
---|
333 | /// Event called if BehaviorMode PreventLeave is changed
|
---|
334 | /// </summary>
|
---|
335 | public event BoolPropertyChangedEventHandler PreventLeaveChanged;
|
---|
336 |
|
---|
337 | /// <summary>
|
---|
338 | /// Occures when Appearance-Mode Notation was changed
|
---|
339 | /// </summary>
|
---|
340 | /// <param name="arg_Value">Value, Input IP-Address notation</param>
|
---|
341 | protected virtual void OnNotationChanged(IPNotation arg_Value) {
|
---|
342 | if (this.NotationChanged != null)
|
---|
343 | this.NotationChanged(arg_Value);
|
---|
344 | }
|
---|
345 |
|
---|
346 | private void ChangeNotation(IPNotation arg_oldValue, IPNotation arg_newValue) {
|
---|
347 | string sTo = "";
|
---|
348 | ArrayList arlFrom = new ArrayList(this.Text.Replace(" ", "").Split((char[])this.arlDelimeter.ToArray(typeof(char))));
|
---|
349 |
|
---|
350 | switch (arg_newValue) {
|
---|
351 | case IPNotation.IPv4Decimal:
|
---|
352 | this.regexValidNumbers = new Regex("[0-9]");
|
---|
353 | this.arlDelimeter = new ArrayList(new char[] { '.' });
|
---|
354 | break;
|
---|
355 | case IPNotation.IPv4DecimalCIDR:
|
---|
356 | this.regexValidNumbers = new Regex("[0-9]");
|
---|
357 | this.arlDelimeter = new ArrayList(new char[] { '.', '/' });
|
---|
358 | break;
|
---|
359 | case IPNotation.IPv4Binary:
|
---|
360 | this.regexValidNumbers = new Regex("[01]");
|
---|
361 | this.arlDelimeter = new ArrayList(new char[] { '.' });
|
---|
362 | break;
|
---|
363 | case IPNotation.IPv4BinaryCIDR:
|
---|
364 | this.regexValidNumbers = new Regex("[01]");
|
---|
365 | this.arlDelimeter = new ArrayList(new char[] { '.', '/' });
|
---|
366 | break;
|
---|
367 | default:
|
---|
368 | break;
|
---|
369 | }
|
---|
370 |
|
---|
371 | switch (arg_oldValue) {
|
---|
372 | case IPNotation.IPv4Decimal:
|
---|
373 | switch (arg_newValue) {
|
---|
374 | case IPNotation.IPv4Decimal:
|
---|
375 | break;
|
---|
376 | case IPNotation.IPv4DecimalCIDR:
|
---|
377 | for (int i = 0; i < arlFrom.Count; i++) {
|
---|
378 | sTo += arlFrom[i].ToString() +
|
---|
379 | //Add Slash if its the last IPPart, els add a dot
|
---|
380 | (i == arlFrom.Count - 1 ? "/ " : ".");
|
---|
381 | }
|
---|
382 | break;
|
---|
383 | case IPNotation.IPv4Binary:
|
---|
384 | for (int i = 0; i < arlFrom.Count; i++) {
|
---|
385 | //Convert Decimal to Binary
|
---|
386 | sTo += this.Dec2Bin(arlFrom[i].ToString()) +
|
---|
387 | //Add Dot if its not the last IPPart, else add nothing
|
---|
388 | (i == arlFrom.Count - 1 ? "" : ".");
|
---|
389 | }
|
---|
390 | break;
|
---|
391 | case IPNotation.IPv4BinaryCIDR:
|
---|
392 | for (int i = 0; i < arlFrom.Count; i++) {
|
---|
393 | //Convert Decimal to Binary
|
---|
394 | sTo += this.Dec2Bin(arlFrom[i].ToString()) +
|
---|
395 | //Add Slash if its the last IPPart, else add a dot
|
---|
396 | (i == arlFrom.Count - 1 ? "/ " : ".");
|
---|
397 | }
|
---|
398 | break;
|
---|
399 | default:
|
---|
400 | break;
|
---|
401 | }
|
---|
402 | break;
|
---|
403 | case IPNotation.IPv4DecimalCIDR:
|
---|
404 | switch (arg_newValue) {
|
---|
405 | case IPNotation.IPv4Decimal:
|
---|
406 | //do not use the last Item, its the Subnetmask
|
---|
407 | for (int i = 0; i < arlFrom.Count - 1; i++) {
|
---|
408 | sTo += arlFrom[i].ToString() +
|
---|
409 | //Add Dot if its not the last IPPart, else add nothing
|
---|
410 | (i == arlFrom.Count - 2 ? "" : ".");
|
---|
411 | }
|
---|
412 | break;
|
---|
413 | case IPNotation.IPv4DecimalCIDR:
|
---|
414 | break;
|
---|
415 | case IPNotation.IPv4Binary:
|
---|
416 | //do not use the last Item, its the Subnetmask
|
---|
417 | for (int i = 0; i < arlFrom.Count - 1; i++) {
|
---|
418 | //Convert Decimal to Binary
|
---|
419 | sTo += this.Dec2Bin(arlFrom[i].ToString()) +
|
---|
420 | //Add Dot if its not the last IPPart, else add nothing
|
---|
421 | (i == arlFrom.Count - 2 ? "" : ".");
|
---|
422 | }
|
---|
423 | break;
|
---|
424 | case IPNotation.IPv4BinaryCIDR:
|
---|
425 | //do not use the last Item, its the Subnetmask
|
---|
426 | for (int i = 0; i < arlFrom.Count - 1; i++) {
|
---|
427 | //Convert Decimal to Binary
|
---|
428 | sTo += this.Dec2Bin(arlFrom[i].ToString()) +
|
---|
429 | //Add Dot if its not the last IPPart, else add nothing
|
---|
430 | (i == arlFrom.Count - 2 ? "" : ".");
|
---|
431 | }
|
---|
432 | //Add Subnetmask
|
---|
433 | sTo += "/" + arlFrom[arlFrom.Count - 1];
|
---|
434 | break;
|
---|
435 | default:
|
---|
436 | break;
|
---|
437 | }
|
---|
438 | break;
|
---|
439 | case IPNotation.IPv4Binary:
|
---|
440 | switch (arg_newValue) {
|
---|
441 | case IPNotation.IPv4Decimal:
|
---|
442 | for (int i = 0; i < arlFrom.Count; i++) {
|
---|
443 | //Convert Binary to Decimal
|
---|
444 | sTo += this.Bin2Dec(arlFrom[i].ToString()) +
|
---|
445 | //Add Dot if its not the last IPPart, else add nothing
|
---|
446 | (i == arlFrom.Count - 1 ? "" : ".");
|
---|
447 | }
|
---|
448 | break;
|
---|
449 | case IPNotation.IPv4DecimalCIDR:
|
---|
450 | for (int i = 0; i < arlFrom.Count; i++) {
|
---|
451 | //Convert Binary to Decimal
|
---|
452 | sTo += this.Bin2Dec(arlFrom[i].ToString()) +
|
---|
453 | //Add Slash if its the last IPPart, els add a dot
|
---|
454 | (i == arlFrom.Count - 1 ? "/ " : ".");
|
---|
455 | }
|
---|
456 | break;
|
---|
457 | case IPNotation.IPv4Binary:
|
---|
458 | break;
|
---|
459 | case IPNotation.IPv4BinaryCIDR:
|
---|
460 | for (int i = 0; i < arlFrom.Count; i++) {
|
---|
461 | sTo += arlFrom[i].ToString() +
|
---|
462 | //Add Slash if its the last IPPart, else add a dot
|
---|
463 | (i == arlFrom.Count - 1 ? "/ " : ".");
|
---|
464 | }
|
---|
465 | break;
|
---|
466 | default:
|
---|
467 | break;
|
---|
468 | }
|
---|
469 | break;
|
---|
470 | case IPNotation.IPv4BinaryCIDR:
|
---|
471 | switch (arg_newValue) {
|
---|
472 | case IPNotation.IPv4Decimal:
|
---|
473 | //do not use the last Item, its the Subnetmask
|
---|
474 | for (int i = 0; i < arlFrom.Count - 1; i++) {
|
---|
475 | sTo += this.Bin2Dec(arlFrom[i].ToString()) +
|
---|
476 | //Add Dot if its not the last IPPart, else add nothing
|
---|
477 | (i == arlFrom.Count - 2 ? "" : ".");
|
---|
478 | }
|
---|
479 | break;
|
---|
480 | case IPNotation.IPv4DecimalCIDR:
|
---|
481 | //do not use the last Item, its the Subnetmask
|
---|
482 | for (int i = 0; i < arlFrom.Count - 1; i++) {
|
---|
483 | sTo += this.Bin2Dec(arlFrom[i].ToString()) +
|
---|
484 | //Add Dot if its not the last IPPart, else add nothing
|
---|
485 | (i == arlFrom.Count - 2 ? "" : ".");
|
---|
486 | }
|
---|
487 | //Add Subnetmask
|
---|
488 | sTo += "/" + arlFrom[arlFrom.Count - 1];
|
---|
489 | break;
|
---|
490 | case IPNotation.IPv4Binary:
|
---|
491 | //do not use the last Item, its the Subnetmask
|
---|
492 | for (int i = 0; i < arlFrom.Count - 1; i++) {
|
---|
493 | sTo += arlFrom[i].ToString() +
|
---|
494 | //Add Dot if its not the last IPPart, else add nothing
|
---|
495 | (i == arlFrom.Count - 2 ? "" : ".");
|
---|
496 | }
|
---|
497 | break;
|
---|
498 | case IPNotation.IPv4BinaryCIDR:
|
---|
499 | break;
|
---|
500 | default:
|
---|
501 | break;
|
---|
502 | }
|
---|
503 | break;
|
---|
504 |
|
---|
505 | }
|
---|
506 |
|
---|
507 | this.Text = sTo;
|
---|
508 | this.MaxLength = this.TextLength;
|
---|
509 | }
|
---|
510 |
|
---|
511 | /// <summary>
|
---|
512 | /// Occures when Behaviour-Mode OverWriteMode was changed
|
---|
513 | /// </summary>
|
---|
514 | /// <param name="arg_bValue">Value, Overwrite Numbers in Editfield or not</param>
|
---|
515 | protected virtual void OnOverWriteChanged(bool arg_bValue) {
|
---|
516 | if (this.OverWriteModeChanged != null)
|
---|
517 | this.OverWriteModeChanged(arg_bValue);
|
---|
518 | }
|
---|
519 |
|
---|
520 | /// <summary>
|
---|
521 | /// Occures when Behaviour-Mode PreventLeave was changed
|
---|
522 | /// </summary>
|
---|
523 | /// <param name="arg_bValue">Value, leave control if there is an error or not</param>
|
---|
524 | protected virtual void OnPreventLeaveChanged(bool arg_bValue) {
|
---|
525 | if (this.PreventLeaveChanged != null)
|
---|
526 | this.PreventLeaveChanged(arg_bValue);
|
---|
527 | }
|
---|
528 |
|
---|
529 | #endregion //events
|
---|
530 |
|
---|
531 | #region Event-Overrides
|
---|
532 |
|
---|
533 | /// <summary>
|
---|
534 | /// Override standard KeyDownEventHandler
|
---|
535 | /// Catches Inputs of "." and "/" to jump to next positions
|
---|
536 | /// </summary>
|
---|
537 | /// <param name="e">KeyEventArgument</param>
|
---|
538 | protected override void OnKeyDown(KeyEventArgs e) {
|
---|
539 | //Zeichen an die richtige stelle schreiben
|
---|
540 | int iPos = this.SelectionStart;
|
---|
541 | char[] cText = this.Text.ToCharArray();
|
---|
542 |
|
---|
543 | if (e.Modifiers == Keys.None) {
|
---|
544 | if ((char.IsLetterOrDigit(Convert.ToChar(e.KeyValue)) || e.KeyCode == Keys.NumPad0)//Numpad0=96 --> `
|
---|
545 | && iPos < this.TextLength) {
|
---|
546 | if (this.arlDelimeter.Contains(cText[iPos]))
|
---|
547 | iPos += 1;
|
---|
548 | this.SelectionStart = iPos;
|
---|
549 | if (this.OverWriteMode) {
|
---|
550 | if (iPos < this.TextLength)
|
---|
551 | this.SelectionLength = 1;
|
---|
552 | } else {
|
---|
553 | if (iPos < this.TextLength)
|
---|
554 | if (cText[iPos] == ' ')
|
---|
555 | this.SelectionLength = 1;
|
---|
556 | }
|
---|
557 | }
|
---|
558 | }
|
---|
559 | base.OnKeyDown(e);
|
---|
560 | }
|
---|
561 |
|
---|
562 | /// <summary>
|
---|
563 | /// Override standard KeyUpEventHandler
|
---|
564 | /// Catches Inputs of "." and "/" to jump to next positions
|
---|
565 | /// </summary>
|
---|
566 | /// <param name="e">KeyEventArgument</param>
|
---|
567 | protected override void OnKeyUp(KeyEventArgs e) {
|
---|
568 | //Zeichen an die richtige stelle schreiben
|
---|
569 | int iPos = this.SelectionStart;
|
---|
570 | char[] cText = this.Text.ToCharArray();
|
---|
571 |
|
---|
572 | //Cursor hintern Punkt setzen
|
---|
573 | if ((char.IsLetterOrDigit(Convert.ToChar(e.KeyValue)) || e.KeyCode == Keys.NumPad0)//Numpad0=96 --> `
|
---|
574 | && iPos < this.TextLength) {
|
---|
575 | if (this.arlDelimeter.Contains(cText[iPos]))
|
---|
576 | iPos += 1;
|
---|
577 |
|
---|
578 | this.SelectionStart = iPos;
|
---|
579 | }
|
---|
580 | base.OnKeyUp(e);
|
---|
581 | }
|
---|
582 |
|
---|
583 |
|
---|
584 | /// <summary>
|
---|
585 | /// Override standard KeyPressEventHandler
|
---|
586 | /// Catches Inputs of "." and "/" to jump to next positions
|
---|
587 | /// </summary>
|
---|
588 | /// <param name="e">KeyPressEventArgument</param>
|
---|
589 | protected override void OnKeyPress(KeyPressEventArgs e) {
|
---|
590 | //valid input charachters
|
---|
591 | if (char.IsControl(e.KeyChar) ||
|
---|
592 | regexValidNumbers.IsMatch(e.KeyChar.ToString())) {
|
---|
593 | e.Handled = false;
|
---|
594 | } else {
|
---|
595 | switch (e.KeyChar) {
|
---|
596 | case '/':
|
---|
597 | this.JumpToSlash();
|
---|
598 | break;
|
---|
599 | case '.':
|
---|
600 | case ':':
|
---|
601 | this.JumpToNextDot();
|
---|
602 | break;
|
---|
603 | default:
|
---|
604 | break;
|
---|
605 | }
|
---|
606 | e.Handled = true;
|
---|
607 | }
|
---|
608 | base.OnKeyPress(e);
|
---|
609 | }
|
---|
610 |
|
---|
611 | /// <summary>
|
---|
612 | /// Override standard TextChangedEventHandler
|
---|
613 | /// Looks if inserted IP-Address is valid
|
---|
614 | /// </summary>
|
---|
615 | /// <param name="e">EventArgument</param>
|
---|
616 | protected override void OnTextChanged(EventArgs e) {
|
---|
617 | base.OnTextChanged(e);
|
---|
618 | if (this.Text.Length == 0)
|
---|
619 | this.ResetText();
|
---|
620 |
|
---|
621 | try {
|
---|
622 | if (!this.ValidateIP())
|
---|
623 | this.error.SetError(this, "Invalid IP-address");
|
---|
624 | else
|
---|
625 | this.error.SetError(this, "");
|
---|
626 | }
|
---|
627 | catch (Exception LastError) {
|
---|
628 | this.error.SetError(this, LastError.Message);
|
---|
629 | }
|
---|
630 | }
|
---|
631 |
|
---|
632 | /// <summary>
|
---|
633 | /// Override standard ValidatingEventHandler
|
---|
634 | /// Validates inserted IP-Address, and cancels Textbox if valid or PreventLeave=false
|
---|
635 | /// </summary>
|
---|
636 | /// <param name="e">CancelEventArgument</param>
|
---|
637 | protected override void OnValidating(CancelEventArgs e) {
|
---|
638 | //e.Cancel = true;//suppress cancel-signal = not validated
|
---|
639 | e.Cancel = (!this.ValidateIP() && this.bPreventLeave);
|
---|
640 | base.OnValidating(e);
|
---|
641 | }
|
---|
642 |
|
---|
643 | #endregion //Eventhandling
|
---|
644 |
|
---|
645 | #region Methods
|
---|
646 |
|
---|
647 | /// <summary>
|
---|
648 | /// Override standard ResetText
|
---|
649 | /// Fills Textbox with Dots and Slashes dependend on Properties
|
---|
650 | /// </summary>
|
---|
651 | public override void ResetText() {
|
---|
652 | base.ResetText();
|
---|
653 | switch (this.Notation) {
|
---|
654 | case IPNotation.IPv4Decimal:
|
---|
655 | this.Text = " . . . ";
|
---|
656 | break;
|
---|
657 | case IPNotation.IPv4DecimalCIDR:
|
---|
658 | this.Text = " . . . / ";
|
---|
659 | break;
|
---|
660 | case IPNotation.IPv4Binary:
|
---|
661 | this.Text = " . . . ";
|
---|
662 | break;
|
---|
663 | case IPNotation.IPv4BinaryCIDR:
|
---|
664 | this.Text = " . . . / ";
|
---|
665 | break;
|
---|
666 | default:
|
---|
667 | break;
|
---|
668 | }
|
---|
669 |
|
---|
670 | this.MaxLength = this.TextLength;
|
---|
671 | }
|
---|
672 |
|
---|
673 |
|
---|
674 | /// <summary>
|
---|
675 | /// Window-Message Constant
|
---|
676 | /// </summary>
|
---|
677 | protected const int WM_KEYDOWN = 0x0100;
|
---|
678 |
|
---|
679 | /// <summary>
|
---|
680 | /// Override standard PreProcessMessge
|
---|
681 | /// Catches Inputs of Backspaces and Deletes to remove IP-Digits at the right position
|
---|
682 | /// </summary>
|
---|
683 | /// <param name="msg">Process Message</param>
|
---|
684 | public override bool PreProcessMessage(ref Message msg) {
|
---|
685 | if (msg.Msg == WM_KEYDOWN) {
|
---|
686 | Keys keyData = ((Keys)(int)msg.WParam) | ModifierKeys;
|
---|
687 | Keys keyCode = ((Keys)(int)msg.WParam);
|
---|
688 |
|
---|
689 | int iPos = this.SelectionStart;
|
---|
690 | char[] cText = this.Text.ToCharArray();
|
---|
691 | switch (keyCode) {
|
---|
692 | case Keys.Delete:
|
---|
693 | if (iPos < this.TextLength) {
|
---|
694 | while (cText[iPos] == '.' || cText[iPos] == ':' || cText[iPos] == '/') {
|
---|
695 | if ((iPos += 1) >= cText.Length)
|
---|
696 | break;
|
---|
697 | }
|
---|
698 | if (iPos < this.TextLength) {
|
---|
699 | base.Text = this.Text.Substring(0, iPos) + " " + this.Text.Substring(iPos + 1);
|
---|
700 | this.SelectionStart = iPos + 1;
|
---|
701 | } else
|
---|
702 | this.SelectionStart = this.TextLength - 1;
|
---|
703 | }
|
---|
704 | return true;
|
---|
705 | case Keys.Back:
|
---|
706 | if (iPos > 0) {
|
---|
707 | while (cText[iPos - 1] == '.' || cText[iPos - 1] == ':' || cText[iPos - 1] == '/') {
|
---|
708 | if ((iPos -= 1) <= 0)
|
---|
709 | break;
|
---|
710 | }
|
---|
711 | if (iPos > 0) {
|
---|
712 | base.Text = this.Text.Substring(0, iPos - 1) + " " + this.Text.Substring(iPos);
|
---|
713 | this.SelectionStart = iPos - 1;
|
---|
714 | } else
|
---|
715 | this.SelectionStart = 0;
|
---|
716 | }
|
---|
717 | return true;
|
---|
718 | default:
|
---|
719 | break;
|
---|
720 | }
|
---|
721 | }
|
---|
722 | return base.PreProcessMessage(ref msg);
|
---|
723 | }
|
---|
724 |
|
---|
725 | /// <summary>
|
---|
726 | /// Returns the formatted IP-Addresses without spaces and Zeroes
|
---|
727 | /// </summary>
|
---|
728 | /// <returns>IP-Address without spaces and Zeroes</returns>
|
---|
729 | public string GetPureIPAddress() {
|
---|
730 | string s = "";
|
---|
731 | ArrayList arlIP = new ArrayList(this.Text.Replace(" ", "").Split((char[])this.arlDelimeter.ToArray(typeof(char))));
|
---|
732 | for (int i = 0; i < arlIP.Count; i++) {
|
---|
733 | while (arlIP[i].ToString().StartsWith("0"))
|
---|
734 | arlIP[i] = arlIP[i].ToString().Substring(1);
|
---|
735 | }
|
---|
736 | s = IPAddressTextBox.MakeIP((string[])arlIP.ToArray(typeof(string)), this.ipNotation);
|
---|
737 | return s;
|
---|
738 | }
|
---|
739 |
|
---|
740 | #endregion //Methods
|
---|
741 |
|
---|
742 | #region Helperfunctions
|
---|
743 |
|
---|
744 | /// <summary>
|
---|
745 | /// Sets Inputcursor to Subnet-Slash
|
---|
746 | /// </summary>
|
---|
747 | private void JumpToSlash() {
|
---|
748 | int iSelStart = this.Text.LastIndexOf("/");
|
---|
749 | if (iSelStart >= 0) {
|
---|
750 | this.Select(iSelStart + 1, 0);
|
---|
751 | }
|
---|
752 | }
|
---|
753 |
|
---|
754 | /// <summary>
|
---|
755 | /// Sets input cursour to next Dot
|
---|
756 | /// </summary>
|
---|
757 | private void JumpToNextDot() {
|
---|
758 | int iSelStart = this.Text.IndexOf('.', this.SelectionStart);
|
---|
759 | if (iSelStart >= 0) {
|
---|
760 | this.Select(iSelStart + 1, 0);
|
---|
761 | } else {
|
---|
762 | iSelStart = this.Text.IndexOf(':', this.SelectionStart);
|
---|
763 | if (iSelStart >= 0) {
|
---|
764 | this.Select(iSelStart + 1, 0);
|
---|
765 | }
|
---|
766 | }
|
---|
767 | }
|
---|
768 |
|
---|
769 | /// <summary>
|
---|
770 | /// Converts Decimal IP-Part to Binary (default IPv6 = false)
|
---|
771 | /// </summary>
|
---|
772 | /// <param name="arg_sDec">Decimal IP-Part</param>
|
---|
773 | /// <param name="arg_bIPv6">Binary for IPv6 (has 16 digits)</param>
|
---|
774 | /// <returns>Binary IP-Part</returns>
|
---|
775 | private string Dec2Bin(string arg_sDec) {
|
---|
776 | return this.Dec2Bin(arg_sDec, false);
|
---|
777 | }
|
---|
778 | /// <summary>
|
---|
779 | /// Converts Decimal IP-Part to Binary
|
---|
780 | /// </summary>
|
---|
781 | /// <param name="arg_sDec">Decimal IP-Part</param>
|
---|
782 | /// <param name="arg_bIPv6">Binary for IPv6 (has 16 digits)</param>
|
---|
783 | /// <returns>Binary IP-Part</returns>
|
---|
784 | private string Dec2Bin(string arg_sDec, bool arg_bIPv6) {
|
---|
785 | string sBin = (arg_bIPv6 ? "0000000000000000" : "00000000"), sSubnet = "";
|
---|
786 | arg_sDec = arg_sDec.Trim();
|
---|
787 | while (arg_sDec.Length < 3)
|
---|
788 | arg_sDec = "0" + arg_sDec;
|
---|
789 | if (arg_sDec.IndexOf("/") >= 0) {
|
---|
790 | sSubnet = arg_sDec.Substring(arg_sDec.IndexOf("/"));
|
---|
791 | arg_sDec = arg_sDec.Substring(0, arg_sDec.IndexOf("/"));
|
---|
792 | }
|
---|
793 | int iDec = Convert.ToInt32(arg_sDec, 10);
|
---|
794 | sBin = Convert.ToString(iDec, 2);
|
---|
795 | while (sBin.Length < (arg_bIPv6 ? 16 : 8))
|
---|
796 | sBin = "0" + sBin;
|
---|
797 | return sBin + sSubnet;
|
---|
798 | }
|
---|
799 |
|
---|
800 | /// <summary>
|
---|
801 | /// Converts Binary IP-Part to Decimal
|
---|
802 | /// </summary>
|
---|
803 | /// <param name="arg_sBin">Binary IP-Part</param>
|
---|
804 | /// <returns>Decimal IP-Part</returns>
|
---|
805 | private string Bin2Dec(string arg_sBin) {
|
---|
806 | string sDec = "000", sSubnet = "";
|
---|
807 | arg_sBin = arg_sBin.Trim();
|
---|
808 | while (arg_sBin.Length < 8)
|
---|
809 | arg_sBin = "0" + arg_sBin;
|
---|
810 | if (arg_sBin.IndexOf("/") >= 0) {
|
---|
811 | sSubnet = arg_sBin.Substring(arg_sBin.IndexOf("/"));
|
---|
812 | arg_sBin = arg_sBin.Substring(0, arg_sBin.IndexOf("/"));
|
---|
813 | }
|
---|
814 | int iBin = Convert.ToInt32(arg_sBin, 2);
|
---|
815 | if (iBin > 255)
|
---|
816 | throw new Exception(string.Format("Can't convert Binary to Decimal IP-Address\nbin:{0} is greater than 255", iBin));
|
---|
817 | sDec = Convert.ToString(iBin, 10);
|
---|
818 | while (sDec.Length < 3)
|
---|
819 | sDec = "0" + sDec;
|
---|
820 | return sDec + sSubnet;
|
---|
821 | }
|
---|
822 |
|
---|
823 | /// <summary>
|
---|
824 | /// Converts Binary IP-Part to Hexadecimal
|
---|
825 | /// </summary>
|
---|
826 | /// <param name="arg_sBin">Binary IP-Part</param>
|
---|
827 | /// <returns>Hexadecimal IP-Part</returns>
|
---|
828 | private string Bin2Hex(string arg_sBin) {
|
---|
829 | string sHex = "0000", sSubnet = "";
|
---|
830 | arg_sBin = arg_sBin.Trim();
|
---|
831 | while (arg_sBin.Length < 8)
|
---|
832 | arg_sBin = "0" + arg_sBin;
|
---|
833 | if (arg_sBin.IndexOf("/") >= 0) {
|
---|
834 | sSubnet = arg_sBin.Substring(arg_sBin.IndexOf("/"));
|
---|
835 | arg_sBin = arg_sBin.Substring(0, arg_sBin.IndexOf("/"));
|
---|
836 | }
|
---|
837 | int iBin = Convert.ToInt32(arg_sBin, 2);
|
---|
838 | sHex = Convert.ToString(iBin, 16);
|
---|
839 | while (sHex.Length < 4)
|
---|
840 | sHex = "0" + sHex;
|
---|
841 | return sHex + sSubnet;
|
---|
842 | }
|
---|
843 |
|
---|
844 | /// <summary>
|
---|
845 | /// Converts Hexadecimal IP-Part to Binary (default IPv6=true)
|
---|
846 | /// </summary>
|
---|
847 | /// <param name="arg_sHex">Hexadecimal IP-Part</param>
|
---|
848 | /// <returns>Binary IP-Part</returns>
|
---|
849 | private string Hex2Bin(string arg_sHex) {
|
---|
850 | return this.Hex2Bin(arg_sHex, true);
|
---|
851 | }
|
---|
852 | /// <summary>
|
---|
853 | /// Converts Hexadecimal IP-Part to Binary
|
---|
854 | /// </summary>
|
---|
855 | /// <param name="arg_sHex">Hexadecimal IP-Part</param>
|
---|
856 | /// <param name="arg_bIPv6">Binary for IPv6 (16 digits)</param>
|
---|
857 | /// <returns>Binary IP-Part</returns>
|
---|
858 | private string Hex2Bin(string arg_sHex, bool arg_bIPv6) {
|
---|
859 | string sBin = (arg_bIPv6 ? "0000000000000000" : "00000000"), sSubnet = "";
|
---|
860 | arg_sHex = arg_sHex.Trim();
|
---|
861 | while (arg_sHex.Length < 3)
|
---|
862 | arg_sHex = "0" + arg_sHex;
|
---|
863 | if (arg_sHex.IndexOf("/") >= 0) {
|
---|
864 | sSubnet = arg_sHex.Substring(arg_sHex.IndexOf("/"));
|
---|
865 | arg_sHex = arg_sHex.Substring(0, arg_sHex.IndexOf("/"));
|
---|
866 | }
|
---|
867 | int iHex = Convert.ToInt32(arg_sHex, 16);
|
---|
868 | if (iHex > 255 && !arg_bIPv6)
|
---|
869 | throw new Exception(string.Format("Can't convert Hexadecimal to Binary IP-Address\nhex:{0} is greater than 11111111", iHex));
|
---|
870 | sBin = Convert.ToString(iHex, 2);
|
---|
871 | while (sBin.Length < (arg_bIPv6 ? 16 : 8))
|
---|
872 | sBin = "0" + sBin;
|
---|
873 | return sBin + sSubnet;
|
---|
874 | }
|
---|
875 |
|
---|
876 | /// <summary>
|
---|
877 | /// Converts Decimal IP-Part to Hexadecimal
|
---|
878 | /// </summary>
|
---|
879 | /// <param name="arg_sDec">Decimal IP-Part</param>
|
---|
880 | /// <returns>Hexadecimal IP-Part</returns>
|
---|
881 | private string Dec2Hex(string arg_sDec) {
|
---|
882 | string sHex = "0000", sSubnet = "";
|
---|
883 | arg_sDec = arg_sDec.Trim();
|
---|
884 | while (arg_sDec.Length < 8)
|
---|
885 | arg_sDec = "0" + arg_sDec;
|
---|
886 | if (arg_sDec.IndexOf("/") >= 0) {
|
---|
887 | sSubnet = arg_sDec.Substring(arg_sDec.IndexOf("/"));
|
---|
888 | arg_sDec = arg_sDec.Substring(0, arg_sDec.IndexOf("/"));
|
---|
889 | }
|
---|
890 | int iDec = Convert.ToInt32(arg_sDec, 10);
|
---|
891 | sHex = Convert.ToString(iDec, 16);
|
---|
892 | while (sHex.Length < 4)
|
---|
893 | sHex = "0" + sHex;
|
---|
894 | return sHex + sSubnet;
|
---|
895 | }
|
---|
896 |
|
---|
897 | /// <summary>
|
---|
898 | /// Converts Hexadecimal IP-Part to Decimal
|
---|
899 | /// </summary>
|
---|
900 | /// <param name="arg_sHex">Hexadecimal IP-Part</param>
|
---|
901 | /// <returns>Decimal IP-Part</returns>
|
---|
902 | private string Hex2Dec(string arg_sHex) {
|
---|
903 | string sDec = "000", sSubnet = "";
|
---|
904 | arg_sHex = arg_sHex.Trim();
|
---|
905 | while (arg_sHex.Length < 8)
|
---|
906 | arg_sHex = "0" + arg_sHex;
|
---|
907 | if (arg_sHex.IndexOf("/") >= 0) {
|
---|
908 | sSubnet = arg_sHex.Substring(arg_sHex.IndexOf("/"));
|
---|
909 | arg_sHex = arg_sHex.Substring(0, arg_sHex.IndexOf("/"));
|
---|
910 | }
|
---|
911 | int iHex = Convert.ToInt32(arg_sHex, 16);
|
---|
912 | if (iHex > 255)
|
---|
913 | throw new Exception(string.Format("Can't convert Hexadecimal to Decimal IP-Address\nhex:{0} is greater than 255", iHex));
|
---|
914 | sDec = Convert.ToString(iHex, 10);
|
---|
915 | while (sDec.Length < 3)
|
---|
916 | sDec = "0" + sDec;
|
---|
917 | return sDec + sSubnet;
|
---|
918 | }
|
---|
919 |
|
---|
920 | /// <summary>
|
---|
921 | /// Checks if IP in Textfield is valid
|
---|
922 | /// </summary>
|
---|
923 | /// <returns>true/false valid/not</returns>
|
---|
924 | private bool ValidateIP() {
|
---|
925 | if (IPAddressTextBox.ValidateIP(this.Text, this.newIPNotation, this.arlDelimeter))
|
---|
926 | return true;
|
---|
927 | else
|
---|
928 | //if Control is not visible or enabled, it doesn't matter if IP is valid
|
---|
929 | return this.Enabled || this.Visible ? false : true;
|
---|
930 | }
|
---|
931 |
|
---|
932 | /// <summary>
|
---|
933 | /// Checks if the given String is an valid ip-address
|
---|
934 | /// </summary>
|
---|
935 | /// <param name="arg_sIP">IP-String</param>
|
---|
936 | /// <param name="arg_ipNotation">IP-notation</param>
|
---|
937 | /// <param name="arg_arlDelimeter">Delimeter to parse IPString</param>
|
---|
938 | /// <returns>true/false validated/not</returns>
|
---|
939 | protected static bool ValidateIP(string arg_sIP, IPNotation arg_ipNotation, ArrayList arg_arlDelimeter) {
|
---|
940 | bool bValidated = false;
|
---|
941 | ArrayList arlIP = new ArrayList(arg_sIP.Split((char[])arg_arlDelimeter.ToArray(typeof(char))));
|
---|
942 |
|
---|
943 | try {
|
---|
944 | switch (arg_ipNotation) {
|
---|
945 | case IPNotation.IPv4Decimal:
|
---|
946 | case IPNotation.IPv4Binary:
|
---|
947 | bValidated = arlIP.Count == 4;
|
---|
948 | break;
|
---|
949 | case IPNotation.IPv4DecimalCIDR:
|
---|
950 | case IPNotation.IPv4BinaryCIDR:
|
---|
951 | bValidated = arlIP.Count == 5;
|
---|
952 | break;
|
---|
953 | default:
|
---|
954 | break;
|
---|
955 | }
|
---|
956 | if (!bValidated) {
|
---|
957 | throw new Exception("IP-Address has wrong element count");
|
---|
958 | }
|
---|
959 |
|
---|
960 | //don't check the 1st 2 elemnt if its IPv4 in IPv6-notation
|
---|
961 | for (int i = (arg_ipNotation.ToString().IndexOf("IPv6IPv4") == 0 ? 2 : 0);
|
---|
962 | //don't check the subnet element
|
---|
963 | i < (arg_ipNotation.ToString().IndexOf("CIDR") > 0 ? arlIP.Count - 1 : arlIP.Count);
|
---|
964 | i++) {
|
---|
965 | string sIPPart = arlIP[i].ToString().Replace(" ", "");
|
---|
966 | int iIPPart = 0;
|
---|
967 | switch (arg_ipNotation) {
|
---|
968 | case IPNotation.IPv4Decimal:
|
---|
969 | case IPNotation.IPv4DecimalCIDR:
|
---|
970 | while (sIPPart.Length < 3)
|
---|
971 | sIPPart = "0" + sIPPart;
|
---|
972 | iIPPart = Convert.ToInt32(sIPPart, 10);
|
---|
973 | if (iIPPart < 256)
|
---|
974 | bValidated = true;
|
---|
975 | else
|
---|
976 | bValidated = false;
|
---|
977 | break;
|
---|
978 | case IPNotation.IPv4Binary:
|
---|
979 | case IPNotation.IPv4BinaryCIDR:
|
---|
980 | while (sIPPart.Length < 8)
|
---|
981 | sIPPart = "0" + sIPPart;
|
---|
982 | iIPPart = Convert.ToInt32(sIPPart, 2);
|
---|
983 | if (iIPPart < 256)
|
---|
984 | bValidated = true;
|
---|
985 | else
|
---|
986 | bValidated = false;
|
---|
987 | break;
|
---|
988 | default:
|
---|
989 | break;
|
---|
990 | }
|
---|
991 | if (!bValidated) {
|
---|
992 | throw new Exception(string.Format("IP-Address element {0}({1}) has wrong format", i, sIPPart));
|
---|
993 | }
|
---|
994 | }
|
---|
995 | }
|
---|
996 | catch (Exception LastError) {
|
---|
997 | System.Diagnostics.Debug.WriteLine(LastError.Message);
|
---|
998 | bValidated = false;
|
---|
999 | throw LastError;
|
---|
1000 | }
|
---|
1001 | return bValidated;
|
---|
1002 | }
|
---|
1003 |
|
---|
1004 | /// <summary>
|
---|
1005 | /// Adds Spaces to given IP-Address, so it fits in the textfield
|
---|
1006 | /// </summary>
|
---|
1007 | /// <param name="arg_sIP">IP-String</param>
|
---|
1008 | /// <param name="arg_ipNotation">IP-notation</param>
|
---|
1009 | /// <param name="arg_arlDelimeter">Delimeter to parse IPString</param>
|
---|
1010 | /// <returns>IP-Address with Spaces</returns>
|
---|
1011 | protected static string MakeValidSpaces(string arg_sIP, IPNotation arg_ipNotation, ArrayList arg_arlDelimeter) {
|
---|
1012 | ArrayList arlIP = new ArrayList(arg_sIP.Split((char[])arg_arlDelimeter.ToArray(typeof(char))));
|
---|
1013 | //don't check the 1st 2 elemnt if its IPv4 in IPv6-notation
|
---|
1014 | for (int i = (arg_ipNotation.ToString().IndexOf("IPv6IPv4") == 0 ? 2 : 0);
|
---|
1015 | //don't check the subnet element
|
---|
1016 | i < (arg_ipNotation.ToString().IndexOf("CIDR") > 0 ? arlIP.Count - 1 : arlIP.Count);
|
---|
1017 | i++) {
|
---|
1018 | switch (arg_ipNotation) {
|
---|
1019 | case IPNotation.IPv4Decimal:
|
---|
1020 | case IPNotation.IPv4DecimalCIDR:
|
---|
1021 | while (arlIP[i].ToString().Length < 3)
|
---|
1022 | arlIP[i] = arlIP[i].ToString() + " ";
|
---|
1023 | break;
|
---|
1024 | case IPNotation.IPv4Binary:
|
---|
1025 | case IPNotation.IPv4BinaryCIDR:
|
---|
1026 | while (arlIP[i].ToString().Length < 8)
|
---|
1027 | arlIP[i] = arlIP[i].ToString() + " ";
|
---|
1028 | break;
|
---|
1029 | default:
|
---|
1030 | break;
|
---|
1031 | }
|
---|
1032 | }
|
---|
1033 |
|
---|
1034 | return IPAddressTextBox.MakeIP((string[])arlIP.ToArray(typeof(string)), arg_ipNotation);
|
---|
1035 | }
|
---|
1036 |
|
---|
1037 | /// <summary>
|
---|
1038 | /// Adds Zeroes to given IP-Address, so it fits in the textfield
|
---|
1039 | /// </summary>
|
---|
1040 | /// <param name="arg_sIP">IP-String</param>
|
---|
1041 | /// <param name="arg_ipNotation">IP-notation</param>
|
---|
1042 | /// <param name="arg_arlDelimeter">Delimeter to parse IPString</param>
|
---|
1043 | /// <returns>IP-Address with Spaces</returns>
|
---|
1044 | protected static string MakeValidZeroes(string arg_sIP, IPNotation arg_ipNotation, ArrayList arg_arlDelimeter) {
|
---|
1045 | ArrayList arlIP = new ArrayList(arg_sIP.Split((char[])arg_arlDelimeter.ToArray(typeof(char))));
|
---|
1046 | //don't check the 1st 2 elemnt if its IPv4 in IPv6-notation
|
---|
1047 | for (int i = (arg_ipNotation.ToString().IndexOf("IPv6IPv4") == 0 ? 2 : 0);
|
---|
1048 | //don't check the subnet element
|
---|
1049 | i < (arg_ipNotation.ToString().IndexOf("CIDR") > 0 ? arlIP.Count - 1 : arlIP.Count);
|
---|
1050 | i++) {
|
---|
1051 | switch (arg_ipNotation) {
|
---|
1052 | case IPNotation.IPv4Decimal:
|
---|
1053 | case IPNotation.IPv4DecimalCIDR:
|
---|
1054 | while (arlIP[i].ToString().Length < 3)
|
---|
1055 | arlIP[i] = "0" + arlIP[i].ToString();
|
---|
1056 | break;
|
---|
1057 | case IPNotation.IPv4Binary:
|
---|
1058 | case IPNotation.IPv4BinaryCIDR:
|
---|
1059 | while (arlIP[i].ToString().Length < 8)
|
---|
1060 | arlIP[i] = "0" + arlIP[i].ToString();
|
---|
1061 | break;
|
---|
1062 | default:
|
---|
1063 | break;
|
---|
1064 | }
|
---|
1065 | }
|
---|
1066 |
|
---|
1067 | return IPAddressTextBox.MakeIP((string[])arlIP.ToArray(typeof(string)), arg_ipNotation);
|
---|
1068 | }
|
---|
1069 |
|
---|
1070 | /// <summary>
|
---|
1071 | /// Creates IP-Addresstring from given StrignArray and Notation
|
---|
1072 | /// </summary>
|
---|
1073 | /// <param name="arg_sIP">String-Array with elements for IP-Address</param>
|
---|
1074 | /// <param name="arg_ipNotation">Notation of IP-Address</param>
|
---|
1075 | /// <returns>IPAddress-String</returns>
|
---|
1076 | protected static string MakeIP(string[] arg_sIP, IPNotation arg_ipNotation) {
|
---|
1077 | string s = "";
|
---|
1078 | for (int i = 0; i < arg_sIP.Length; i++) {
|
---|
1079 | switch (arg_ipNotation) {
|
---|
1080 | case IPNotation.IPv4Decimal:
|
---|
1081 | case IPNotation.IPv4Binary:
|
---|
1082 | s += (arg_sIP[i].Length > 0 ? arg_sIP[i] : "0") + (i < (arg_sIP.Length - 1) ? "." : "");
|
---|
1083 | break;
|
---|
1084 | case IPNotation.IPv4DecimalCIDR:
|
---|
1085 | case IPNotation.IPv4BinaryCIDR:
|
---|
1086 | s += (arg_sIP[i].Length > 0 ? arg_sIP[i] : "0") + (i < (arg_sIP.Length - 2) ? "." : (i < arg_sIP.Length - 1) ? "/" : "");
|
---|
1087 | break;
|
---|
1088 | default:
|
---|
1089 | break;
|
---|
1090 | }
|
---|
1091 | }
|
---|
1092 | return s;
|
---|
1093 | }
|
---|
1094 |
|
---|
1095 | #endregion //Helperfunctions
|
---|
1096 | }
|
---|
1097 | }
|
---|