From 93f0c6e4a3c3646b090cf1b8c459a3ca03ed214c Mon Sep 17 00:00:00 2001 From: Tim Young Date: Mon, 29 Apr 2019 17:15:10 -0500 Subject: [PATCH] Allow ping/arp/traceroute to cancel by 'x' WellDone ought to default to "false" so that canceling the form does not return a true. However, that then means that the validating (particularly for DHCP) needs to be handled differently. Otherwise validation would ONLY occur when you press OK. (Not a terrible thing, but also not ideal.) In any case, this is a solution I found on the internet. It seems to work nicely. Validating still runs unless the form is closing (via ESC, Cancel, or X), including when moving from textbox to textbox, or tabbing to OK. I think this also solves a lingering problem where DHCP couldn't X out without attempting to validate. So it feels like an all-around winner. Step to reproduce: Level0 ping test 1.) on net_switch0, try to ping pc0. -tab through and notice that pc0 resolves to 192.168.1.2 on OK button. 2.) close with form window using X -notice that the ping happens anyway. Same is true for arp and traceroute. --- EduNetworkBuilder/IPAddressEntry.cs | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/EduNetworkBuilder/IPAddressEntry.cs b/EduNetworkBuilder/IPAddressEntry.cs index 7d8aae6..70bf153 100644 --- a/EduNetworkBuilder/IPAddressEntry.cs +++ b/EduNetworkBuilder/IPAddressEntry.cs @@ -17,13 +17,24 @@ namespace EduNetworkBuilder { NB_IPAddress WhatToEdit; NB_IPAddress DHCPInterface=null; - bool WellDone = true; + bool WellDone = false; NetworkDevice ParentDevice = null; Point StartLocation = new Point (50,50); NB_IPAddress SavedIPAddress = null; ToolTip myTooltip = new ToolTip(); + //variable to hold true if the for is closing + private bool isFormClosing = false; + // Contant for the close message + private const int WM_CLOSE = 16; + //override the WndProc msg to trap the WM_CLOSE message + protected override void WndProc(ref Message m) + { + if (m.Msg == WM_CLOSE) + isFormClosing = true; + base.WndProc(ref m); + } public IPAddressEntry(NB_IPAddress toEdit, NetworkDevice ToEdit, Form ParentForm, bool JustPinging=false) { @@ -152,6 +163,7 @@ namespace EduNetworkBuilder WhatToEdit.Reparse(tbIPAddress.Text, tbNetmask.Text, tbGateway.Text); Network myNet = NB.GetNetwork(); myNet.StoreLastIP(WhatToEdit); + WellDone = true; Close(); } @@ -189,13 +201,12 @@ namespace EduNetworkBuilder if(SavedIPAddress != null) WhatToEdit.Reparse(SavedIPAddress.GetIPString, SavedIPAddress.GetMaskString, SavedIPAddress.GetGateway.ToIpString()); UpdateFieldsFromAddress(); - WellDone = false; Close(); } private void tbGateway_Validating(object sender, CancelEventArgs e) { - if (!WellDone) return; + if (isFormClosing) return; Network mynet = NB.GetNetwork(); if (ParentDevice != null) @@ -224,7 +235,7 @@ namespace EduNetworkBuilder private void tbNetmask_Validating(object sender, CancelEventArgs e) { - if (!WellDone) return; + if (isFormClosing) return; UInt32 taddress = tbNetmask.Text.ParseIp(); tbNetmask.Text = taddress.ToIpString(); @@ -241,7 +252,7 @@ namespace EduNetworkBuilder private void tbIPAddress_Validating(object sender, CancelEventArgs e) { - if (!WellDone) return; + if (isFormClosing) return; UInt32 taddress; Network mynet = NB.GetNetwork();