-
Notifications
You must be signed in to change notification settings - Fork 0
/
frmDeviceSettings.cs
186 lines (166 loc) · 6.76 KB
/
frmDeviceSettings.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
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
using System;
using System.Windows.Forms;
namespace uKNX_Configurator
{
public partial class frmDeviceSettings : Form
{
public GatewayItem Gateway { get; set; }
public frmDeviceSettings(GatewayItem gw)
{
InitializeComponent();
btnCancel.DialogResult = DialogResult.Cancel;
Gateway = gw;
if (gw.DhcpEnabled)
{
cbDhcpEnabled.Checked = true;
}
else
{
tbIpAddress.Text = gw.StaticIP;
tbSubnetMask.Text = gw.StaticSubnet;
tbGatewayAddress.Text = gw.StaticGateway;
tbDns1.Text = gw.StaticDns1;
tbDns2.Text = gw.StaticDns2;
}
if (gw.LocalUdpEnabled) cbLocalServerEnabled.Checked = true;
if (gw.BroadcastKnx) cbBroadcastKnxTraffic.Checked = true;
cbDhcpEnabled.Focus();
}
private void cbDhcpEnabled_CheckedChanged(object sender, EventArgs e)
{
if (cbDhcpEnabled.Checked)
{
tbIpAddress.Enabled = false;
tbSubnetMask.Enabled = false;
tbGatewayAddress.Enabled = false;
tbDns1.Enabled = false;
tbDns2.Enabled = false;
tbIpAddress.Text = "";
tbSubnetMask.Text = "";
tbGatewayAddress.Text = "";
tbDns1.Text = "";
tbDns2.Text = "";
}
else
{
MessageBox.Show("You have selected not using DHCP. Please check your inputs twice, a wrong entry may cause your gateway inaccessable", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
tbIpAddress.Enabled = true;
tbSubnetMask.Enabled = true;
tbGatewayAddress.Enabled = true;
tbDns1.Enabled = true;
tbDns2.Enabled = true;
tbIpAddress.Text = Gateway.StaticIP;
tbSubnetMask.Text = Gateway.StaticSubnet;
tbGatewayAddress.Text = Gateway.StaticGateway;
tbDns1.Text = Gateway.StaticDns1;
tbDns2.Text = Gateway.StaticDns2;
}
}
private void cbLocalServerEnabled_CheckedChanged(object sender, EventArgs e)
{
if (cbLocalServerEnabled.Checked)
{
cbBroadcastKnxTraffic.Enabled = true;
}
else
{
cbBroadcastKnxTraffic.Enabled = false;
}
}
private void btnSaveSettings_Click(object sender, EventArgs e)
{
if (cbDhcpEnabled.Checked)
{
Gateway.DhcpEnabled = true;
} else
{
Gateway.DhcpEnabled = false;
if (Helper.ValidateIPv4(tbIpAddress.Text)
&& Helper.ValidateIPv4(tbSubnetMask.Text) && Helper.ValidateIPv4(tbGatewayAddress.Text))
{
if (tbSubnetMask.Text != Helper.GetSubnetMask(Helper.GetLocalIPAddress()).ToString())
{
DialogResult dialogResult = MessageBox.Show("You are trying to set gateway's subnet mask "
+ tbSubnetMask.Text + " different than your computer "
+ Helper.GetSubnetMask(Helper.GetLocalIPAddress())
+ ". Usually they are both expected same. Inconsistent network settings can make your gateway inaccessable. Are you sure?", "Check input", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (dialogResult == DialogResult.No)
{
return;
}
}
string computerIp = Helper.GetLocalIPAddress().ToString();
if (computerIp.Split('.')[0] != tbIpAddress.Text.Split('.')[0]
|| computerIp.Split('.')[1] != tbIpAddress.Text.Split('.')[1]
|| computerIp.Split('.')[2] != tbIpAddress.Text.Split('.')[2])
{
DialogResult dialogResult = MessageBox.Show("You are trying to set gateway's ip address "
+ tbIpAddress.Text + " quite different than your computer " + computerIp
+ ". Inconsistent network settings can make your gateway inaccessable. Are you sure?", "Check input", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (dialogResult == DialogResult.No)
{
return;
}
}
Gateway.StaticIP = tbIpAddress.Text;
Gateway.StaticSubnet = tbSubnetMask.Text;
Gateway.StaticGateway = tbGatewayAddress.Text;
Gateway.StaticDns1= tbDns1.Text;
Gateway.StaticDns2 = tbDns2.Text;
} else
{
MessageBox.Show("At least one IP address entered is wrong", "Check input", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
}
Gateway.LocalUdpEnabled = (cbLocalServerEnabled.Checked) ? true : false;
Gateway.BroadcastKnx = (cbBroadcastKnxTraffic.Checked) ? true : false;
DialogResult = DialogResult.OK;
}
private void cbDhcpEnabled_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
tbIpAddress.Focus();
tbIpAddress.SelectAll();
}
}
private void tbIpAddress_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
tbSubnetMask.Focus();
tbSubnetMask.SelectAll();
}
}
private void tbSubnetMask_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
tbGatewayAddress.Focus();
tbGatewayAddress.SelectAll();
}
}
private void tbGatewayAddress_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
cbLocalServerEnabled.Focus();
}
}
private void cbLocalServerEnabled_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
cbBroadcastKnxTraffic.Focus();
}
}
private void cbBroadcastKnxTraffic_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
btnSaveSettings.Focus();
}
}
}
}