-
Notifications
You must be signed in to change notification settings - Fork 2
/
FormUtils.cs
41 lines (35 loc) · 1.1 KB
/
FormUtils.cs
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
using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Windows.Forms;
using System.Drawing;
namespace JoeriBekker.PuttyTunnelManager
{
class FormUtils
{
internal static int ValidatePortTextBox(object sender, CancelEventArgs e)
{
TextBox textBox = sender as TextBox;
int result = -1;
if (textBox == null || !textBox.Visible)
return result;
try
{
result = Int32.Parse(textBox.Text);
if (result <= 0)
throw new FormatException("Value must be positive.");
if (result >= 65536)
throw new FormatException("Value must be smaller than 65536.");
textBox.BackColor = SystemColors.Window;
}
catch (FormatException)
{
textBox.BackColor = Color.LightCoral;
textBox.Focus();
e.Cancel = true;
}
return result;
}
}
}