m66n / ipaddresscontrollib

Automatically exported from code.google.com/p/ipaddresscontrollib
Other
62 stars 26 forks source link

Problem in Validating and Validated Events #16

Closed Bahaa-Sobhy closed 7 years ago

Bahaa-Sobhy commented 7 years ago

hello m66n,

First I would like to thank you for this great work... But at the practical scenario, when you add many IPAddressControl Controls (ex. for IP, subnet mask, default gateway... etc) , it causes confusion to the events (Validating and Validated) they hang to the first control and don't work for the second, third, fourth... Controls.

for example:

        private void IPv4_Validating(object sender, CancelEventArgs e)
        {
            if (IPv4.AnyBlank) { IPv4.BackColor = Color.Red; e.Cancel = true;}
            else{e.Cancel = false;}
        }
        private void IPv4_Validated(object sender, EventArgs e)
        {
           IPv4.BackColor = Color.Green;
        }

       private void Subnet_Validating(object sender, CancelEventArgs e)
        {
            if ( Subnet.AnyBlank) {  Subnet.BackColor = Color.Red; e.Cancel = true;}
            else{e.Cancel = false;}
        }
        private void  Subnet_Validated(object sender, EventArgs e)
        {
            Subnet.BackColor = Color.Green;
        }

       private void Gateway_Validating(object sender, CancelEventArgs e)
        {
            if ( Gateway.AnyBlank) {  Gateway.BackColor = Color.Red; e.Cancel = true;}
            else{e.Cancel = false;}
        }
        private void  Gateway_Validated(object sender, EventArgs e)
        {
            Gateway.BackColor = Color.Green;
        }

Whatever is the action to take inside these two events, the second and third controls (in the given example Subnet and Gateway) they don't do the required actions, instead only the BackColor of the first control (IPv4) is changed, moreover the application can't be closed by any mean!

When I tried to remove "e.Cancel = true;" nothing changes in the Controls problem, but the Applicatopn can be closed normally.

What do you think the reason of this problem? I think it's something related to Disposing the Control event when it's done... Any idea/solution would be highly appreciated.

Thanks in advance.

m66n commented 7 years ago

By calling e.Cancel = true, you're ceasing the event propagation that allows the control to lose focus. Each control has its own associated event, so handling an event in one control has no effect on the other controls. In your case it seems you should just set the colors to indicate invalid data and don't touch e.

m66n commented 7 years ago

Also, take a look at the event propagation chain. Validated follows Validating. So you're setting one color and then immediately setting another.

Bahaa-Sobhy commented 7 years ago

Thanks m66n... I removed one of them, but in vain...

Thanks anyway :)

m66n commented 7 years ago

It has nothing to do with the number of controls. Try doing something similar with a set of TextBoxs and you'll see the same behavior. You're cancelling an event when you shouldn't. And you should do all of your coloring in Validating or Validated, but not in both.