-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainWindow.xaml.cs
139 lines (111 loc) · 3.78 KB
/
MainWindow.xaml.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
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
using System;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Navigation;
namespace SubnetCalculatorGUI
{
public class Cbx
{
UInt16 num=420;
public string Text { get { return String.Format("/{0} \t {1} IPs", num, (1u << (32 - (int)num) )-2); } }
public UInt16 Value { get { return num; } set { num = (UInt16)value; } }
public override string ToString()
{
return "/" + num.ToString();
}
}
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
bool isInitialized = false;
SubnetCalculator calc = new SubnetCalculator();
public MainWindow()
{
InitializeComponent();
isInitialized = true;
// Change all input to default values from calc.
IP.Text = calc.Ip;
Prefix.Text = calc.Prefix.ToString();
PrefixSlider.Value = (double) calc.Prefix;
// Refresh display with updated values.
Refresh();
}
// Refresh display with updated values.
private void Refresh()
{
if (isInitialized)
{
txtbxNetworkID.Text = calc.NetworkID;
txtbxBroadcast.Text = calc.BroadCast;
txtbxFirstIP.Text = calc.FirstIp;
txtbxLastIP.Text = calc.LastIp;
txtbxSubnet.Text = calc.SubnetMask;
txtbxWildcard.Text = calc.WildcardMask;
txtbxHosts.Text = calc.Hosts;
}
}
// Give changed subnet mask to calc and refresh displayed information.
private void Slider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
//Prevent prefix from being changed while selected
if (!Prefix.IsSelectionActive)
{
var slider = sender as Slider;
calc.Prefix = (ushort) slider.Value;
Prefix.Text = calc.Prefix.ToString();
this.Refresh();
}
}
private void InputChanged(object sender, TextChangedEventArgs e)
{
var textbox = sender as TextBox;
if(textbox.Name == "IP")
{
calc.Ip = IP.Text;
if (calc.Ip == IP.Text)
{
IP.BorderBrush = System.Windows.Media.Brushes.Green;
}
else
{
IP.BorderBrush = System.Windows.Media.Brushes.Red;
}
}
else
{
try
{
calc.Prefix = Convert.ToUInt16(Prefix.Text);
}
catch
{
}
if (calc.Prefix.ToString() == Prefix.Text)
{
PrefixSlider.Value = calc.Prefix;
Prefix.BorderBrush = System.Windows.Media.Brushes.Green;
}
else
{
Prefix.BorderBrush = System.Windows.Media.Brushes.Red;
}
}
Refresh();
}
// Only allow number or dots in text.
private void InputPreviewTextInput(object sender, TextCompositionEventArgs e)
{
Regex regex = new Regex("[^0-9.]+");
e.Handled = regex.IsMatch(e.Text);
}
// Open browser with given hyperlink.
private void Hyperlink_RequestNavigate(object sender, RequestNavigateEventArgs e)
{
System.Diagnostics.Process.Start(e.Uri.AbsoluteUri);
}
}
}